Solved

hWnd of the Word 97 document windows?

Posted on 2002-06-27
4
758 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

740 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