Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

hWnd of the Word 97 document windows?

Posted on 2002-06-27
4
Medium Priority
?
769 Views
Last Modified: 2012-08-14
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
Comment
Question by:schworak
  • 2
4 Comments
 
LVL 44

Accepted Solution

by:
bruintje earned 400 total points
ID: 7115590
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
 
LVL 2

Expert Comment

by:egsemsem
ID: 7116470
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
 
LVL 3

Author Comment

by:schworak
ID: 7117639
Wish I could make it an A+ because that is exactly what I was looking for!
0
 
LVL 44

Expert Comment

by:bruintje
ID: 7117871
thanks for the grade, and glad i could help
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question