Results 1 to 12 of 12
  1. #1
    2 Star Lounger
    Join Date
    Sep 2002
    Posts
    180
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

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

    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

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Calling GetCurrentProcess in Word VBA Module (

    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

  4. #4
    2 Star Lounger
    Join Date
    Sep 2002
    Posts
    180
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

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

    Sorry, this is far over my head.

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Calling GetCurrentProcess in Word VBA Module (

    <P ID="edit" class=small>(Edited by jscher2000 on 26-Nov-04 18:59. )</P>I'm with Hans. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    According to the documentation, the <img src=/w3timages/censored.gif alt=censored border=0> 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. <img src=/S/laugh.gif border=0 alt=laugh width=15 height=15> )

  7. #7
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calling GetCurrentProcess in Word VBA Module (

    <hr>the NewDocument and DocumentOpen application-level events fire "after" a document is created or opened<hr>
    This was indeed the case in Word 97, but is not entirely true in Word 2003. See my <post#=408195>post 408195</post#>.

  8. #8
    2 Star Lounger
    Join Date
    Sep 2002
    Posts
    180
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calling GetCurrentProcess in Word VBA Module (

    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

  9. #9
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Calling GetCurrentProcess in Word VBA Module (

    You probably can test that by trying to retrieve those window names from inside a NewDocument event handler. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  10. #10
    2 Star Lounger
    Join Date
    Sep 2002
    Posts
    180
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calling GetCurrentProcess in Word VBA Module (

    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?

  11. #11
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calling GetCurrentProcess in Word VBA Module (

    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.

  12. #12
    2 Star Lounger
    Join Date
    Sep 2002
    Posts
    180
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calling GetCurrentProcess in Word VBA Module (

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

Posting Permissions

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