Article ID: 302295 - Last Review: January 31, 2007 - Revision: 7.5 How To Obtain the Window Handle for an Office Automation Server by Using Visual C# .NETThis article was previously published under Q302295 SUMMARY This article demonstrates how to obtain the window handle
of a Microsoft Office application while automating that application from Microsoft Visual
C# .NET. 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 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 for 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 Excel versions 2002 and later and 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 that is discussed in this article is not necessary. Find the Window Handle for an Application That Is Single InstanceThe following steps illustrate how you can use the FindWindow function with a Visual C# 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 employ when you use PowerPoint as your Automation server.Step-by-Step Example
Find the Window Handle for an Application That Can Have Multiple InstancesSome 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 Excel as the Automation server.Step-by-Step Example
REFERENCES For more information, visit the following Microsoft Developer
Network (MSDN) Web site:
Microsoft Office Development with Visual Studio
For additional information about retrieving the window handle for an Office Automation server
using Visual Basic.NET, click the following article number to view the article in the Microsoft Knowledge Base:
http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx (http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx) 302281
(http://support.microsoft.com/kb/302281/
)
How To Obtain the Window Handle
for an Office Automation Server Using VB.Net
APPLIES TO
| Article Translations
|
Back to the top
