Editing an already open word document with VB6

N

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
 
N

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
 
N

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
 
J

Jonathan West

Nam said:
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
 
N

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
 
S

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
 
J

Jonathan West

Nam said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top