How can I get the window handle (for use with APIs) of the current Word userform?
How can I get the window handle (for use with APIs) of the current Word userform?
Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
<img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

This month, every Windows Secrets subscriber can download a one-chapter excerpt of Windows 7: The Missing Manual.Windows 7: The Missing Manual provides valuable information to help you overcome these difficulties in learning a new operating system. Subscribe today to download your free excerpt.
This may be of help.
<pre>Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function LockWindowUpdate Lib "user32" _
(ByVal hWnd As Long) As Long
Public Function ThisHandle() As Long
Dim hWndXl As Long
' Necessary because most Office applications can handle multiple instances
Application.Caption = "New Caption Supplied by Program"
' Calling the below API gives the hWnd to ThisHandle (the function)
ThisHandle = FindWindow("OpusApp", Application.Caption)
' Set the original caption back
Application.Caption = Empty
End Function
Public Sub LockWindow(hWnd As Long, blnValue As Boolean)
'This function is called by the following lines:
' LockWindow ThisWindow True
' LockWindow ThisWindow False
If blnValue Then
' Locks the window
LockWindowUpdate hWnd
Else
' Unlocks the window
LockWindowUpdate 0
End If
End Sub
</pre>
Al, Thanks, but this does not give me the window handle of a Word userform. It give the App (Word) handle. Any other ideas?
Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
<img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>
Ok, how about....
<pre>FindWindowEx
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</pre>
Look where I was when your email hit:
<A target="_blank" HREF=http://www.vbapi.com/ref/f/findwindowex.html>http://www.vbapi.com/ref/f/findwindowex.html</A>
Thanks! I'm with you on this one...
Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
<img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>
Al,
I've got it using FindWindow(). Turns out it's not that complicated after all.
I had not realized that the classname (Parameter 1) can be nullstring and if so, the window search entails the current app's windows (my understandint -- maybe it means "seach ALL windows" -- heck, I can't understand these geeks who put these things together. Which brings up another issue --later). So, this does the trick
hWinChild = Findwindow(vbNullString, "My Form Name")
Voila!
I wanted the window handle in order to position the very cool SHBrowseForFolder dialog relative to the userform window, not the App window (Word). Success.
Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
<img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>
Glad that it worked out.
There is some important information in this thread. I would suspect that getting a form handle via VBA would be something that is needed now and again. Thanks for posting the "solution."
This was a fun thread. Especially since it all worked toward a solution (that was one line of CODE!). You should have seen my head pounding last night. I left work with a big ol' headache.
I think the Lounge folks really ought to consider a code library like so many other board/forums etc. have. Talk about one-stop shopping. I know seaching should get you what you need, but it's not clear we have any sort of code library available here.
Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
<img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>
Kevin.
Try this :-
Private Declare Function GetActiveWindow Lib "user32" () As Long
then
hWnd = GetActiveWindow()
Andrew C
And where were you during the heat of the battle.
Just letting me pound this one out, huh? Wise guy.
Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
<img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>