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


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

  1. 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.
  2. When the MFC Application Wizard appears, click Application Type, set the application type to Dialog Based, and then click Finish.
  3. Modify the IDD_AUTOPROJECT_DIALOG dialog box as follows:
    1. Remove the Label control (IDC_STATIC) and the Cancel button (IDCANCEL).
    2. Change the ID of the OK button to "IDRUN" and the caption to "Run."
  4. Create class wrappers from the type library for the Automation server as follows:
    1. In Solution Explorer, right-click AutoProject, and then click Add Class.
    2. Click MFC Class from Typelib, and then click Open.
    3. Click Add a class from: Registry, and locate the registered type library for your Automation server.

      For this example, select "Microsoft Excel 10.0 Type Library" for Microsoft ExcelXP or "Microsoft Excel 9.0 Type Library" for Microsoft Excel 2000.
    4. 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.
  5. To load and enable the COM services library to the CAutoProjectApp::InitInstance function, add the following code:
    if(!AfxOleInit())  // Your addition starts here.
    AfxMessageBox("Cannot initialize COM dll");
    return FALSE;
    // End of your addition.

  6. 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 "stdafx.h"
    #include "CApplication.h"

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.

AfxMessageBox("Cannot start Excel and get Application object.");
//Make the application visible and give the user control of
//Microsoft Excel.

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.

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:
ApplicationType Library
Microsoft Access 97Msacc8.olb
Microsoft Jet Database 3.5DAO350.dll
Microsoft Binder 97Msbdr8.olb
Microsoft Excel 97Excel8.olb
Microsoft Graph 97Graph8.olb
Microsoft Office 97Mso97.dll
Microsoft Outlook 97Msoutl97.olb
Microsoft PowerPoint 97Msppt8.olb
Microsoft Word 97Msword8.olb
Microsoft Access 2000Msacc9.olb
Microsoft Jet Database 3.51DAO360.dll
Microsoft Binder 2000Msbdr9.olb
Microsoft Excel 2000Excel9.olb
Microsoft Graph 2000 Graph9.olb
Microsoft Office 2000Mso9.dll
Microsoft Outlook 2000Msoutl9.olb
Microsoft PowerPoint 2000Msppt9.olb
Microsoft Word 2000Msword9.olb
Microsoft Access 2002Msacc.olb
Microsoft Excel 2002Excel.exe
Microsoft Graph 2002Graph.exe
Microsoft Office 2002 MSO.dll
Microsoft Outlook 2002MSOutl.olb
Microsoft PowerPoint 2002MSPpt.olb
Microsoft Word 2002MSWord.olb
Microsoft Office Access 2003Msacc.olb
Microsoft Office Excel 2003Excel.exe
Microsoft Office Graph 2003Graph.exe
Microsoft Office 2003MSO.dll
Microsoft Office Outlook 2003MSOutl.olb
Microsoft Office PowerPoint 2003MSPpt.olb
Microsoft Office Word 2003MSWord.olb
Microsoft Office Access 2007Msacc.olb
Microsoft Office Excel 2007Excel.exe
Microsoft Office Graph 2007Graph.exe
2007 Microsoft OfficeMSO.dll
Microsoft Office Outlook 2007MSOutl.olb
Microsoft Office PowerPoint 2007MSPpt.olb
Microsoft Office Word 2007MSWord.olb
Note The default location for these type libraries is:
Office VersionPath
Office 97C:\Program Files\Microsoft Office\Office
Office 2000C:\Program Files\Microsoft Office\Office
Office XPC:\Program Files\Microsoft Office\Office10
Office 2003C:\Program Files\Microsoft Office\Office11
2007 OfficeC:\Program Files\Microsoft Office\Office12
The default location for Dao350.dll and Dao360.dll is C:\Program Files\Common Files\Microsoft Shared\Dao.


Visit the Visual C++ .NET Support Center at the following Microsoft Web site:

ID článku: 307473 – Posledná kontrola: 16. 10. 2012 – Revízia: 1