PDA

View Full Version : From Excel , determine if Word is open



velohead
08-18-2010, 06:58 PM
Hi All,

I am at novice level.
I have been reading other "not dis-similiar" threads, but have failed to grasp what bits of code I need/dont need.
So I'll ask the question.

Background.
I have a macro, that resides in an excel file.
The macro does this.......
copy multiple variables from excel sheet1
open specific word document_1
paste those variables in the correct place
delete specific paragraphs (based on excel variables)
save file as document_2
The macro works very well indeed.

Question
There may be a problem occuring if either document_1 or document_2 is open.
I thought the way to go would be this...
Test if Word App is open
If Yes, then stop excel macro (with excel msgbox message)
If No, then continue.

So, how do I test if Word App is open, please.



My relevant code that may be of use.....

Set objWord = CreateObject("Word.Application")
objWord.Visible = True

Set objDoc = objWord.Documents.Open("I:\HOFolders\Raymond\Word Development\Ex 08 - semi final\HR Doc B - Template.doc")


Thanks

Bob Phillips
08-19-2010, 07:22 AM
Use GetObject



On Error Resume Next
Set objWord = GetObject(, "Word.Application")
On Error Goto 0
If Not objWord Is Nothing Then

MsgBox "Word already open"
End If


But why not just use that instance of Word if it is open?

velohead
08-19-2010, 03:01 PM
But why not just use that instance of Word if it is open?

I guess I could, but as a novice, I was thinking that.....
..if Word App is not open, then by definition Word Doc_1 (and also Word Doc_2) is therefore not open.

Perhaps rather 'crude and unrefined' but it would satisfy the basic test.
I guess with more experience (and knowledge) I could refine it to be more succint.



Just so I can expand my limited knowledge......
What does getObject do ?
What 'value' does it assign ?
What if there are 2 objects 'open' ? (does it pick the 'first' ?)

Bob Phillips
08-20-2010, 03:00 AM
GetObject will just pick up an instance of the targetted application. As you can see in my code, there is a Set variable that uses GetObject, so that variable will have a reference to that instance (if one exists, else it will be Nothing). If there are 2 instances, it will pick up one, but it is indeterminate which one.

You can test if a document is already open, withing getting a WOrd instance at all.

velohead
08-22-2010, 09:35 PM
Thanks for the explanation.


You can test if a document is already open, withing getting a Word instance at all.

Maybe that is a better (more specific) way to go.
How would I test to see if documant_1 is open.

Bob Phillips
08-25-2010, 05:12 PM
In a similar manner, set an object variable to the document and check it for nothing.

Tinbendr
08-29-2010, 03:52 AM
If you are using HR Doc B - Template.doc as a template, then save it as a template, then open a new file based on the template. This way, you only have one file open.

Set objDoc = objWord.Documents.Add("I:\HOFolders\Raymond\Word Development\Ex 08 - semi final\HR Doc B - Template.dot")