This article describes the resources and documentation available to you for automating Microsoft Office applications. This information can be applied regardless of the programming language you choose for automating an Office application. This article also illustrates how you can begin writing automation code by stepping you through the documentation to find the information you need to accomplish a specific task through automation.
Where can I find the object model documentation?The object models for the Office applications are documented in a Language Reference for both versions of Office: Microsoft Office 97 Visual Basic for Applications Language Reference
Microsoft Office 2000 Visual Basic for Applications Language Reference
The Language References are available on the MSDN and in the online Help that ships with Microsoft Office. They can also be purchased in printed form. For ordering information, please visit:
The following table lists the Help files for each Office application.
|Application||Version 97 (or 8.0)||Version 2000 (or 9.0)||Version 2002 (or 10)||Version 2003 (or 11)|
|MapPoint||N/A||Mappoint.chm (ver.2001)||Mappoint.chm||MAPPOINT.chmn (Ver. 2004)|
|FrontPage||N/A||Vbafp4.chm, Vbafpom4.chm||Vbafpd10.chm, vbafpw10.chm||VBAFPD10.chm, VBAFPW10.chm|
The Help files that ship with Microsoft Office 97 are installed by default at:
C:\Program Files\Microsoft Office\OfficeIf you cannot find the Office 97 VBA Help file you need, it probably was not installed when you initially ran Office 97 setup. To install the Help file, run the Office setup to add the VBA Help file. Note that the Outlook 97 VBA Help file is not installed by Office setup. For information on installing the Outlook 97 VBA Help file, please see the following article in the Microsoft Knowledge Base:
166738 OL97: How to install Visual Basic HelpThe Help files that ship with Microsoft Office 2000 are installed by default at:
C:\Program Files\Microsoft Office\Office\1033The Help files that ship with Microsoft Office XP are installed by default at:
C:\Program Files\Microsoft Office\Office10\1033
C:\Program Files\Microsoft Office\Office11\1033NOTE: The Microsoft Office 2000, Microsoft Office XP, and Microsoft Office 2003 Setup programs install the VBA Help files "on first use." Therefore, you might not see the Help file in this directory if you have not previously attempted to access VBA Help in the Office application.
How can I use the object model documentation?There are several methods you could use to find documentation for a specific class, method, or property:
- Search the VBA Help File:
In the Visual Basic Editor for the Office application, click Contents and Index on the Help menu. On the Contents tab, select the language reference you want and click Display. The VBA Help for the Language Reference you selected appears. At this point, you can use either the Index or the Find tab to locate information on a specific class, method, or property.
- Use Context Sensitive Help in a Module or in the Immediate Pane:
In the Visual Basic Editor for the Office application, type the class, method, or property in the code window of a module or in the Immediate Window, highlight the text and press the F1 key. The Help topic for the item appears.
- Use the Object Browser:
Press the F2 key in the Visual Basic Editor for the Office application to display the Object Browser. The Object Browser lists all the classes the application exposes and the methods or properties associated with each class. To view Help on a specific class or class member, select it in the Object Browser and press the F1 key.
How do I know which classes, methods, and properties to use?If you are not already familiar with the object model of the application you intend to automate, you can use the application's Macro Recorder to get an idea. To illustrate, suppose you want to automate Microsoft Word to add some text to a new document and then save the document but you don't know which methods and properties to use; you can start with the Macro Recorder:
- Start Microsoft Word.
- Click Macro on the Tools menu and then select Record New Macro. Make note of the new macro's name and click OK to start recording.
- Start a new document.
- Type one and press the ENTER key.
- Type two and press the ENTER key.
- Type three.
- Click Save on the File menu and save the document as "C:\doc1.doc."
- Click the Stop Recording button (or click Macro on the Tools menu and select Stop Recording).
- To view the VBA code that the Macro Recorder generated from your actions, click Macro on the Tools menu and select Macros. Select the name of the new macro in the list and click Edit. The Visual Basic Editor appears with the recorded macro:
ActiveDocument.SaveAs FileName:="Doc1.doc", FileFormat:=wdFormatDocument,_
LockComments:=False, Password:="", AddToRecentFiles:=True, _
WritePassword:="", ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, _
SaveFormsData:=False, SaveAsAOCELetter:= False
Start with examining the first line of the recorded macro: Documents.Add. Highlight Documents on the code module for the Recorded macro and press the F1 key. The Help topic provides you with the following important information:
- The "Documents Property" returns a Documents collection that represents all the open documents.
- The "Documents Property" applies to the Application object.
- The "Add Method" adds a new, empty document to the collection of open documents.
- The "Add Method" can take two arguments, both of which are optional.
- The "Selection Property" returns the Selection object that represents a selected range or the insertion point.
- The "Selection Property" applies to the Application object.
- The "TypeText Method" inserts the specified text.
- The "TypeText Method" has one required argument of type String.
- The "TypeText Method" applies to the Selection Object.
- The "TypeParagraph Method" inserts a new blank paragraph.
- The "TypeParagraph Method" applies to the Selection Object and has no arguments.
- The "ActiveDocument property" returns a Document object representing the document with the focus. "ActiveDocument" applies to the Application object.
- The "SaveAs method" saves a document. Depending on the version of Word, this method has from 11 to 16 arguments, only one of which is required. "SaveAs" applies to a Document object.
Upon examination of the recorded macro, you see that the SaveAs method has an argument for which it passes the built-in constant wdFormatDocument. Depending on the programming language you choose for your automation controller, you might need to pass the numeric value for the built-in constants. The Help topic for the SaveAs method does not give this information but you can find it in the Object Browser. Press the F2 key to display the Object Browser. Type:
in the search window and press the ENTER key. In the bottom pane of the Object Browser, you see the numeric equivalent of wdFormatDocument(=0) as well as other information about the constant.
Where can I find some automation code samples?
The information presented so far gives you good groundwork for writing automation code. The Microsoft Knowledge Base is an excellent resource for finding automation code samples written in Visual Basic, Visual C++ and MFC. Here are just a few:
For Visual Basic developersFor more information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
220595 How to automate Outlook using Visual Basic
219151 How to automate Microsoft Excel from Visual Basic
147650 How to navigate Excel objects from Visual Basic
142387 How to create Excel chart w/OLE automation from Visual Basic
184974 How to use (OLE) automation with Word
220607 How to automate Microsoft Word to perform mail merge from Visual Basic
222929 How to automate PowerPoint by using Visual Basic in Office 2003, in Office XP Developer, and in Office 2000 Developer
Visual C++ developers (using MFC)For more information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
178749 How to create an automation project using MFC and a type library
179706 How to use MFC to automate Excel and create and format a new workbook
186120 How to use MFC to automate Excel and fill a range with an array
186122 How to use MFC to automate Excel 2000 and Excel 2002 and obtain an array from a range in Excel 2000 and Excel 2002
220911 How to automate Microsoft Word to perform a mail merge using Visual C++ and MFC
222960 How to automate PowerPoint by using Visual C++ 5.0 or Visual C++ 6.0 with The Microsoft Foundation Classes
C/C++ developers (without MFC)For more information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
216686 How to automate Excel from C++ without using MFC or #import
181473 How to use OLE Automation from a C application rather than C++
Visual J++ developersFor more information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
219430 How to create/format an Excel workbook using Visual J++ automation
215484 How to automate PowerPoint by using Visual J++ 6.0
Visual FoxPro developersFor more information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
142193 How to use OLE Automation to add data to Excel sheet
180901 How to create a categorized table in Word 97 or in Office Word 2003 from Visual FoxPro for Windows 5.0 and later versions
181926 How to automate mail merge to Word using OLE and ODBC
194306 How to make Word print duplex by using OLE automation from Visual FoxPro
230154 How to automate PowerPoint using Visual FoxPro
241942 How to prevent Word printing error with BackgroundPrintingStatus propertyFor additional information, including sample code and resources for automating Microsoft Office, visit the following Microsoft Web site: