Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How to obtain the window handle for an Office Automation server


View products that this article applies to.

Summary

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

↑ Back to the top


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
        ' Replace the frame_class place holder with one of the following:
        ' PP12FrameClass for PowerPoint 2007
        ' PP11FrameClass for PowerPoint 2003
        ' PP10FrameClass for PowerPoint 2002
        ' PP9FrameClass for PowerPoint 2000
        ' PP97FrameClass for PowerPoint 97
        hWndPpt = FindWindow("frame_class", 0)  
        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.

↑ Back to the top


References

For more 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

↑ Back to the top


Keywords: kbexpertiseinter, kbautomation, kbhowto, KB258511

↑ Back to the top

Article Info
Article ID : 258511
Revision : 9
Created on : 4/17/2007
Published on : 4/17/2007
Exists online : False
Views : 719