Microsoft KB Archive/258511

= How to obtain the window handle for an Office Automation server =

Article ID: 258511

Article Last Modified on 4/17/2007

-

APPLIES TO


 * Microsoft Office Access 2007
 * Microsoft Office Access 2003
 * Microsoft Access 2002 Standard Edition
 * Microsoft Access 2000 Standard Edition
 * Microsoft Access 97 Standard Edition
 * Microsoft Office Excel 2007
 * Microsoft Office Excel 2003
 * Microsoft Excel 2002 Standard Edition
 * Microsoft Excel 2000 Standard Edition
 * Microsoft Excel 97 Standard Edition
 * Microsoft Office PowerPoint 2007
 * Microsoft Office PowerPoint 2003
 * Microsoft PowerPoint 2002 Standard Edition
 * Microsoft PowerPoint 2000 Standard Edition
 * Microsoft PowerPoint 97 Standard Edition
 * Microsoft Office Word 2007
 * Microsoft Office Word 2003
 * Microsoft Word 2002 Standard Edition
 * Microsoft Word 2000 Standard Edition
 * Microsoft Word 97 Standard Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Professional Edition

-



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  Start a new Standard EXE project in Visual Basic. Form1 is created by default. Add a command button, Command1, to Form1.  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  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  Start a new Standard EXE project in Visual Basic. Form1 is created by default. Add a command button, Command1, to Form1.</li>  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 </li> 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.</li></ol>

<div class="references_section">