• Calling GetCurrentProcess in Word VBA Module (2k,XP)

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » Calling GetCurrentProcess in Word VBA Module (2k,XP)

    Author
    Topic
    #412759

    I’m trying to get the process handle for Word by calling GetCurrentProcess in a Word VBA module. However GetCurrentProcess is returning -1 (INVALID_HANDLE_VALUE). Anybody know what’s going on, and how I can get the Word process handle?

    Thanks…Dan

    Viewing 3 reply threads
    Author
    Replies
    • #905362

      This is not strictly a Word question. I don’t know why GetCurrentProcess fails, but try the following:

      Private Declare Function GetWindowThreadProcessId Lib “user32” ( _
      ByVal hwnd As Long, _
      ByRef lpdwProcessId As Long) As Long

      Private Declare Function FindWindow Lib “user32” Alias “FindWindowA” ( _
      ByVal lpClassName As String, _
      ByVal lpWindowName As String) As Long

      Function GetWordProcessID() As Long
      Dim lngWindowHandle As Long
      Dim lngProcessID As Long
      Dim lngThreadID As Long
      lngWindowHandle = FindWindow(“OpusApp”, vbNullString)
      lngThreadID = GetWindowThreadProcessId(lWindowHandle, lngProcessID)
      GetWordProcessID = lngProcessID
      End Function

      • #905554

        Thanks guys. This is somewhat interesting. I am able to get a valid processID and process handle for Word (see code below), but when I call
        WaitForInputIdle on the Word handle, it gives me an error 6: ERROR_INVALID_HANDLE. I have done this type of thing successfully
        before, but in that case I actually create the Word process via CreateProcess. However, in this case, I’m just retrieving
        the handle from a Word process that’s already out there. I wonder if I’m being denied SYNCHRONIZE access (although you would think I would get this
        error when I open the process handle).

        BTW, the reason I’m doing this is that my app requires the handle to the Word editing window (class “_WwG”). 999 times out
        of 1000, I successfully retrieve it. But occasionally, it doesn’t seem to be there; I’m thinking that, at the point that I
        look for the window, Word hasn’t finished initializing the document I’m looking at. So I thought:

        1. Put a WaitForInputIdle in another process
        2. When I fall through the wait, signal an event
        3. When I see that the event is signaled, THEN look for the “_WwG” window

        Any advice would be a big help. Thanks…Dan

        m_hProcess = OpenProcess(SYNCHRONIZE, 0, GetWordProcessID)
        If (m_hProcess INVALID_HANDLE_VALUE) Then
        rc = WaitForInputIdle(m_hProcess, 5)
        dwError = Err.LastDllError
        End If
        If (rc = WAIT_TIMEOUT) Then
        MsgBox “Wait timeout”
        ElseIf (rc = -1) Then
        MsgBox “Error ” & dwError
        Else
        MsgBox “Wait fell through”
        End If

        • #905558

          Sorry, this is far over my head.

        • #905559

          Sorry, this is far over my head.

        • #905631

          (Edited by jscher2000 on 26-Nov-04 18:59. )

          I’m with Hans. grin

          According to the documentation, the censored NewDocument and DocumentOpen application-level events fire “after” a document is created or opened. Now, I don’t know that it is “completely” set up and ready for you to get a Process ID, but could you maybe set a global variable or fire your code using one of those events? It seems a whole lot harder to get Windows to figure that out than to use Word’s own events, if they work.

          (On the one hand, I want to say we don’t know enough about your application to help you leverage the Word object model. On the other hand, what I’ve seen so far is sufficiently scary that I’m reluctant to ask. laugh )

          • #906123

            [indent]


            the NewDocument and DocumentOpen application-level events fire “after” a document is created or opened


            [/indent] This was indeed the case in Word 97, but is not entirely true in Word 2003. See my post 408195.

            • #906442

              Thanks Jan. In the thread you mentioned, you wrote (in reference to wdApp_NewDocument and wdApp_DocumentOpen events in Word2003): “They work fine, except for the fact that the doc is not shown until the start-up code in the event-procedure is finished”.

              Do you know whether, once these events finish, ALL of the child windows in the Word document have been created? For example, when I open a new Word window in Word XP on my machine, it contains the windows listed below (the number to the left indicates the child level of the window). Do you (or does anyone) know whether I can assume that they’ve all been created at the time wdApp_NewDocument exits?

              1. Caption: WordWindowExperiment.doc – Word Window Test: ClassName:OpusApp
              2. Caption: MsoDockLeft: ClassName:MsoCommandBarDock
              2. Caption: MsoDockRight: ClassName:MsoCommandBarDock
              2. Caption: MsoDockTop: ClassName:MsoCommandBarDock
              3. Caption: Reviewing: ClassName:MsoCommandBar
              3. Caption: CrossEyes: ClassName:MsoCommandBar
              3. Caption: Formatting: ClassName:MsoCommandBar
              3. Caption: Standard: ClassName:MsoCommandBar
              3. Caption: Menu Bar: ClassName:MsoCommandBar
              2. Caption: MsoDockBottom: ClassName:MsoCommandBarDock
              2. Caption: ***NONE: ClassName:_WwC
              2. Caption: ***NONE: ClassName:_WwF
              3. Caption: WordWindowExperiment.doc: ClassName:_WwB
              4. Caption: MSO Generic Control Container: ClassName:MsoCommandBar
              4. Caption: MSO Generic Control Container: ClassName:MsoCommandBar
              4. Caption: ***NONE: ClassName:_WwG
              4. Caption: ***NONE: ClassName:ScrollBar
              4. Caption: ***NONE: ClassName:_WwC
              4. Caption: ***NONE: ClassName:ScrollBar
              4. Caption: ***NONE: ClassName:_WwC
              4. Caption: ***NONE: ClassName:_WwC
              4. Caption: ***NONE: ClassName:_WwC

            • #906490

              You probably can test that by trying to retrieve those window names from inside a NewDocument event handler. smile

            • #906501

              I agree–except: I don’t know that I can trust the tests. Suppose I test it 1000 times and everything comes up perfect? I then release the app and the 1001st time, it doesn’t work. What do I tell the user?

              The problem is (as is often the case): does anyone really know how to find out it really means when Word fires these events?

            • #906502

              I agree–except: I don’t know that I can trust the tests. Suppose I test it 1000 times and everything comes up perfect? I then release the app and the 1001st time, it doesn’t work. What do I tell the user?

              The problem is (as is often the case): does anyone really know how to find out it really means when Word fires these events?

            • #906491

              You probably can test that by trying to retrieve those window names from inside a NewDocument event handler. smile

            • #906562

              I’m with Hans: this is far over my head. I just made an observation and thought it could have some bearing on your post.

            • #906563

              I’m with Hans: this is far over my head. I just made an observation and thought it could have some bearing on your post.

            • #906685

              Thanks all. I’ll let you know if I come up with something…Dan

            • #906686

              Thanks all. I’ll let you know if I come up with something…Dan

            • #906443

              Thanks Jan. In the thread you mentioned, you wrote (in reference to wdApp_NewDocument and wdApp_DocumentOpen events in Word2003): “They work fine, except for the fact that the doc is not shown until the start-up code in the event-procedure is finished”.

              Do you know whether, once these events finish, ALL of the child windows in the Word document have been created? For example, when I open a new Word window in Word XP on my machine, it contains the windows listed below (the number to the left indicates the child level of the window). Do you (or does anyone) know whether I can assume that they’ve all been created at the time wdApp_NewDocument exits?

              1. Caption: WordWindowExperiment.doc – Word Window Test: ClassName:OpusApp
              2. Caption: MsoDockLeft: ClassName:MsoCommandBarDock
              2. Caption: MsoDockRight: ClassName:MsoCommandBarDock
              2. Caption: MsoDockTop: ClassName:MsoCommandBarDock
              3. Caption: Reviewing: ClassName:MsoCommandBar
              3. Caption: CrossEyes: ClassName:MsoCommandBar
              3. Caption: Formatting: ClassName:MsoCommandBar
              3. Caption: Standard: ClassName:MsoCommandBar
              3. Caption: Menu Bar: ClassName:MsoCommandBar
              2. Caption: MsoDockBottom: ClassName:MsoCommandBarDock
              2. Caption: ***NONE: ClassName:_WwC
              2. Caption: ***NONE: ClassName:_WwF
              3. Caption: WordWindowExperiment.doc: ClassName:_WwB
              4. Caption: MSO Generic Control Container: ClassName:MsoCommandBar
              4. Caption: MSO Generic Control Container: ClassName:MsoCommandBar
              4. Caption: ***NONE: ClassName:_WwG
              4. Caption: ***NONE: ClassName:ScrollBar
              4. Caption: ***NONE: ClassName:_WwC
              4. Caption: ***NONE: ClassName:ScrollBar
              4. Caption: ***NONE: ClassName:_WwC
              4. Caption: ***NONE: ClassName:_WwC
              4. Caption: ***NONE: ClassName:_WwC

          • #906124

            [indent]


            the NewDocument and DocumentOpen application-level events fire “after” a document is created or opened


            [/indent] This was indeed the case in Word 97, but is not entirely true in Word 2003. See my post 408195.

        • #905632

          (Edited by jscher2000 on 26-Nov-04 18:59. )

          I’m with Hans. grin

          According to the documentation, the censored NewDocument and DocumentOpen application-level events fire “after” a document is created or opened. Now, I don’t know that it is “completely” set up and ready for you to get a Process ID, but could you maybe set a global variable or fire your code using one of those events? It seems a whole lot harder to get Windows to figure that out than to use Word’s own events, if they work.

          (On the one hand, I want to say we don’t know enough about your application to help you leverage the Word object model. On the other hand, what I’ve seen so far is sufficiently scary that I’m reluctant to ask. laugh )

      • #905555

        Thanks guys. This is somewhat interesting. I am able to get a valid processID and process handle for Word (see code below), but when I call
        WaitForInputIdle on the Word handle, it gives me an error 6: ERROR_INVALID_HANDLE. I have done this type of thing successfully
        before, but in that case I actually create the Word process via CreateProcess. However, in this case, I’m just retrieving
        the handle from a Word process that’s already out there. I wonder if I’m being denied SYNCHRONIZE access (although you would think I would get this
        error when I open the process handle).

        BTW, the reason I’m doing this is that my app requires the handle to the Word editing window (class “_WwG”). 999 times out
        of 1000, I successfully retrieve it. But occasionally, it doesn’t seem to be there; I’m thinking that, at the point that I
        look for the window, Word hasn’t finished initializing the document I’m looking at. So I thought:

        1. Put a WaitForInputIdle in another process
        2. When I fall through the wait, signal an event
        3. When I see that the event is signaled, THEN look for the “_WwG” window

        Any advice would be a big help. Thanks…Dan

        m_hProcess = OpenProcess(SYNCHRONIZE, 0, GetWordProcessID)
        If (m_hProcess INVALID_HANDLE_VALUE) Then
        rc = WaitForInputIdle(m_hProcess, 5)
        dwError = Err.LastDllError
        End If
        If (rc = WAIT_TIMEOUT) Then
        MsgBox “Wait timeout”
        ElseIf (rc = -1) Then
        MsgBox “Error ” & dwError
        Else
        MsgBox “Wait fell through”
        End If

    • #905363

      This is not strictly a Word question. I don’t know why GetCurrentProcess fails, but try the following:

      Private Declare Function GetWindowThreadProcessId Lib “user32” ( _
      ByVal hwnd As Long, _
      ByRef lpdwProcessId As Long) As Long

      Private Declare Function FindWindow Lib “user32” Alias “FindWindowA” ( _
      ByVal lpClassName As String, _
      ByVal lpWindowName As String) As Long

      Function GetWordProcessID() As Long
      Dim lngWindowHandle As Long
      Dim lngProcessID As Long
      Dim lngThreadID As Long
      lngWindowHandle = FindWindow(“OpusApp”, vbNullString)
      lngThreadID = GetWindowThreadProcessId(lWindowHandle, lngProcessID)
      GetWordProcessID = lngProcessID
      End Function

    • #905441

      I’ve never tried this function, but the applicable MSDN page seems to say that it returns a “pseudo handle” which might only be useful in the context in which it was called. You could try GetCurrentProcessId instead. This seems to work, comparing with the number returned by the Processes tab of the Task Manager for WINWORD.EXE:

      Private Declare Function GetCurrentProcessId Lib “kernel32” () As Long
      Sub GetMyPID()
      MsgBox GetCurrentProcessId
      End Sub

    • #905442

      I’ve never tried this function, but the applicable MSDN page seems to say that it returns a “pseudo handle” which might only be useful in the context in which it was called. You could try GetCurrentProcessId instead. This seems to work, comparing with the number returned by the Processes tab of the Task Manager for WINWORD.EXE:

      Private Declare Function GetCurrentProcessId Lib “kernel32” () As Long
      Sub GetMyPID()
      MsgBox GetCurrentProcessId
      End Sub

    Viewing 3 reply threads
    Reply To: Calling GetCurrentProcess in Word VBA Module (2k,XP)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: