The information in this article applies to:
- Microsoft Visual Basic Learning, Professional, and Enterprise Editions
for Windows, version 5.0
on the following platforms: NT, Win95
- Microsoft Visual Basic Standard, Professional, and Enterprise Editions
for Windows, version 4.0
on the following platforms: NT, Win95
- Microsoft Word for Windows 95, versions 7.0, 7.0a
SUMMARY
Microsoft Word 95 does not appear in the Windows running objects table and
does not use Visual Basic as its macro language. This makes Microsoft
Word 95 difficult to detect from a Visual Basic application. This article
illustrates a technique that involves looping through all Windows and
checking for the "Microsoft Word" sub-string at the beginning of the title
bar.
MORE INFORMATION
The recommended method for checking for an instance of a Microsoft Office
application is to execute GetObject and trap for error 429, which will be
returned if there is no running instance. Because Microsoft Word 95 has no
application object, this approach fails and the error 429 will always be
returned.
However, because Microsoft Word, exclusively, always begins its title bar
text with the "Microsoft Word" sub-string, the GetWindowText API function
can be applied to each running window to detect the presence of Microsoft
Word 95. The following code demonstrates this procedure and works whether
or not Microsoft Word is visible or has a document open. This technique can
also be applied to any application that uses a top-level window that starts
its title bar text with a known sub-string. The call syntax is demonstrated
below:
boolVariable = GetWordWindow(strTitleStart, hwnd)
where:
- boolVariable is set to true for success and false for failure
(no window found).
- GetWordWindow is the Visual Basic function that wraps the API function
GetWindowText.
- strTitleStart is the sub-string sought. If the title of any top level
window begins with this sub-string, the handle of the first such
instance will be returned. An empty string will be converted by the
function to "Microsoft Word." The function is not case-sensitive.
- hwnd is the window handle. The handle of the last top-level window is
returned in case of failure.
Step-by-Step Example
The project in this example runs as a form containing no controls. Clicking
on the form itself results in the display of a message box either stating,
"I did not find MS Word," indicating that Microsoft Word is not running, or
"I found MS Word and its handle is," and displaying the window handle. This
example works for all versions of Word for Windows:
- Open a new Project. Form1 is created by default.
- From the Project menu (Insert menu in Visual Basic 4), insert a new
Module (Module1).
- Copy the Form Code below to the General Declarations section of Form1.
- Copy the Module Code below to the General Declarations section of
Module1. Be sure each API Declare Function statement is entered
on a separate line (line-continuation underscore not allowed by
Visual Basic).
- Take care to remove a duplicate entry of the 'Option Explicit' line for
the case when your Tools/Option/Environment/Require Variable
Declaration CheckBox has been selected.
'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
REFERENCES
Win32 Programmer's Reference Volume 3 (Microsoft Press)
For additional information, please see the following articles in the
Microsoft Knowledge Base:
ARTICLE-ID: Q72918
TITLE : How VB Can Determine if a Specific Windows Program
Is Running
ARTICLE-ID: Q147659
TITLE : How to Get a Window Handle Without Specifying an Exact Title