Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 779
  • Last Modified:

hWnd of the Word 97 document windows?

I am able to get the hWnd of the Word application window. But what I really need is the hwnd of the document window. This way I could make a custom form a child of the document instead of the application.

Anyone know how to get it?
0
schworak
Asked:
schworak
  • 2
1 Solution
 
bruintjeCommented:
Hi schworak,

this is a snippet from Japser Mandos

---------------------------------

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal
nCmdShow As Long) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA"
(ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal
lpsz2 As String) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal
wCmd As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA"
(ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long)
As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Const SW_HIDE = 0
Const SW_SHOWNORMAL = 1
Const SW_NORMAL = 1
Const SW_SHOWMINIMIZED = 2
Const SW_SHOWMAXIMIZED = 3
Const SW_MAXIMIZE = 3
Const SW_SHOWNOACTIVATE = 4
Const SW_SHOW = 5
Const SW_MINIMIZE = 6
Const SW_SHOWMINNOACTIVE = 7
Const SW_SHOWNA = 8
Const SW_RESTORE = 9
Const SW_SHOWDEFAULT = 10
Const SW_MAX = 10
Const SWP_HIDEWINDOW = &H80
Const SWP_SHOWWINDOW = &H40
Const GW_CHILD = 5
Const GW_HWNDNEXT = 2

Sub DemoShowHide()

Dim hWnd_Parent As Long
Dim hWnd_Child As Long
Dim hWnd_ChildChild As Long
Dim ClassName As String * 250
Dim Ret As Long
Dim i As Long

    'get the word windowhandle
    hWnd_Parent = FindWindow(vbNullString, Application.Caption &
vbNullString)

    'get it's first child
    hWnd_Child = GetWindow(hWnd_Parent, GW_CHILD)
    Do
        'get the classname of the child
        GetClassName hWnd_Child, ClassName, 250
        'check if its "_WwB". This is what spy++ tells me the classname
should be.
        If (Left(ClassName, 4)) = "_WwB" Then
            "Find if the activewindow is a child of this child
            hWnd_ChildChild = FindWindowEx(hWnd_Child, 0, vbNullString,
ActiveWindow.Caption & vbNullString)
            'if it is found, end the loop. (this is not really safe, so it's
better to built in some extra safety
            If hWnd_ChildChild <> 0 Then Exit Do
        End If
        'find the next window if it has not been found.
        hWnd_Child = GetWindow(hWnd_Child, GW_HWNDNEXT)
    Loop

    'demo loop:
    For i = 1 To 2
        ShowWindow hWnd_ChildChild, SW_HIDE
        Sleep 1000
        ShowWindow hWnd_ChildChild, SW_SHOW
        Sleep 1000
    Next i

End Sub
--------------------------

maybe a pointer in the right direction

HAGD:O)Bruintje
0
 
egsemsemCommented:
I think you will need to enumerate Word child windows, for example (WordHWnd is the handle of Word application window):


'in a form
Private Sub Form_Load()
    Me.AutoRedraw = True
    EnumChildWindows WordHWnd, AddressOf EnumChildProc, ByVal 0&
End Sub


'in a module
Declare Function GetDesktopWindow Lib "user32" () As Long
Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Public Function EnumChildProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
    Dim sSave As String
    'Get the windowtext length
    sSave = Space$(GetWindowTextLength(hwnd) + 1)
    'get the window text
    GetWindowText hwnd, sSave, Len(sSave)
    'remove the last Chr$(0)
    sSave = Left$(sSave, Len(sSave) - 1)
    If sSave <> "" Then Form1.Print sSave & " Handle: " & hwnd
    'continue enumeration
    EnumChildProc = 1
End Function
0
 
schworakAuthor Commented:
Wish I could make it an A+ because that is exactly what I was looking for!
0
 
bruintjeCommented:
thanks for the grade, and glad i could help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now