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
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » 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
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
(Edited by jscher2000 on 26-Nov-04 18:59. )
I’m with Hans.
According to the documentation, the 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. )
[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.
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
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?
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?
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
[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.
(Edited by jscher2000 on 26-Nov-04 18:59. )
I’m with Hans.
According to the documentation, the 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. )
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
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
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
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
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.
Notifications