How To Obtain the Window Handle for an Office Automation Server (258511)



The information in this article applies to:

  • Microsoft Office Word 2003
  • Microsoft Word 2002
  • Microsoft Word 2000
  • Microsoft Word 97 for Windows
  • Microsoft Office Excel 2003
  • Microsoft Excel 2002
  • Microsoft Excel 2000
  • Microsoft Excel 97 for Windows
  • Microsoft Access 2002
  • Microsoft Access 2000
  • Microsoft Access 97
  • Microsoft Office PowerPoint 2003
  • Microsoft PowerPoint 2002
  • Microsoft PowerPoint 2000
  • Microsoft PowerPoint 97 for Windows
  • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual Basic Professional Edition for Windows 5.0

This article was previously published under Q258511

SUMMARY

This article demonstrates how to obtain the window handle of an Microsoft Office application while automating that application from Visual Basic.

MORE INFORMATION

The object models for most Microsoft Office applications do not expose properties for retrieving the application window handles. To determine the window handle of an Office application that you are automating, you can use the FindWindow API function with the class name of the top-most window for the application. If the application can have multiple instances running at the same time, then you may need to account for this so that you retrieve the correct window handle. The following sections illustrate techniques for retrieving the window handle for both single and multiple instance applications.

NOTE: The Microsoft Access object model exposes the hWndAccessApp property for the Application object for determining the window handle of the application. You can also use the hWnd property for Forms and Reports to retrieve handles to those specific windows. Additionally, Microsoft Excel 2002 is the first version of Excel to introduce an hWnd property for its Application object. With respect to Microsoft Excel 2002 and Microsoft Access versions 97 and later, because these Office applications provide a means through their respective object models to retrieve the window handle for the application, the FindWindow approach discussed in this article is not necessary.

Find the Window Handle for an Application That Is Single Instance

The following steps illustrate how you can use the FindWindow API function with a Visual Basic Automation client to determine the window handle for an out-of-process Automation server that can have only a single instance. This is the technique you would employ when using Microsoft PowerPoint as your Automation server.

Step-by-Step Example
  1. Start a new Standard EXE project in Visual Basic. Form1 is created by default.
  2. Add a command button, Command1, to Form1.
  3. Copy the following code to the Code window of the Form1 form:
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _ 
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
       
    Private Sub Command1_Click()
        Dim PptApp As Object
        Set PptApp = CreateObject("PowerPoint.Application")
        PptApp.Visible = True
        Dim hWndPpt As Long
        hWndPpt = FindWindow("PP9FrameClass", 0)  'PP97FrameClass w/ PowerPoint 97
        MsgBox "hWndPpt ( " & Hex(hWndPpt) & " ) contains the Window Handle " & _
               "of the PowerPoint application created by this program." & vbCr & _
               "You can use this Window Handle in various Win 32 APIs, such " & _
               "as SetForeGroundWindow," & vbCr & _
               "which require a Window Handle parameter to be supplied." & vbCr & _
               vbCr & "All Done.  Click OK to close PowerPoint.", vbMsgBoxSetForeground
            PptApp.Quit
        Set PptApp = Nothing
    End Sub
    					
  4. Press the F5 key to run the program. Click Command1. PowerPoint starts and then a message box appears indicating the window handle for PowerPoint. Click OK to dismiss the message box and quit PowerPoint.

Find the Window Handle for an Application That Can Have Multiple Instances

Some applications, such as Microsoft Excel or Microsoft Word, can have multiple instances running at the same time. To retrieve the handle to the application instance that you are automating, you can first use Automation to change the title of the application to a unique value and then use the FindWindow API function to retrieve its window handle. The following steps illustrate this technique by using Microsoft Excel as the Automation server.

Step-by-Step Example
  1. Start a new Standard EXE project in Visual Basic. Form1 is created by default.
  2. Add a command button, Command1, to Form1.
  3. Copy the following code to the Code window of the Form1 form:
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _ 
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long 
       
    Private Sub Command1_Click()
        Dim xlApp As Object
        Set xlApp = CreateObject("Excel.Application")
        xlApp.Caption = "New Caption Supplied by Program"
        Dim hWndXl As Long
        hWndXl = FindWindow("XLMAIN", xlApp.Caption)
        xlApp.Caption = Empty 'Set the original caption back
    
        xlApp.Visible = True
        MsgBox "hWndXl ( " & Hex(hWndXl) & " ) contains the Window Handle " & _
               "of the Excel application created by this program." & vbCr & _
               "You can use this Window Handle in various Win 32 APIs, " & _
               "such as SetForeGroundWindow," & vbCr & _
               "which require a Window Handle parameter to be supplied." & vbCr _
               & vbCr & "All Done.  Click OK to close Excel.", vbMsgBoxSetForeground
        xlApp.Quit
        Set xlApp = Nothing
    End Sub
    					
  4. Press F5 to run the program. Click Command1. Excel starts and then a message box appears indicating the window handle for Excel. Click OK to dismiss the message box and quit Excel.

REFERENCES

For additional information, click the following article numbers to view the articles in the Microsoft Knowledge Base:

112649 How To Get a Window's Class Name and Other Attributes

147659 How To Get a Window Handle Without Specifying an Exact Title

242308 How To Find a Window Handle from an Instance Handle

183009 How To Enumerate Windows Using the WIN32 API


Modification Type:MajorLast Reviewed:3/23/2006
Keywords:kbAutomation kbhowto KB258511