Obtaining the handle to a VBA Userform
Introduction
Unlike VB forms VBA Userforms do not have a .hWnd property. However, being a window they do have a window handle and the many API functions available to adjust windows do work with Userforms too, it's just a little trickier to get the window handle in the first place.
FindWindow
In order to obtain the handle you need to use the API function FindWindow. The function should be declared as follows:
Declare Function FindWindow Lib"user32" Alias"FindWindowA" _ (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
As you can see the function takes 2 arguments. A string giving the name of the class of the window it needs to find and a string giving the caption of the window it needs to find.
Window Classes
Every window in windows has a class associated with it. The information in this class is what defines how the window looks and how it works. The window class of a UserForm is dependent on the version of Excel that you are using. For 2000 and upwards the class name is "ThunderDFrame" but before that the class name was "ThunderXFrame". In Excel the other class name you may find useful at times is "XLMAIN". This is the class name of the main application window. Other classes used in VBA programming can be found here.
Putting It All Together
To put it all together then the following piece of code will determine the handle of a userform for all versions of Excel:
Declare Function FindWindow Lib "user32" Alias "FindWindowA" _ (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Dim hWnd As Long 'check If we are using XL97 Or Not If Val(Application.Version) < 9 Then hWnd = FindWindow("ThunderXFrame", Me.Caption) Else hWnd = FindWindow("ThunderDFrame", Me.Caption) End If MsgBox hWnd