Solved

hWnd of the Word 97 document windows?

Posted on 2002-06-27
4
749 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 100 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now