Solved

hWnd of the Word 97 document windows?

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month4 days, 8 hours left to enroll

635 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