Home All Groups Group Topic Archive Search About

Editing an already open word document with VB6

Author
7 Oct 2005 8:14 PM
Nam
“Without closing” an already open word document, how can we use a VB6 program
to run a macro of that open document? I know how to do this by opening the
document that was closed, as follows:

Dim oApp As Word.Application
Dim oDoc As Word.Document

  Set oApp = New Word.Application
  Set oDoc = oApp.Documents.Open("C:\Test.doc")
  oApp.Run "Macro1", Param1
  oDoc.Save
  oDoc.Close
  oApp.Quit

The reason I want to do it for an already open document is that I want my
VB6 program to insert a certain text at the insertion point (where the cursor
is) of the document. Doing this task through a word macro is simple, running
the following macro will insert the desired text at the insertion point:

Sub Macro1(str As String)
Selection.TypeText Text:="Text to be entered"
End Sub

But I need to use my VB6 program since it does several other tasks besides
Word Automation.

Thanks,
Nam

Author
7 Oct 2005 8:55 PM
Jonathan West
Hi Nam,

Is the document the currently active document? If not, how do you identify
it?

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org

Show quoteHide quote
"Nam" <N**@discussions.microsoft.com> wrote in message
news:4DF8B51B-A1AD-4AFB-85F2-D6533B61099F@microsoft.com...
> “Without closing” an already open word document, how can we use a VB6
> program
> to run a macro of that open document? I know how to do this by opening the
> document that was closed, as follows:
>
> Dim oApp As Word.Application
> Dim oDoc As Word.Document
>
>  Set oApp = New Word.Application
>  Set oDoc = oApp.Documents.Open("C:\Test.doc")
>  oApp.Run "Macro1", Param1
>  oDoc.Save
>  oDoc.Close
>  oApp.Quit
>
> The reason I want to do it for an already open document is that I want my
> VB6 program to insert a certain text at the insertion point (where the
> cursor
> is) of the document. Doing this task through a word macro is simple,
> running
> the following macro will insert the desired text at the insertion point:
>
> Sub Macro1(str As String)
> Selection.TypeText Text:="Text to be entered"
> End Sub
>
> But I need to use my VB6 program since it does several other tasks besides
> Word Automation.
>
> Thanks,
> Nam
Author
7 Oct 2005 9:49 PM
Nam
Jonathan,

The document will be the currently active document as follows: I have placed
a custom commandBar button on a Word CommandBar control. User opens a word
document, places a cursor at a certain point on the document. By clicking on
the commandBar button that I placed on the toolbar, user opens my VB6
application. Clicking on a button of my VB6 application should insert some
symbol/text etc at the insertion (cursor) point of the document.

So, when my VB6 application opens, how do I connect to that already open
(currently active) document?

Thanks,
Nam


Show quoteHide quote
"Jonathan West" wrote:

> Hi Nam,
>
> Is the document the currently active document? If not, how do you identify
> it?
>
> --
> Regards
> Jonathan West - Word MVP
> www.intelligentdocuments.co.uk
> Please reply to the newsgroup
> Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
>
> "Nam" <N**@discussions.microsoft.com> wrote in message
> news:4DF8B51B-A1AD-4AFB-85F2-D6533B61099F@microsoft.com...
> > “Without closing” an already open word document, how can we use a VB6
> > program
> > to run a macro of that open document? I know how to do this by opening the
> > document that was closed, as follows:
> >
> > Dim oApp As Word.Application
> > Dim oDoc As Word.Document
> >
> >  Set oApp = New Word.Application
> >  Set oDoc = oApp.Documents.Open("C:\Test.doc")
> >  oApp.Run "Macro1", Param1
> >  oDoc.Save
> >  oDoc.Close
> >  oApp.Quit
> >
> > The reason I want to do it for an already open document is that I want my
> > VB6 program to insert a certain text at the insertion point (where the
> > cursor
> > is) of the document. Doing this task through a word macro is simple,
> > running
> > the following macro will insert the desired text at the insertion point:
> >
> > Sub Macro1(str As String)
> > Selection.TypeText Text:="Text to be entered"
> > End Sub
> >
> > But I need to use my VB6 program since it does several other tasks besides
> > Word Automation.
> >
> > Thanks,
> > Nam
>
>
Author
8 Oct 2005 4:48 AM
Jonathan West
Hi Nam

Set oDoc = oApp.ActiveDocument

Carry on from there.

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org

Show quoteHide quote
"Nam" <N**@discussions.microsoft.com> wrote in message
news:1E0AEAF0-48C9-48D7-99FE-9706C1425FFD@microsoft.com...
> Jonathan,
>
> The document will be the currently active document as follows: I have
> placed
> a custom commandBar button on a Word CommandBar control. User opens a word
> document, places a cursor at a certain point on the document. By clicking
> on
> the commandBar button that I placed on the toolbar, user opens my VB6
> application. Clicking on a button of my VB6 application should insert some
> symbol/text etc at the insertion (cursor) point of the document.
>
> So, when my VB6 application opens, how do I connect to that already open
> (currently active) document?
>
> Thanks,
> Nam
>
>
> "Jonathan West" wrote:
>
>> Hi Nam,
>>
>> Is the document the currently active document? If not, how do you
>> identify
>> it?
>>
>> --
>> Regards
>> Jonathan West - Word MVP
>> www.intelligentdocuments.co.uk
>> Please reply to the newsgroup
>> Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
>>
>> "Nam" <N**@discussions.microsoft.com> wrote in message
>> news:4DF8B51B-A1AD-4AFB-85F2-D6533B61099F@microsoft.com...
>> > “Without closing” an already open word document, how can we use a VB6
>> > program
>> > to run a macro of that open document? I know how to do this by opening
>> > the
>> > document that was closed, as follows:
>> >
>> > Dim oApp As Word.Application
>> > Dim oDoc As Word.Document
>> >
>> >  Set oApp = New Word.Application
>> >  Set oDoc = oApp.Documents.Open("C:\Test.doc")
>> >  oApp.Run "Macro1", Param1
>> >  oDoc.Save
>> >  oDoc.Close
>> >  oApp.Quit
>> >
>> > The reason I want to do it for an already open document is that I want
>> > my
>> > VB6 program to insert a certain text at the insertion point (where the
>> > cursor
>> > is) of the document. Doing this task through a word macro is simple,
>> > running
>> > the following macro will insert the desired text at the insertion
>> > point:
>> >
>> > Sub Macro1(str As String)
>> > Selection.TypeText Text:="Text to be entered"
>> > End Sub
>> >
>> > But I need to use my VB6 program since it does several other tasks
>> > besides
>> > Word Automation.
>> >
>> > Thanks,
>> > Nam
>>
>>
Author
10 Oct 2005 5:00 PM
Nam
Jonathan,

I guess I may not have explained my question properly. After I open
C:\Test.doc and run the following code from VB6, it does not work:

Private Sub Command1_Click()
  Dim oApp As Word.Application
  Dim oDoc As Word.Document

  Set oApp = New Word.Application
  Set oDoc = oApp.ActiveDocument
  Debug.Print oDoc.Content.Text

  Set oDoc = Nothing
  Set oApp = Nothing
End Sub

The above code give the following error:
Document is not available because no document is open

When I replaced [Set oDoc = oApp.ActiveDocument]  with the following it
worked:

Set oDoc = oApp.Documents.Open(FileName:="C:\Test.doc", Revert:=False)

The key was to use the parameter "Revert" described on MSDN as:
Revert:   Optional Variant. Controls what happens if Name is the file name
of an open document. True to discard any unsaved changes to the open document
and reopen the file. False to activate the open document.

Thanks,
Nam

Show quoteHide quote
"Jonathan West" wrote:

> Hi Nam
>
> Set oDoc = oApp.ActiveDocument
>
> Carry on from there.
>
> --
> Regards
> Jonathan West - Word MVP
> www.intelligentdocuments.co.uk
> Please reply to the newsgroup
> Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
>
> "Nam" <N**@discussions.microsoft.com> wrote in message
> news:1E0AEAF0-48C9-48D7-99FE-9706C1425FFD@microsoft.com...
> > Jonathan,
> >
> > The document will be the currently active document as follows: I have
> > placed
> > a custom commandBar button on a Word CommandBar control. User opens a word
> > document, places a cursor at a certain point on the document. By clicking
> > on
> > the commandBar button that I placed on the toolbar, user opens my VB6
> > application. Clicking on a button of my VB6 application should insert some
> > symbol/text etc at the insertion (cursor) point of the document.
> >
> > So, when my VB6 application opens, how do I connect to that already open
> > (currently active) document?
> >
> > Thanks,
> > Nam
> >
> >
> > "Jonathan West" wrote:
> >
> >> Hi Nam,
> >>
> >> Is the document the currently active document? If not, how do you
> >> identify
> >> it?
> >>
> >> --
> >> Regards
> >> Jonathan West - Word MVP
> >> www.intelligentdocuments.co.uk
> >> Please reply to the newsgroup
> >> Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
> >>
> >> "Nam" <N**@discussions.microsoft.com> wrote in message
> >> news:4DF8B51B-A1AD-4AFB-85F2-D6533B61099F@microsoft.com...
> >> > “Without closing” an already open word document, how can we use a VB6
> >> > program
> >> > to run a macro of that open document? I know how to do this by opening
> >> > the
> >> > document that was closed, as follows:
> >> >
> >> > Dim oApp As Word.Application
> >> > Dim oDoc As Word.Document
> >> >
> >> >  Set oApp = New Word.Application
> >> >  Set oDoc = oApp.Documents.Open("C:\Test.doc")
> >> >  oApp.Run "Macro1", Param1
> >> >  oDoc.Save
> >> >  oDoc.Close
> >> >  oApp.Quit
> >> >
> >> > The reason I want to do it for an already open document is that I want
> >> > my
> >> > VB6 program to insert a certain text at the insertion point (where the
> >> > cursor
> >> > is) of the document. Doing this task through a word macro is simple,
> >> > running
> >> > the following macro will insert the desired text at the insertion
> >> > point:
> >> >
> >> > Sub Macro1(str As String)
> >> > Selection.TypeText Text:="Text to be entered"
> >> > End Sub
> >> >
> >> > But I need to use my VB6 program since it does several other tasks
> >> > besides
> >> > Word Automation.
> >> >
> >> > Thanks,
> >> > Nam
> >>
> >>
>
>
Author
10 Oct 2005 5:14 PM
Jonathan West
"Nam" <N**@discussions.microsoft.com> wrote in message
news:BFECE805-1A39-472B-9308-0AF062BC5C22@microsoft.com...
> Jonathan,
>
> I guess I may not have explained my question properly. After I open
> C:\Test.doc and run the following code from VB6, it does not work:
>
> Private Sub Command1_Click()
>  Dim oApp As Word.Application
>  Dim oDoc As Word.Document
>
>  Set oApp = New Word.Application

Change the line above to this, to pick up the current instance of Word
rather than creating a new one.

Set oApp = Word.Application

>  Set oDoc = oApp.ActiveDocument
>  Debug.Print oDoc.Content.Text
>
>  Set oDoc = Nothing
>  Set oApp = Nothing
> End Sub
>


That should fix the problem.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
Author
10 Oct 2005 5:47 PM
Nam
It did not work. It gives the following error # 4248:

"This command is not available because no document is open"

I guess, we still have to use the following that works, or something else:

Set oDoc = oApp.Documents.Open(FileName:="C:\Test.doc", Revert:=False)

Thanks,
Nam


Show quoteHide quote
"Jonathan West" wrote:

>
> "Nam" <N**@discussions.microsoft.com> wrote in message
> news:BFECE805-1A39-472B-9308-0AF062BC5C22@microsoft.com...
> > Jonathan,
> >
> > I guess I may not have explained my question properly. After I open
> > C:\Test.doc and run the following code from VB6, it does not work:
> >
> > Private Sub Command1_Click()
> >  Dim oApp As Word.Application
> >  Dim oDoc As Word.Document
> >
> >  Set oApp = New Word.Application
>
> Change the line above to this, to pick up the current instance of Word
> rather than creating a new one.
>
> Set oApp = Word.Application
>
> >  Set oDoc = oApp.ActiveDocument
> >  Debug.Print oDoc.Content.Text
> >
> >  Set oDoc = Nothing
> >  Set oApp = Nothing
> > End Sub
> >
>
>
> That should fix the problem.
>
>
> --
> Regards
> Jonathan West - Word MVP
> www.intelligentdocuments.co.uk
> Please reply to the newsgroup
> Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
>
>
Author
10 Oct 2005 6:58 PM
Steve Barnett
How about something like... (bear in mind, it's off the top of my head):

Dim oApp as Word.Application
Dim oDoc as Word.Document
Dim oTestDoc as Word.Document

    set oApp = GetObject(, "Word.Application")
    for each oTestDoc in oApp.Documents
        if lcase$(oTestDoc.fullname) = "c:\test.doc" then
            set oDoc = oTestDoc
            oDoc.activate
            exit for
        end if
    next

    Debug.Print oDoc.Content.Text

    set oDoc  = nothing
    set oApp = nothing


You'll have to code for the circumstance where Word isn't loaded and/or your
doc isn't loaded.
HTH
Steve


Show quoteHide quote
"Nam" <N**@discussions.microsoft.com> wrote in message
news:291615E0-A403-4E05-BBE8-3AE61463C73F@microsoft.com...
> It did not work. It gives the following error # 4248:
>
> "This command is not available because no document is open"
>
> I guess, we still have to use the following that works, or something else:
>
> Set oDoc = oApp.Documents.Open(FileName:="C:\Test.doc", Revert:=False)
>
> Thanks,
> Nam
>
>
> "Jonathan West" wrote:
>
>>
>> "Nam" <N**@discussions.microsoft.com> wrote in message
>> news:BFECE805-1A39-472B-9308-0AF062BC5C22@microsoft.com...
>> > Jonathan,
>> >
>> > I guess I may not have explained my question properly. After I open
>> > C:\Test.doc and run the following code from VB6, it does not work:
>> >
>> > Private Sub Command1_Click()
>> >  Dim oApp As Word.Application
>> >  Dim oDoc As Word.Document
>> >
>> >  Set oApp = New Word.Application
>>
>> Change the line above to this, to pick up the current instance of Word
>> rather than creating a new one.
>>
>> Set oApp = Word.Application
>>
>> >  Set oDoc = oApp.ActiveDocument
>> >  Debug.Print oDoc.Content.Text
>> >
>> >  Set oDoc = Nothing
>> >  Set oApp = Nothing
>> > End Sub
>> >
>>
>>
>> That should fix the problem.
>>
>>
>> --
>> Regards
>> Jonathan West - Word MVP
>> www.intelligentdocuments.co.uk
>> Please reply to the newsgroup
>> Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
>>
>>
Author
11 Oct 2005 1:57 PM
Jonathan West
"Nam" <N**@discussions.microsoft.com> wrote in message
news:291615E0-A403-4E05-BBE8-3AE61463C73F@microsoft.com...
> It did not work. It gives the following error # 4248:
>
> "This command is not available because no document is open"
>


try again, having first checked that you haven't got a few extra instances
of word floating about as a result of you running Set oApp = New
Word.Application too many times.

if there is just *one* instance of Word running, and that it has your
document open and with the focus, then Set oDoc = oApp.ActiveDocument will
work.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org