The FindWindow function returns a handle to a window. A handle doesn’t do you much good on its own, but you need it for some other API functions, so it’s pretty important.
You declare API functions in the declarations section (read: top) of a standard module. The FindWindow declaration looks like this:
Public Declare Function FindWindow Lib “user32? Alias “FindWindowA” _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
This is an example of how to use the function in code. All it does is print out the handle number.
Dim lHwnd As Long
lHwnd = FindWindow(“wndClass_desked_gsk”, vbNullString)
The class name “wndClass_desked_gsk” I got from a program called Spy++ that ships with (at least) Visual Studio 6.0. If I didn’t have Spy++, I don’t know how I would find class names to pass to this function, but maybe someone who does know can comment. The class for the Excel window is XLMAIN.
For the second argument, I passed a null string. Since I know there’s only one VBE open, I can get away with the null string which tells the function to return the first window for that class. I could also sepecify that argument by using Application.VBE.MainWindow.Caption. Similarly, if you’re using XLMAIN as your class, you can use Application.Caption as the second argument.