|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Editing an already open word document with VB6to 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 Hi Nam,
Is the document the currently active document? If not, how do you identify it? -- Show quoteHide quoteRegards 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 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 > > Hi Nam
Set oDoc = oApp.ActiveDocument Carry on from there. -- Show quoteHide quoteRegards 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 >> >> 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 > >> > >> > > "Nam" <N**@discussions.microsoft.com> wrote in message Change the line above to this, to pick up the current instance of Word 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 rather than creating a new one. Set oApp = Word.Application > Set oDoc = oApp.ActiveDocument That should fix the problem.> Debug.Print oDoc.Content.Text > > Set oDoc = Nothing > Set oApp = Nothing > End Sub > -- 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 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 > > 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 >> >> "Nam" <N**@discussions.microsoft.com> wrote in message try again, having first checked that you haven't got a few extra instances 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" > 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 |
|||||||||||||||||||||||