Results 1 to 12 of 12
  1. #1
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Is Word Running? (Word 97SR2)

    What is the best way for a VBA global app to test whether Word is already running on the PC then switch to it and exit? That is, switch to the running copy then exit the second copy. Is this even possible?
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  2. #2
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Is Word Running? (Word 97SR2)

    Use GetObject. If it fails (err #429), then use CreateObject. Post back if you need the code. --Sam
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  3. #3
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Is Word Running? (Word 97SR2)

    Sam,

    I'm trying to prevent a user from starting two instances or Word. When the first instance starts, GetObject returns 0 meaning Word is running -- 'cause it is. When the second instance starts, GetObject reports the same.

    So, I need a different approach.

    I tried Findwindow, but that works the same way. It will find OpusApp when the 1st instance starts -- add-ins apparently load AFTER the app is offically started and considered running.

    Got any other ideas?
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  4. #4
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Is Word Running? (Word 97SR2)

    Kevin, here's the code for Office 2K. I think that it will work for 97. <img src=/S/crossfingers.gif border=0 alt=crossfingers width=17 height=16><pre>Option Explicit
    Sub GetWord()
    Dim wrdApp As Word.Application
    Dim wrdDoc As Word.Document
    On Error Resume Next
    ' Try to get a reference to running Word
    Set wrdApp = GetObject(, "Word.Application")
    If Err.Number = 429 Then ' no running Word
    On Error GoTo 0
    Set wrdApp = CreateObject("Word.Application")
    ElseIf Err.Number <> 0 Then ' bad error
    MsgBox "Error: " & Err.discription
    Exit Sub
    End If
    On Error GoTo 0
    wrdApp.Visible = True
    Set wrdDoc = wrdApp.Documents.Add
    wrdDoc.ActiveWindow.SetFocus
    wrdDoc.Content = "My new document: " & wrdDoc.FullName
    End Sub</pre>

    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  5. #5
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Is Word Running? (Word 97SR2)

    Sam,

    Are you calling this from Word or another Office app?
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  6. #6
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Is Word Running? (Word 97SR2)

    Kevin,

    If you're running the code from Word, then you have the luxury of using the Task object:

    <pre>Dim aTask As Task
    For Each aTask In Tasks
    If InStr(aTask.Name, "Microsoft Word") Then
    MsgBox "Word is running!"
    End If
    Next 'aTask
    </pre>

    - the InStr is in there as it appears to work a little differently between 97 and 2000, if memory serves. In 97 it just returned "Microsoft Word" but in 2000 it returns "DocName - Microsoft Word".

    You can even use "aTask.Close" to quit the app (as long as you're sure which one is which).

    Gary

  7. #7
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Is Word Running? (Word 97SR2)

    <img src=/w3timages/blackline.gif width=33% height=2>
    > Are you calling this from Word?
    <img src=/w3timages/blackline.gif width=33% height=2>
    From Excel, sorry, when you said VBA Global App, I read non-Word app. Looks like Gary has bailed me out again! <img src=/S/mad.gif border=0 alt=mad width=15 height=15> Oh well, someone learning automation will like my code.
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  8. #8
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Is Word Running? (Word 97SR2)

    Oh, I liked your code right well. It just didn't work from Word. Sorry I was not clearer in the first post. Thanks though!
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  9. #9
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Is Word Running? (Word 97SR2)

    Gary,

    Thanks. I had tried the Tasks object many times over the years but never could get it to work just the way I wanted. It's the second instance of Word that I want to prohibit, so just testing for Word will return true for every instance started. I concluded that Tasks cannot tell that the currently starting Word task is actually the second task. VB has the App.PrevInstance method (or is it a property?) that is just what we need in VBA.

    After playing with the Task object for a while yesterday, I noticed that the .exists method does not read the Word Window caption literally to identify the task (as the .name property does). I change the Word caption when Word starts (one of my many faults), so the .name testing got to be a real mess and not reliable.

    Anyway, the .exists method appears to have some smarts behind it. By putting the following code in the WthEvents generated Application Event "Class_Initialize()", the first instance of Word starts fine. Apparently, the Class_initialize event must end before Windows puts the Tasks in the list. If the user tries to open Word again, they get the message and the second instance closes. The beauty of the .exists method is it doesn't matter that the Word Window caption displays something other than "Microsoft Word"

    Sub CheckForPreviousInstance()

    If Tasks.Exists("Word") Then
    MsgBox "A Microsoft Word session is already running on this PC.", vbOK, "Word is Running."
    Application.Quit
    End If

    End Sub
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  10. #10
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Is Word Running? (Word 97SR2)

    Verry clever!

    Just to see what would happen, I put a MsgBox function in both an AutoExec procedure and an App Events Class Initialize procedure in the same global template in Startup.

    The code in AutoExec ran before the code in Class Initialize, so possibly your test could just as easily go in an AutoExec. (And this also implies that all the code in both procedures has to finish, before Word is added to the Tasks list).

    I've always been fascinated by the Tasks object in Word - for ex. why did they choose to put this in Word only?
    You could use this object to build your own customized Tasks Manager - although offhand I can't think of a reason why you'd want to, other than that it would be cool....

    Gary

  11. #11
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Is Word Running? (Word 97SR2)

    I'm glad you like it. And thanks for the AutoExec Test. I keep thinking the WithEvents app object is supposed to come in handy somewhere, but the built-in auto macros seem to cover the bases just fine. I might just switch back based on your tests.

    To be as humble as possible in light of this discovery, I have to say I've not seen this solution anywhere. Not that it isn't tucked away on some newgroup or MVP site, but I couldn' t find it. I looked tirelessly at every "one instance at a time" (and upteen variations thereof) net search hit but nothing fit the Word situation. I still think there must be a way to do this with API calls, but the Tasks("Word") trick is too slick to care now.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  12. #12
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Is Word Running? (Word 97SR2)

    I tried it on Word 2002 SP1. Guess what? Word is an official task before AutoExec starts! So, the code does not work.

    The good news is, the code isn't necessary with 2002. If you start a second instance that has the same command line parameters as the first instance (including no command line params), Windows just switches you to the running instance. Now that's nice.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •