<Content Editor:
Insert Logo Here>®
Automating Microsoft
Oce 97 and Oce 2000
Microsoft Product Support Services White Paper
Written by Lori Turner
Published on <Publish Date>
Abstract
This document provides the fundamentals for understanding how to automate Microsoft Office 97 and
Microsoft Office 2000 applications. This document starts with the basics and walks you through
creating a variety of fully functional Automation clients. It is structured as a tutorial with a large assortment
of sample code and provides tips throughout that will facilitate development of your Automation clients.
Exercises and sample code are presented for Microsoft Visual Basic, Microsoft C/C++, and Microsoft
Foundation Classes (MFC) developers. However, much of the information is generic (not language-
specific) so you can apply it regardless of the development language you use.
© 1999 Microsoft Corporation. All rights reserved.
The information contained in this document represents the current view of Microsoft Corporation on the issues discussed as
of the date of publication. Because Microsoft must respond to changing market conditions, it should not be interpreted to be
a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information presented after the
date of publication.
This White Paper is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN
THIS DOCUMENT.
Microsoft, Visual Basic, Visual C++, Visual J++, Outlook, and PowerPoint are either registered trademarks registered
trademarks or trademarks of Microsoft Corporation in the United States and other countries.
Other product or company names mentioned herein may be the trademarks of their respective owners.
Microsoft Corporation • One Microsoft Way • Redmond, WA 98052-6399 • USA
0X97 Part no. 098-XXXXX
CONTENTS
INTRODUCTION.....................................................................................................................1
OFFICE OBJECT MODELS.......................................................................................................3
Objects, Methods, and Properties 3
Type Libraries 5
Object Browser 5
OLE/COM Object Viewer 6
Where to Find the Object Model Documentation 7
How to Use the Object Model Documentation 8
Exercise 1: Determining Which Classes, Methods, and Properties to Use 9
PROGIDs and CLSIDs 11
HOW AN OBJECT EXPOSES ITS METHODS AND PROPERTIES..............................................13
The Dispatch Interface 13
Virtual Function Table (vtable) 14
CREATING AN AUTOMATION CLIENT WITH VISUAL BASIC...................................................15
Binding 15
Early Binding 15
Late Binding 15
Choosing the Correct Type of Binding for Your Automation Client 16
Exercise 2: Creating a Visual Basic Automation Client that Uses Early Binding 16
Exercise 3: Creating a Visual Basic Automation Client that Uses Late Binding 17
Automating a Running Instance of an Office Application 18
CREATING AN AUTOMATION CLIENT WITH C++.................................................................20
The IUnknown and IDispatch Interfaces 20
Passing Parameters and Receiving Return Values 25
Using the BSTR String Type 26
Using SAFEARRAYs 27
Exercise 4: Creating an Automation Client with C/C++ 28
Passing Optional Arguments to Methods 32
Special Case for Property Put Functions 34
Error Handling 34
Exercise 5: Implementing an Error Handler for Your Client 35
Automate a Running Instance of an Office Application 37
CREATING AN AUTOMATION CLIENT WITH MFC..................................................................39
The COleDispatchDriver Class 39
Exercise 6: Creating an Automation Client with MFC 40
Exception Handling 45
Exercise 7: Implement Exception Handling in Your Automation Client 45
IMPROVING THE PERFORMANCE OF YOUR AUTOMATION CODE.........................................48
Tip #1 – Minimize "Selecting" or "Activating" Objects When Possible 48
Tip #2 – Minimize the Server's Screen Repaints 49
Tip #3 – Use Arrays 50
Tip #4 – Use the Features of the Automation Server to Your Benefit 50
Tip #5 – Minimize Interface Requests 52
Windows 95/98 Limitation on Interface Requests 53
HANDLING EVENTS IN THE AUTOMATION SERVER.............................................................54
Using WithEvents in Visual Basic 54
Exercise 8: Create a Visual Basic Automation client for Microsoft Excel that traps Excel's Change event
55
Using Connection Points with C++ and MFC 56
Exercise 9: Create an MFC Automation client for Microsoft Excel that traps Excel's Change event 57
AUTOMATING EMBEDDED AND LINKED OFFICE DOCUMENTS............................................62
Using the OLE Container in Visual Basic 63
Exercise 10: Embed and Automate an Excel Chart with Visual Basic 64
Use COleClientItem with MFC 66
Exercise 11: Create an MFC container that embeds and Automates a Microsoft Excel Chart 67
SAMPLE CODE FOR AUTOMATING MICROSOFT OFFICE APPLICATIONS...............................72
Automate Excel to Create and Format a New Workbook 72
Visual Basic Example 73
MFC Example 74
Additional Notes 76
Automate Excel to Add Data from a DAO Recordset to a Workbook 76
Visual Basic Example 77
MFC Example 78
Additional Notes 80
Automate Word to Perform a Mail Merge with Access Data 80
Visual Basic Example 82
MFC Example 83
Automate PowerPoint to Create and Run a Slide Show 85
Visual Basic Example 85
MFC Example 87
Automate an Office Application to Obtain a Document's Properties 90
Visual Basic Sample 91
MFC Sample 94
Additional Notes for Working with Office Document Properties 99
TROUBLESHOOTING COMMON PROBLEMS.......................................................................100
FOR MORE INFORMATION.................................................................................................104
INTRODUCTION
Automation, formerly called "OLE Automation," is a technology that allows you to take advantage of an
existing program's content and functionality, and to incorporate it into your own applications. Automation
is based on the Component Object Model (COM). COM is a standard software architecture based on
interfaces that is designed to separate code into self-contained objects, or components. Each component
exposes a set of interfaces through which all communication to the component is handled.
With Automation, you can use the Microsoft Word mail merge feature to generate form letters from data in
a database without the user being aware that Word is involved. You could even use all of the charting and
data analysis functionality that Microsoft Excel provides using Automation. You don’t need to write your
own calculation engine to provide the multitude of mathematical, financial, and engineering functions that
Excel provides; Instead, you can automate Microsoft Excel to "borrow" this functionality and incorporate it
into your own application.
Automation consists of a client and a server. The Automation client attaches to the Automation server so
that it can use the content and functionality that the Automation server provides. The terms client and
server are mentioned frequently throughout this document, so it is important that you understand their
relationship.
This document is intended to provide you with a foundation for developing your own Automation clients
for Microsoft Office applications. In this document, a hands-on approach is used to help you to do the
following:
1 Understand how Office applications expose their content and functionality to Automation clients.
2 Identify the specific functions for the task you choose to Automate.
3 Locate the resources and documentation you need.
4 Understand how Automation works behind the scenes.
5 Create Automation clients with Visual Basic, Visual C++, and MFC.
6 Develop a controller that uses the server as efficiently as possible.
All of the Microsoft Office applications have their own scripting language, which can be used to perform
tasks within the applications. This scripting language is Microsoft Visual Basic for Applications (VBA). The
set of functions that a Visual Basic for Applications routine, or macro, can use to control its host
application is the same set of functions that the Automation client can use to control the application
externally, regardless of the programming language for the controller. Understandably, the Office
applications provide documentation on their scripting functions in a syntax that is easily interpreted by the
Visual Basic for Applications programmer. So, if you choose to write your controller in another
programming language, such as Visual C++, Microsoft Foundation Classes (MFC), or Microsoft Visual J+
+, you must translate the Visual Basic for Applications syntax of Office functions so that you can apply it
to the programming language you choose for your controller.
This document is structured as a tutorial and consists of a series of discussions and exercises. For the
exercises, you need the following:
7 Microsoft Office 97 or 2000
8 Microsoft Visual Basic 5.0 or 6.0