Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detecting whether Word is already open

Status
Not open for further replies.

GaryRW

Programmer
Mar 8, 2002
67
GB
I am using the code below to detect word (MyWordApp is a global variable):

Public Sub StartWord()
On Error GoTo Err_StartWord
Dim wrdTmp As Word.Application
'On Error Resume Next

Set wrdTmp = GetObject(, "Word.Application")
If Err.number <> 0 Then Set wrdTmp = CreateObject(&quot;Word.Application&quot;)
Set myWordApp = wrdTmp
myWordApp.Application.Visible = True
Debug.Print &quot;word was already running&quot;

Exit_StartWord:
Exit Sub

Err_StartWord:
' err.number for word running but not able to be visible is 462
Debug.Print Err.number
Debug.Print &quot;No word running or won't be visible&quot;
'MsgBox Err.Description
Set myWordApp = CreateObject(&quot;Word.Application&quot;)
myWordApp.Application.Visible = True
Resume Exit_StartWord
End Sub

I am basically running a database in Access, which uses word to run mailmerges. In short, the speed to load up word is a significant part of the time to run the mailmerge and *lots* of small mail merges are needed by the users, hence I link to an instance of word already open.

As I am sure you have guessed, this creates problems and word occasionally crashes. I have noticed that this always seems to be when Outlook has a message open and is using Word to edit it.

Any ideas why this should happen? Is there a way to detect whether an outlook message is open and using Word to edit it (on those ocassions I could just start a new version of word)

Any help would be much appreciated - so far all I've been able to say to users is don't use Word to edit emails (and am fairly unpopular for that)
 
I replied to this thread a moment ago - Windows quit the browser and I lost the post (so this time I will be brief before flames start shooting out my comp or something - you just never know with Windows - save regular!)

Anyway, I think your best bet would be to use the API call to FindWindow - accompanied my the Classname of the app you are interested in (In this case its Word.) If you dont know the Classname for Word - use a program like &quot;WinDowse&quot; to help find this (I think as far as Word 2000 is concerned - its &quot;OpusApp&quot;.)

I would also suggest going to the MS support site and doing a search on MS access and &quot;Findwindow&quot;.

Opp.
 
Opp: Is using the Windows API a more reliable approach to info versus the scripting approach Gary's using?

Gary: This is just a stab at it, but what about checking for WordApp.Application.Visible before setting it (and don't set it if it's visible)? If it's open, visible, and in use it might be throwing some low-level page fault--it seems like this sort of thing crashes apps all the time when you do it from menus, etc. (Windows stability you know).

Another blind shot: What about dimensioning a document object and setting it to WordApp.Documents.Add, so that you start a new doc if Word is in the middle of something else.

 
It should be fairly realiable Quehay, but it will mean that you have to code for a certain version of Word (as the Classname may change .)

I just assumed the code Gary was using did not work( I probably misunderstodd the initial problem with his method.)

I also wondered about the clause &quot;Visible&quot;, Im not sure how this works - but will it depend on the Apps visability ? In that case - will it work if Word is Minimised ?

Cheers..
Opp.

P.S I would not worry too much at having to code for specific verisons of Word. The way MS change the Interfaces to COM objects means I doubt there is a future-assured method anyway ;-).
 
sorry, realised I didn't make myself at all clear. The code above works fine and finds word no problem. I was just trying to give a bit of background.


I found the above on a FAQ or thread some time ago, but was wondering whether anybody could suggest a way to further identify whether there was not only a copy of word open, but then to check to see whether it was being used to edit an outlook message - if it was, i'd steer clear and load my own copy up, otherwise I'd link to the open copy.

Apologies for misleading you helpful folk...
 
Ahh..ok n/p :). In that case - I guess I cant really add much more. You may want to try the method I mentioned and use the &quot;Classname&quot; to identify an instance of Word - then use the Caption method to check if it contains the word &quot;outlook&quot; (in combination with &quot;StrPos&quot;)

But I cant thin of an elegant alternative Im afraid - no.

Cheers..

Opp.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top