The VB
AppActivate statement falls hopelessly
short of being truly useful since the
window title of an application can change
without notice. A much more reliable
means of activating another application
is by using the window class name. It's
much more reliable because the class name
of a window will not change once the
application has been installed.
So, you ask, how do I get the class name
of another application? You can find the
window class name for any application by
running a few lines of code. This is a
one-time-only procedure.
Private
Declare Function FindWindow Lib
"user32" Alias
"FindWindowA" (ByVal
lpClassName As String, ByVal lpWindowName
As String) 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 Function ShowWindow Lib
"user32" (ByVal hwnd As Long,
ByVal nCmdShow As Long) As Long
Const SW_SHOWNORMAL = 1
Const gcClassnameMSWord =
"OpusApp"
Const gcClassnameMSExcel =
"XLMAIN"
Const gcClassnameMSIExplorer =
"IEFrame"
Const gcClassnameMSVBasic =
"wndclass_desked_gsk"
Const gcClassnameNotePad =
"Notepad"
Const gcClassnameMyVBApp =
"ThunderForm"
Public Sub GetClassNameFromTitle()
Dim sInput As String, hWnd As Long,
lpClassName As String
Dim nMaxCount As Long, lresult As Long
nMaxCount = 256
lpClassName = Space(nMaxCount)
sInput = InputBox("Enter the exact
window title:" + Chr$(13) + Chr$(10)
+ "Note: must be an exact
match")
hWnd = FindWindow(vbNullString, sInput)
If hWnd = 0 Then
MsgBox "Couldn't find the
window."
Else
lresult = GetClassName(hWnd, lpClassName,
nMaxCount)
MsgBox "Window: " + sInput +
Chr$(13) + Chr$(10) + "Classname:
" + Left$(lpClassName, lresult)
End If
End Sub
Run
GetClassNameFromTitle and, if you enter
the window title correctly, the class
name will be shown in a messagebox.
Once you have the window class name, you
can always get a window handle by using
FindWindow with the class name and
vbNullString. For example:
hWnd =
FindWindow("OpusApp",
vbNullString)
...will find
the hWnd for the Microsoft Word window.
You can now activate the Word window with
this function:
Public Function
fActivateWindowClass(psClassname As
String) As Boolean
Dim hwnd As Long
hwnd = FindWindow(psClassname,
vbNullString)
If hwnd > 0 Then
ShowWindow hwnd, SW_SHOWNORMAL
fActivateWindowClass = True
End If
End Function
|