HOWTO: Check For a Running Instance of Word 95 From VB

ID: Q168751

The information in this article applies to:

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:

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:

1. Open a new Project. Form1 is created by default.

2. From the Project menu (Insert menu in Visual Basic 4), insert a new

   Module (Module1).

3. Copy the Form Code below to the General Declarations section of Form1.

4. 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).

5. 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

Additional query words: GetWindowText;GetWindow;GetTopWindow
Keywords          : kbprg kbVBp400 kbVBp500 kbWndw kbhowto IAPThird VB4WIN vbwin 
Version           : WINDOWS:4.0 5.0 7.0 7.0a
Platform          : NT Win95 WINDOWS
Issue type        : kbhowto

Last Reviewed: October 3, 1997