Results 1 to 4 of 4

Thread: How to get a window handle in VBA (Word)

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2000
    Posts
    2

    Angry

    I want to use the BrowseFolder tool like the one on this site in VBA for Word in order to pick a directory where a lot of scripts are located.
    These scripts will be placed in the Word document that's currently open.
    When I import the code from this tool into the VBE for Word, it nag's about a compile error: hWnd
    This handle is the first argument to be parsed to the module which browses for the directory.
    In VB 6.0 it works, in VBA for Word it won't.

    Who can help me?

  2. #2
    Lively Member
    Join Date
    May 2000
    Location
    Norway
    Posts
    112

    Just fount out...

    Here is a code to get the handle to both the Window and the control.

    Code:
    Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hwndParent As Long, ByVal hwndChildAfter As Long, ByVal lpszClass As Any, ByVal lpszWindow As Any) As Long
    Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
    
    Private Sub GetHwnd()
        Dim hwnd As Long
        Dim hWndCommandButton As Long
    
        hwnd = FindWindow(CLng(0), CStr(ActiveDocument.Parent.Caption))
        If hwnd <> 0 Then
            hWndCommandButton = FindWindowEx(hwnd, 0, CLng(0), "BUTTON_CAPTION_GOES_HERE")
        End If
    End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2000
    Posts
    2

    Unhappy

    I implemented the code you replied in the form in which the button is placed.
    Now VBA complains about the ByRef argumenttypes which do not agree.

    what is the error? If you want i can zip the code to you!

    thanks anyway so far!


  4. #4
    Lively Member
    Join Date
    May 2000
    Location
    Norway
    Posts
    112
    Where does it complain? The code is pretty simple.
    If it complains about byrefs and byvals try switching the byrefs with byvals.
    The code below was tested in Word97 and had no problems.

    Code:
    Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
    
    Function GetHwnd() As Long
        GetHwnd = FindWindow(CLng(0), CStr(ActiveDocument.Parent.Caption))
    End Function
    
    Sub Test()
      MsgBox GetHwnd
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width