| || |
Multiple Instances of Word and VBA
Does anyone know a way of specifying a specific Word instance to get at when generating a Word.Application object?
So I have an Application Under Test (AUT) that creates Word Documents and leaves them open. And I would like to use the VBA technology to deal with these open Word Documents. Namely to get the path and file names of the Documents and then close them. But I have run into a bit of an issue in that my AUT generates more than one instance of word.
The GetActiveWordApplicationObj function shown below is capable of finding an existing instance of word and returning you a Word.Application object that you can use to access the Documents collection. Which is all good. However, the method used gets you " a currently active ..." Word application instance. And I can not figure out how to organsie it so I can specify which Word instance I want my Word.Application object to point to.
At this point I find that my only option is to process the documents collection in the first instance of word that my function finds and then close that instance using the .Quit method. Then I need to wait a few seconds and excute GetActiveWordApplicationObj again to allow me to get at the second instance of Word.
This works OK except it has the nasty habit of closing all open word documents that you may have open. It would be nicer if anyone knew a way of specifying a Word instance to get at when trying to generate a Word.Application object.
<font class="small">Code:</font><hr /><pre>
Public Function GetActiveWordApplicationObj
On error resume next
Set oWordApp = GetObject( , "Word.Application")
<font color="green"> ' The MS doco on the GetObject function has this to say ...
' GetObject([pathname] [, class])
'If pathname is a zero-length string (""), GetObject returns a new object instance
' of the specified type.
' If the pathname argument is omitted, GetObject returns a currently active object of the specified type.
' If no object of the specified type exists, an error occurs.
' Note how this says " a currently active ..."
' There does not appear to be anyway of specifiying which instance of word will be returned. So to get at more than one instance of word
' as occurs in my Application Under Test (AUT) I find we have to do a .quit wdDoNotSaveChanges on one word instance and then call
' GetActiveWordApplicationObj again to get at the second instance
' But it would appear if you don't wait a few seconds after calling quit then then if you call GetActiveWordApplicationObj you will get a reference to an Application Object that will complain
' "The remote server machine does not exist or is unavailable."
If err.number <> 0 Then
' this will be
' error 429 "ActiveX component can't create object" when there are no Word Applications currently running</font>
set oWordApp = nothing
On error goto 0
Set GetActiveWordApplicationObj = oWordApp
Re: Multiple Instances of Word and VBA
Problem with GetObject function is that it works on the latest application launched. So what you have in mind to quit one instance and move on to another one is the right one.
The only thing you can do different is to get the object using the path of word document. But then that you have to know beforehand.