Article ID: 168751
Article Last Modified on 10/11/2006
boolVariable = GetWordWindow(strTitleStart, hwnd)where:
'FORM CODE ***********************************************
Option Explicit
Private Sub Form_Click()
Dim hwnd As Long
Select Case getWordWindow("", hwnd) 'call getWordWindow
Case True 'Word is running . . .
MsgBox "I found MS Word and its handle is " & CStr(hwnd) & "."
Case Else 'Word is not running
MsgBox "I did not find MS Word."
End Select
End Sub
'MODULE CODE ***********************************************
Option Explicit
Option Compare Text
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) _
As Long
Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, _
ByVal wCmd As Long) As Long
Declare Function GetTopWindow Lib "user32" (ByVal hwnd As Long) _
As Long
Public Const GW_HWNDNEXT = 2
Function getWordWindow(appTitle As String, appHandle As Long) _
As Boolean
Dim dummyVariable As Long
Dim lenTitle As Integer
Dim winTitle As String * 256
'initialize the function return as False
getWordWindow = 0
If appTitle = "" Then appTitle = "Microsoft Word"
lenTitle = Len(appTitle)
'Get the handle of the first child of the desktop window
appHandle = GetTopWindow(0)
'Loop through all top-level windows and search for the sub-string
'in the Window title
Do Until appHandle = 0
dummyVariable = GetWindowText(appHandle, winTitle, 255)
If Left(winTitle, lenTitle) = appTitle Then
getWordWindow = -1
Exit Function
Else
appHandle = GetWindow(appHandle, GW_HWNDNEXT)
End If
Loop
End Function
72918 How VB Can Determine if a Specific Windows Program is running
147659 How to Get a Window Handle Without Specifying an Exact Title
Additional query words: GetWindowText;GetWindow;GetTopWindow
Keywords: kbhowto kbprogramming kbwndw KB168751