Microsoft KB Archive/307473

= How to use a type library for Office Automation from Visual C++ .NET =

Article ID: 307473

Article Last Modified on 4/18/2007

-

APPLIES TO


 * Microsoft Visual C++ .NET 2002 Standard Edition
 * Microsoft Foundation Class Library 4.2
 * Microsoft Office Professional 2007
 * Microsoft Office Professional Edition 2003
 * Microsoft Office XP Developer Edition
 * Microsoft Office 2000 Developer Edition
 * Microsoft Office 97 Developer Edition

-



This article was previously published under Q307473





SUMMARY
This article describes how to build a Visual C++ .NET project that acts as an Automation client for applications that are Component Object Model (COM) compliant. The sample in this article uses Microsoft Foundation Classes (MFC) with class wrappers for an Office component.



MORE INFORMATION
The following steps demonstrate how to build a simple Automation client. There are three main steps to this procedure:
 * 1) Create the Automation client.
 * 2) Add code to automate Microsoft Excel.
 * 3) Run the Automation client.

You can use the first step, Create the Automation Client, to create a new Automation client regardless of the Automation server that you are using. The second step, Add Code to Automate Microsoft Excel, is specific to the Automation server.

Create an Automation client
 Start Microsoft Visual Studio .NET. On the File menu, click New, and then click Project. Select MFC Application from the Visual C++ Projects types, and then name the project AutoProject. When the MFC Application Wizard appears, click Application Type, set the application type to Dialog Based, and then click Finish. Modify the IDD_AUTOPROJECT_DIALOG dialog box as follows:  Remove the Label control (IDC_STATIC) and the Cancel button (IDCANCEL). Change the ID of the OK button to &quot;IDRUN&quot; and the caption to &quot;Run.&quot; </li> Create class wrappers from the type library for the Automation server as follows:  In Solution Explorer, right-click AutoProject, and then click Add Class.</li> Click MFC Class from Typelib, and then click Open.</li> Click Add a class from: Registry, and locate the registered type library for your Automation server.

For this example, select &quot;Microsoft Excel 10.0 Type Library&quot; for Microsoft ExcelXP or &quot;Microsoft Excel 9.0 Type Library&quot; for Microsoft Excel 2000.</li> Select the interface(s) that you need from the list, and then click the greater than symbol (>) to add them to the list of interfaces for which MFC creates wrappers. Click Finish when you have added all of the interfaces.

For this example, you only need the _Application interface.

Important If there are many interfaces in the type library that you chose, select only those interfaces that you will use because MFC will generate a separate header file for each selected interface. By minimizing your interface selections, you can avoid unnecessary overhead during file generation and compilation.</li></ol> </li>  To load and enable the COM services library to the CAutoProjectApp::InitInstance function, add the following code: if(!AfxOleInit) // Your addition starts here. { AfxMessageBox(&quot;Cannot initialize COM dll&quot;); return FALSE; // End of your addition. }

AfxEnableControlContainer; </li>  Add an include directive for each header file that is generated from the interfaces in the type library of the Automation server. Add the directives after the include statement for Stdafx.h at the top of AutoProjectDlg.cpp.

For this example, add an include for the CApplication.h header file, which is generated for the _Application interface: #include &quot;stdafx.h&quot; #include &quot;CApplication.h&quot; </li></ol>

back to the top

Add code to automate Microsoft Excel
In the IDD_AUTOPROJECT_DIALOG dialog box, right-click Run, and then click Add event handler from the drop-down list box. In the Event Handler Wizard, select the BN_CLICKED message type, and then click Add and Edit. Add the following code to automate Excel in the handler: void CAutoProjectDlg::OnBnClickedRun {  CApplication app;  // app is the Excel _Application object

// Start Excel and get Application object.

if(!app.CreateDispatch(&quot;Excel.Application&quot;)) {     AfxMessageBox(&quot;Cannot start Excel and get Application object.&quot;); return; }  else {     //Make the application visible and give the user control of      //Microsoft Excel. app.put_Visible(TRUE); app.put_UserControl(TRUE); } } back to the top

Run the Automation client
Press the F5 key to build and run the Automation client. When the dialog box appears, click Run. The Automation client starts Excel and makes the application visible. Notice that Excel remains running even when the Automation client ends because the user has been given control of the application.

back to the top

Additional notes
After you have added classes from a type library to your project, you may notice that they appear in the Class View of your project. In Class View, you can double-click a class to see the methods of that class, and then double-click the method to view the definition of that function in the implementation file of the MFC wrapper. You can review the definition of a member function if you want to verify a return type or if you must change an implementation of a function.

Although the earlier steps illustrate how to automate Microsoft Excel, you can apply the same ideas to automate other applications. The following list contains the file names for type libraries of other Microsoft Office applications:

Note The default location for these type libraries is:

The default location for Dao350.dll and Dao360.dll is C:\Program Files\Common Files\Microsoft Shared\Dao.

<div class="references_section">