-1

I wrote Excel VBA to check whether any instance of Word is already running, but some problems are occurring.

  1. If I open the Word without opening a document, the line If Err.Number = 0 Then wdAppRunning = True returns False.

Open Word via Windows Start

enter image description here

The opened Word instance.

enter image description here

  1. If there is an instance of Word running on a background process, the line also returns False.
  2. If I open Word, and create or open a document, and then run the macro, it returns the expected result (True)

How can I manage the code to identify at least the situation n° 1?

Ps.: the code posted in the link Getting instances of Word and saving documents returns the same situation.

Sub wdAppRunning()
    Dim wdApp As Object
    Dim wdAppRunning As Boolean

    On Error Resume Next
    Set wdApp = GetObject(, "Word.Application")
    If Err.Number = 0 Then wdAppRunning = True

    MsgBox wdAppRunning
    Set wdApp = Nothing

End Sub
Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
  • Does this answer your question? [Getting instances of Word and saving documents](https://stackoverflow.com/questions/19775697/getting-instances-of-word-and-saving-documents) – braX Jan 26 '20 at 11:36
  • What do you mean by "if I open the Word without open a document"? – FaneDuru Jan 26 '20 at 11:57
  • It would also help for you to comment out `On Error Resume Next` to find out *what error* is being generated. Without that information, everything is a guess. Also, how many instances of Word are running when you see this behavior? If you're unsure, use the Windows Task Manager and look at the list of processes. I'm guessing there's more than one, which would explain the behavior. – Cindy Meister Jan 26 '20 at 16:40
  • @FaneDuru, "if I open the Word without open a document" means that I open the Word app using Windows Start Menu. – Carlos Matioli Jan 26 '20 at 20:09
  • But it is obvious, I think. With your code you are looking for an already open Word session! If none such a session exist it is normal to return `False`. What would you expect in such a case, trying to create an object from a non existing one? – FaneDuru Jan 26 '20 at 20:19
  • @CindyMeister, I did all the tests with the Windows Task Manager open. When the Word in the background processes area, the `GetObject` don't recognize it. When I first open the Word through Windows Start, `GetObject` also don't recognize. But if I open a document and close it, the `GetObject" recognize normally. In all tests there are only one instance running (first or background process). – Carlos Matioli Jan 26 '20 at 20:20
  • @FaneDuru, the thing is that in some cases there is an open instance, opened through windows start without opening any document. And even in this case it returns `False`. – Carlos Matioli Jan 26 '20 at 20:22
  • Can you explain how you open Word through Windows Start *without* it opening a document? This is *not* normal default behavior. This can only happen if something is interfering with how Word works. The only explanation I can come up with is that some other software is doing something to intefere, in the background. `GetObject` is only able to recognize the first instance of Word that was initiated - all further instances won't be "seen". This approach is used by software where the user's use of Word should not interfere with that program's working with Word. – Cindy Meister Jan 26 '20 at 21:06
  • @CindyMeister, this behavior happens when I manually open the instance. I edited the question and added 2 print screens to help. – Carlos Matioli Jan 27 '20 at 01:18
  • One information that I forgot. This code is running in Office 2019. Today I tried in Office 365, with the same procedure, and in every circunstance returns me `False`. – Carlos Matioli Jan 27 '20 at 11:14
  • Again, I cannot reproduce this. As I said in my last comment, there must be something installed on the system that's interfering with Word's default behavior. This is not something anybody here can solve. – Cindy Meister Jan 27 '20 at 12:29

3 Answers3

1

Something like this should work.

Option Explicit

Public Function IsWordRunning() As Boolean
    IsWordRunning = GetObject("winmgmts:\\.\root\cimv2").ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'WINWORD.EXE'").Count > 0
End Function

Public Sub Example()
    Debug.Print IsWordRunning()
End Sub

A quick bonus, this could be extended for an Executable name if you like.

Public Function IsProcessRunning(ExecutableName As String) As Boolean
    IsProcessRunning = GetObject("winmgmts:\\.\root\cimv2").ExecQuery("SELECT * FROM Win32_Process WHERE Name = '" & ExecutableName & "'").Count > 0
End Function

Example Usage

Public Sub Example()
    Debug.Print IsProcessRunning("WINWORD.EXE")
End Sub

Make sure the name you specify in IsProcessRunning() is the name as it appears in Task Manager.

Ryan Wildry
  • 5,612
  • 1
  • 15
  • 35
  • It worked perfectly, both of the suggestions, in all situations tested. And since I'm new with VBA, I will invest some time to understand the code. Many thanks!!! – Carlos Matioli Jan 28 '20 at 01:39
0

Alternatively you can try with

Public Function Is_Word_Running() As Boolean

   Dim WMG As Object, Proc As Object

    Is_Word_Running = False
    Set WMG = GetObject("winmgmts:")
    For Each Proc In WMG.InstancesOf("win32_process")
        If UCase(Trim(Proc.Name)) = "WINWORD.EXE" Then
            Is_Word_Running = True
            Exit For
        End If
    Next Proc
    Set WMG = Nothing

End Function
Zer0Kelvin
  • 334
  • 2
  • 7
  • Your code worked perfectly, but since @Ryan Wildry code is more compact, i will set him as the answer. But once again, many thanks!!! – Carlos Matioli Jan 28 '20 at 01:38
-1

Try this

    Public Function Is_Word_Running() As Boolean

       Dim Wrd As Object

       On Error Resume Next
       Set Wrd = GetObject(, "Word.Application")
       On Error GoTo 0
       Is_Word_Running = Not Wrd Is Nothing

    End Function
Zer0Kelvin
  • 334
  • 2
  • 7