How do I get the hwnd of Microsoft Word?

N

Neil Penn

How can I get the hwnd of the main Word application window? I've searched
the object model and can't find anything.
 
H

Helmut Weber

Hi Neil,
I got that hint just yersterday by Christian Freßdorf
of the German MVPs.

Private Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function SetForegroundWindow Lib "user32" _
(ByVal hwnd As Long) As Long
Private Declare Function SetActiveWindow Lib "user32.dll" _
(ByVal hwnd As Long) As Long

Private Sub GetWordHandle()
Dim lHandle As Long
Dim retval As Long ' return value
' find window with the given document name
lHandle = FindWindow(vbNullString, ActiveDocument.Name & " - " _
& Application.Name)
'Set this window to the foreground
'lHandle = SetForegroundWindow(lHandle)
or
retval = SetActiveWindow(lHandle)
' set lHandle as the application's active window
'If the function succeeds,
'the return value is the handle to the window
'that was previously active.
'If the function fails, the return value is NULL.
'To get extended error information, call GetLastError.
Debug.Print retval ' !!!
End Sub

Greetings from Bavaria, Germany
Helmut Weber
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 2000
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top