ID: Q178749
The information in this article applies to:
This article illustrates, in detail, how to automate component integration with COM-compliant applications such as the Microsoft Office 97 applications.
The following section illustrates how you can create an MFC project. The example automates Microsoft Excel. You can use the first 8 steps for any project, and modify steps 9 through 15 when you work with another application.
1. With Microsoft Developer Studio, start a new "MFC AppWizard (exe)"
project named "AutoProject".
2. In step 1 of the MFC AppWizard, choose "Dialog Based" for the
application type and then click Finish.
The New Project Information dialog box appears and indicates that the
Classes to be created include:
Application: CAutoProjectApp in AutoProject.h and AutoProject.cpp
Dialog: CAutoProjectDlg in AutoProject.h and AutoProjectDlg.cpp
Click OK to create the project.
3. In the project workspace, click the "Resource View" tab. Double-click
"AutoProject Resources" to expand the resource tree. Double-click
Dialog in the Resource tree and double-click to select the dialog
box resource "IDD_AUTOPROJECT_DIALOG".
4. Remove the Label control (IDC_STATIC) and the Cancel button (IDCANCEL).
5. Change the name of the OK button to "IDRUN" and the caption to "Run".
Close the AutoProject.rc dialog box design form.
6. Click ClassWizard on the View menu (or press CTRL+W).
7. Select the Message Maps tab. Select IDRUN in the Object Ids list box
and select "BN_CLICKED" in the Messages list box. Click Add Function
and accept the function name "OnRun". Click OK to close the
ClassWizard.
NOTE: This step adds a declaration for the function member "OnRun();"
to the header file named AutoProjectDLG.h. This step also adds an empty
skeleton message handler function named CAutoProjectDlg::OnRun() to the
file named AutoProjectDLG.cpp.
8. Click ClassWizard on the View menu (or press CTRL+W).
9. Select the Automation tab. Click Add Class and choose "From a type
library". Navigate to select the object library for the application
you wish to automate (for this example, choose the Microsoft Excel 8.0
Object Library; the default location is C:\Program Files\Microsoft
Office\Office\Excel8.olb) and click Open. Select all classes in the
Confirm Classes list and click OK.
NOTE: The list box in the Confirm Classes dialog box contains all of
the IDispatch interfaces (which are, after all, classes) in the
Microsoft Excel type library. In the lower half of the dialog box you
will see that an Implementation file named Excel8.cpp contains
generated class wrappers derived from ColeDispatchDriver(), and the
appropriate declaration header file is named Excel8.h.
10. Click OK to close the MFC ClassWizard dialog box.
11. Add the following code to the CAutoProjectApp::InitInstance() function,
which loads and enables the COM services library:
BOOL CAutoProjectApp::InitInstance()
{
if(!AfxOleInit()) // Your addition starts here
{
AfxMessageBox("Could not initialize COM dll");
return FALSE;
} // End of your addition
AfxEnableControlContainer();
.
.
.
}
12. Add the following line to the #include statements at the top of the
AutoProject.cpp program file:
#include <afxdisp.h>
13. Add the include statement for excel8.h after the include statement for
stdafx.h at the top of the AutoProjectDlg.cpp program file:
#include "stdafx.h"
#include "excel8.h"
14. Add automation code to the CAutoProjectDlg::OnRun() so that it appears
as shown below:
void CAutoProjectDlg::OnRun()
{
_Application app; // app is the Excel 8 _Application object
// Start Excel and get Application object...
if(!app.CreateDispatch("Excel.Application"))
{
AfxMessageBox("Couldn't start Excel.");
}
else
{
//Make Excel Visible and display a message
app.SetVisible(TRUE);
AfxMessageBox ("Excel is Running!");
}
}
15. Build and run the project.
RESULTS: When you click the Run button in the dialog box, Microsoft Excel will be launched. Activate the Auto_Excel dialog box and dismiss the message box. Microsoft Excel will quit when the CAutoProjectDlg::OnRun() function ends because the application variable will have gone out of scope.
Once you have added the classes from a type library to your project (as you did in step 9 above), you will notice that many classes have been added to the project. In ClassView, you can double-click a class to see the member functions of that class and then double-click the member function to view the definition of that function in the Excel8.cpp implementation file.
You need to go to the definition of a member function if you wish to verify a return type or if you need to change a function's implementation. Any time you change a function definition, remember to change the declaration in the Excel8.h file. When doing so, be sure that you change the correct function declaration; sometimes, the same name is given to member functions of multiple classes--GetApplication() is one such example.
Although the steps above illustrate how to automate Microsoft Excel, you can apply the same ideas to automating other applications. The list below contains the filenames for the type libraries of the Microsoft Office 97 applications.
Application Type Library
--------------------------------------------------
Microsoft Access 97 Msacc8.olb
Microsoft Jet Database 3.5 DAO350.dll
Microsoft Binder 97 Msbdr8.olb
Microsoft Excel 97 Excel8.olb
Microsoft Graph 97 Graph8.olb
Microsoft Office 97 Mso97.dll
Microsoft Outlook 97 Msoutl97.olb
Microsoft Powerpoint 97 Msppt8.olb
Microsoft Word 97 Msword8.olb
NOTE: The default location for these type libraries is C:\Program
Files\Microsoft Office\Office (except for Dao350.dll. The default location
for that file is C:\Program Files\Common Files\Microsoft Shared\Dao.)
This article presents a specific approach to building a dialog-box project. If you would like to see more general documentation about the process of building a VC++ project in the Microsoft Developer Studio environment, there is an excellent tutorial you can access by using Visual Studio InfoView. To access the tutorial, click Search on the Help menu. Click the index tab and type the following:
working with projects
Click List Topics. Select the topic "Home Page: Working With Projects" and
click Display.
Additional query words: IDispatch ole automation listbox
Keywords : kbinterop kbole kbAutomation kbMFC kbVC500 kbVC600
Version : win95:5.0; winnt:5.0,6.0
Platform : Win95 winnt
Issue type : kbhowto
Last Reviewed: January 7, 1999