How to: Use Visual C # .NET to get the Window Handle of Office Automation Server (from MSDN)

xiaoxiao2021-03-06  77

SUMMARY This article describes how to get the Window handle of the Microsoft Office application from Microsoft Visual C # .NET and simultaneously run the application automatically.

MORE INFORMATION Object Models of Most Microsoft Office applications do not disclose properties for retrieving application window handles. To determine the window handle of the Office application you want to automatically run, you can use

The FindWindow function and the class name of the top-level window of the application as a parameter. If the application can run multiple instances at the same time, you may need to take into account this to retrieve the correct window handle. The following sections describe how to retrieve window handles for single instances and multi-instance applications.

Note: The Microsoft Access object model is open

Application object

The HWndAccessApp property is used to determine the window handle of the application. You can also use Forms and Reports

HWND properties to retrieve these specific windows handles. In addition, Microsoft Excel 2002 is the first for it.

Application object introduction

EXCEL version of the hwnd property. For Excel 2002 and Access 97 and its respective versions, since these Office applications provide a method of retrieving the application window handle by their respective object models, this article is not required to be discussed in this article.

FindWindow method.

Find a window handle of a single instance application The following steps describes how to use it in a Visual C # automation client

The FindWindow function determines that there is only a single instance of the process of automated the window handle of the server. This is the skills used when using PowerPoint as an automated server.

Step-by-step example

Start Microsoft Visual Studio .NET. On the File menu, click New, and then click Project. Select a Windows application from the Visual C # project type. FORM1 is created by default. Add a reference to the Microsoft PowerPoint object library. To do this, follow these steps:

On the project menu, click Add Reference. On the COM tab, find the Microsoft PowerPoint Object Library, and then click Select. Note: Microsoft Office 2003 contains the main intero set (PIA). Microsoft Office XP does not contain PIA, but you can download PIA. For additional information about Office XP PIA, click the article number below to view the article in the Microsoft Knowledge Base: 328912 Info: Microsoft Office XP PIA is available for download in the Add Reference dialog box Click OK to accept your choice . On the View menu, select the Toolbox to display the toolbox, then add a button to the form. Press the F7 key to see the FORM1 code window. Add the following instructions to the list of USING instructions defined at the top of the code window: use system.Runtime.InterOpServices;

Using powerpoint = microsoft.Office.Interop.powerpoint;

Add the following code to the command definition: public class myapi

{

[DLLIMPORT ("User32.dll")]]]]]

Public Static Extern Int FindWindow (String strwindown);

}

Add the following code to Button1 Click event: 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 of the Microsoft PowerPoint 2002 application window. If you want to automatically run Microsoft Office PowerPoint 2003, change the name in the code to "PP11Frameclass". Press the F5 key and click Button1 to run the example.

Find a window handle of an application that can have multiple instances (such as Microsoft Excel or Microsoft Word) can have multiple simultaneous instances. To retrieve the handle of the application instance you want to automatically run, you can first use "Automation" to change the title of the application to a unique value, then use

The FindWindow API function retrieves its window handle. The following steps use Excel as an automation server to illustrate this tip.

Step-by-step example

Start Microsoft Visual Studio .NET. On the File menu, click New, and then click Project. Select a Windows application from the Visual C # project type. FORM1 is created by default. Add a reference to the Microsoft Excel object library. To do this, follow these steps:

On the project menu, click Add Reference. On the COM tab, find the Microsoft Excel object library, then click Select. Note: Microsoft Office 2003 contains the main intero set (PIA). Microsoft Office XP does not contain PIA, but you can download PIA. For additional information about Office XP PIA, click the article number below to view the article in the Microsoft Knowledge Base: 328912 Info: Microsoft Office XP PIA is available for download in the Add Reference dialog box Click OK to accept your choice . On the View menu, select the Toolbox to display the toolbox, then add a button to the form. Press the F7 key to see the FORM1 code window. Add the following instructions to the list of USING instructions defined at the top of the code window: use system.Runtime.InterOpServices;

Using Excel = Microsoft.Office.Interop.Excel;

Add the following code to the command definition: public class myapi

{

[DLLIMPORT ("User32.dll")]]]]]

Public Static Extern Int FindWindow (String strwindown);

}

Add the following code to Button1 Click event: 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 ();

Press the F5 key and click Button1 to run the example.

Refer to more information, visit the following Microsoft Developer Network (MSDN) Web site:

Microsoft Office Development with Visual Studio (Microsoft Office with Visual Studio development) http://msdn.microsoft.com/library/en-us/dnoffdev/html/vsofficedev.asp window to retrieve information about using Visual Basic.NET Office Automation server Other information from the handle, click the article number below to see the article in the Microsoft Knowledge Base:

302281 HOWTO: Using VB.NET to get the Window handle of Office Automation Server

The information in this article applies to:

Microsoft Visual C # .NET (2003) Microsoft Visual C # .NET (2002) Microsoft Office Access 2003 Microsoft Access 2002 Microsoft Office Excel 2003 Microsoft Excel 2002 Microsoft Office PowerPoint 2003 Microsoft PowerPoint 2002 Microsoft Office Word 2003 Microsoft Word 2002

Recent Updated: 2004-2-13 (6.0) Keywords: kbpia kbautomation Kbhowto KB302295

转载请注明原文地址:https://www.9cbs.com/read-109056.html

New Post(0)