Microsoft Knowledge Base Email Alertz

KBAlertz.com: (302295) - This article demonstrates how to obtain the window handle of a Microsoft Office application while automating that application from Microsoft Visual C# .NET.

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 302295 - Last Review: January 31, 2007 - Revision: 7.4

How To Obtain the Window Handle for an Office Automation Server by Using Visual C# .NET

This 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 Instance

The 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
  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, click New, and then click Project. Select Windows Application from the Visual C# Project types. Form1 is created by default.
  3. Add a reference to Microsoft PowerPoint Object Library. To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. On the COM tab, locate 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 can be downloaded. For additional information about Office XP PIAs, click the following article number to view the article in the Microsoft Knowledge Base:
      328912  (http://kbalertz.com/Feedback.aspx?kbNumber=328912/ ) INFO: Microsoft Office XP PIAs Are Available for Download
    3. Click OK in the Add References dialog box to accept your selections.
  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. Add the following directive to the list of using directives defined at the top of code window:
    using System.Runtime.InteropServices;
    using PowerPoint = Microsoft.Office.Interop.PowerPoint;
    					
  7. Add the following code after the directive definitions:
    
    public class MyApi
    {
    	    [DllImport("user32.dll")] 
    	    public static extern int FindWindow(string strclassName, string strWindowName);
    };
    
    					
  8. Add the following code to the Click event of Button1:
    int hwndPPt;
    PowerPoint.Application pptApp;
    
    pptApp = new PowerPoint.Application();
    
    pptApp.Visible  = Microsoft.Office.Core.MsoTriState.msoTrue;
    
    hwndPPt = MyApi.FindWindow("PP10FrameClass",null);
    
    MessageBox.Show("hwndPPt (0x" + hwndPPt.ToString("x") + 
    	") has a Window handle to PowerPoint's main Window. " + "\r\n" + 
    	"Click OK to close Powerpoint" , "Handle for PowerPoint");
    
    pptApp.Quit();
    
    					
    Note "PP10FrameClass" is the class name for the Microsoft PowerPoint 2002 application window. If you are automating Microsoft Office PowerPoint 2003, change the class name in the code to "PP11FrameClass."
  9. 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 API 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 Microsoft Visual Studio .NET.
  2. On the File menu, click New, and then click Project. Select Windows Application from the Visual C# Project types. Form1 is created by default.
  3. Add a reference to Microsoft Excel Object Library. To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. On the COM tab, locate Microsoft Excel Object Library, and then click Select.

      Note Microsoft Office 2003 includes Primary Interop Assemblies (PIAs). Microsoft Office XP does not include PIAs, but they can be downloaded. For additional information about Office XP PIAs, click the following article number to view the article in the Microsoft Knowledge Base:
      328912  (http://kbalertz.com/Feedback.aspx?kbNumber=328912/ ) INFO: Microsoft Office XP PIAs Are Available for Download
    3. Click OK in the Add References dialog box to accept your selections.
  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. Add the following directive to the list of using directives defined at the top of code window:
    using System.Runtime.InteropServices;
    using Excel = Microsoft.Office.Interop.Excel;
    					
  7. Add the following code after the directive definitions:
    public class MyApi
    {
    	    [DllImport("user32.dll")] 
    	    public static extern int FindWindow(string strclassName, string strWindowName);
    	
    };
    					
  8. Add the following code to the Click event of Button1:
    int hwndExcel;
    
    Excel.Application  xlapp;
    
    xlapp = new Excel.Application();
    
    xlapp.Visible = true;
    
    xlapp.Caption = "Some Caption For Excel Window";
    
    hwndExcel = MyApi.FindWindow("XLMAIN",xlapp.Caption);
    
    xlapp.Caption = null;
    
    MessageBox.Show("hwndExcel (0x" + hwndExcel.ToString("x") + 
    	") has a Window handle to Excel's main Window." + "\r\n" +  
    	"Click OK to close Excel"  , "Handle for Excel");
    
    xlapp.Quit();
    					
  9. Press F5 and click Button1 to run the sample.

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 (http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx)
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:
302281  (http://kbalertz.com/Feedback.aspx?kbNumber=302281/ ) How To Obtain the Window Handle for an Office Automation Server Using VB.Net

APPLIES TO
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Office Access 2003
  • Microsoft Access 2002 Standard Edition
  • Microsoft Office Excel 2003
  • Microsoft Excel 2002 Standard Edition
  • Microsoft Office PowerPoint 2003
  • Microsoft PowerPoint 2002 Standard Edition
  • Microsoft Word 2002
Keywords: 
kbpia kbautomation kbhowto KB302295
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please