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 by using Visual Basic .NET


View products that this article applies to.

Summary

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

↑ Back to the top


More information

The object models for most 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 function with the class name of the topmost window for the application. If the application can have multiple instances running at the same time, 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. Regarding Excel 2002 and later and Microsoft Access 97 and later, the FindWindow approach that is discussed in this article is not necessary because these Office applications can retrieve the window handle for the application through their respective object models.

Find the window handle for an application that is a single instance

The following steps illustrate how you can use the FindWindow 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 that you employ when you use Microsoft PowerPoint as your Automation server.

Step-by-step example

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, click New, and then click Project. Select Windows Application from the list of Visual Basic Projects types. Form1 is created by default.
  3. Add a reference to the Microsoft PowerPoint Object Library. To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. On the COM tab, locate the Microsoft PowerPoint Object Library, and then click Select.

      Note Microsoft Office 2003 includes Primary Interop Assemblies (PIAs). Microsoft Office XP does not include PIAs, but they may be downloaded. For more information about Office XP PIAs, click the following article number to view the article in the Microsoft Knowledge Base:
      328912 Microsoft Office XP primary interop assemblies (PIAs) are available for download
    3. Click OK in the Add References dialog box to accept your selections. If you are prompted to generate wrappers for the libraries that you selected, click Yes.
  4. On the View menu, select Toolbox to display the Toolbox, and then add a button to the form.
  5. Press F7 to view the code window for Form1.
  6. Paste the following code at the beginning of the code window:
    Imports System.Runtime.InteropServices
    Imports Microsoft.Office.Interop
    
    Public Class MyApi
        <DllImport("user32.dll")> Public Shared Function _
             FindWindow(ByVal strClassName As String, ByVal strWindowName _
                 As String) As Integer
        End Function
    End Class 
    					
  7. Add the following code to the Click event of Button1:
    Dim pptApp As PowerPoint.Application
    Dim hwndPPt As Integer
    
    pptApp = New PowerPoint.Application()
    pptApp.Visible = True
    
    hwndPPt = MyApi.FindWindow("PP10FrameClass", Nothing)
    
    MsgBox("hwndPPt (" & Hex(hwndPPt) & ") has the Window handle to " & _
          "Powerpoint's Main Window." & vbCr &  " Click OK to close PowerPoint.")
    
    pptApp.Quit()
    					

    NotePP10FrameClass is the class name for the Microsoft PowerPoint 2002 application window. If you automate PowerPoint 2003, change the class name in the code to PP11FrameClass.
  8. Press F5 and click Button1 to run the sample.

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 function to retrieve its window handle. The following steps illustrate this technique by using Excel as the Automation server.

Step-by-step example

  1. Start Visual Studio .NET.
  2. On the File menu, click New, and then click Project. Select Windows Application from the Visual Basic Projects types. Form1 is created by default.
  3. Add a reference to the Microsoft Excel Object Library. To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. On the COM tab, locate the Microsoft Excel Object Library, and then click Select.

      Note Microsoft Office 2003 includes Primary Interop Assemblies (PIAs). Microsoft Office 2002 does not include PIAs, but they may be downloaded. For more information about Office XP PIAs, click the following article number to view the article in the Microsoft Knowledge Base:
      328912 Microsoft Office XP primary interop assemblies (PIAs) are available for download
    3. Click OK in the Add References dialog box to accept your selections. If you are prompted to generate wrappers for the libraries that you selected, click Yes.
  4. On the View menu, select Toolbox to display the Toolbox, and then add a button to the form.
  5. Press F7 to view the code window for Form1.
  6. Paste the following code at the beginning of the code window:
    Imports System.Runtime.InteropServices
    Imports Microsoft.Office.Interop
    
    Public Class MyApi
        <DllImport("user32.dll")> Public Shared Function _
             FindWindow(ByVal strClassName As String, ByVal strWindowName _
                 As String) As Integer
        End Function
    End Class 
    					
  7. Add the following code to the Click event of Button1:
    Dim xlapp As Excel.Application
    Dim hwndExcel As Integer
    
    xlapp = New Excel.Application()
    xlapp.Visible = True
    xlapp.Caption = "Some Window Caption"
    
    hwndExcel = MyApi.FindWindow("XLMAIN", xlapp.Caption)
    
    xlapp.Caption = Nothing
    
    MsgBox("hwndExcel (" & Hex(hwndExcel) & ") has the Window handle to " & _
           "Excel's Main Window." & vbCr &  " Click OK to close Excel." )
    
    xlapp.Quit()
    					
  8. Press F5 and click Button1 to run the sample.

↑ Back to the top


References

For more information, visit the following Microsoft Developer Network (MSDN) Web site:
Microsoft Office Development with Visual Studio
http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx
For more information about retrieving the window handle for an Office Automation server by using earlier versions of Visual Basic, click the following article number to view the article in the Microsoft Knowledge Base:
258511 How to obtain the window handle for an Office Automation server

↑ Back to the top


Keywords: KB302281, kbhowto, kbautomation

↑ Back to the top

Article Info
Article ID : 302281
Revision : 12
Created on : 3/29/2007
Published on : 3/29/2007
Exists online : False
Views : 561