HOWTO: Find and Use Office Object Model Documentation

ID: Q222101


The information in this article applies to:
  • Microsoft Visual Basic Professional and Enterprise Editions for Windows, versions 4.0, 5.0, 6.0
  • Microsoft Visual C++, 32-bit Professional Edition, versions 4.0, 5.0, 6.0
  • Microsoft Visual J++, version 6.0
  • Microsoft Access versions 2000, 97
  • Microsoft Excel 2000
  • Microsoft Excel 97 for Windows
  • Microsoft FrontPage 2000
  • Microsoft Outlook, versions 2000, 97, 98
  • Microsoft PowerPoint versions 2000, 97 For Windows
  • Microsoft Word 2000
  • Microsoft Word 97 for Windows
  • Microsoft Visual FoxPro for Windows, versions 5.0a, 6.0


SUMMARY

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.


MORE INFORMATION

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
    ISBN 1-57231-339-0

    Microsoft Office 2000 Visual Basic for Applications Language Reference
    ISBN 1-57231-955-0.
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:
http://mspress.microsoft.com
The following table lists the Help files for each Office application.

Application Version 97 (or 8.0) Version 2000 (or 9.0)
Office VbaOff8.hlp Vbaoff9.chm
Access AcVba80.hlp Acmain9.chm
Excel VbaXl8.hlp Vbaxl9.chm
Outlook VbaOutl.hlp Vbaoutl9.chm
PowerPoint VbaPpt.hlp Vbappt9.chm
Word VbaWrd8.hlp Vbawrd9.chm
Graph VbaGrp8.hlp Vbagrp9.chm
FrontPage N/A Vbafp4.chm, Vbafpom4.chm
Binder Vbabdr8.hlp Vbabdr8.hlp


The Help files that ship with Microsoft Office 97 are installed by default at:
C:\Program Files\Microsoft Office\Office
If 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:
Q166738 OL97: How to Install Visual Basic Help
The Help files that ship with Microsoft Office 2000 are installed by default at:
C:\Program Files\Microsoft Office\Office\1033
NOTE: Microsoft Office 2000 Setup installs 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 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:
  1. Start Microsoft Word.


  2. 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.


  3. Start a new document.


  4. Type one and press the ENTER key.


  5. Type two and press the ENTER key.


  6. Type three.


  7. Click Save on the File menu and save the document as "C:\doc1.doc."


  8. Click the Stop Recording button (or click Macro on the Tools menu and select Stop Recording).


  9. 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:


  10. 
     Documents.Add
     Selection.TypeText Text:="one"
     Selection.TypeParagraph
     Selection.TypeText Text:="two"
     Selection.TypeParagraph
     Selection.TypeText Text:="three"
     ActiveDocument.SaveAs FileName:="Doc1.doc", FileFormat:=wdFormatDocument,_
       LockComments:=False, Password:="", AddToRecentFiles:=True, _ 
       WritePassword:="", ReadOnlyRecommended:=False, _
       EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, _
       SaveFormsData:=False, SaveAsAOCELetter:= False 
You can benefit greatly from understanding how each class fits within the object model and learning the description and type of all parameters for the methods and properties you use.

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.


Return to the recorded macro, highlight Add on the code module and press the F1 key. A Help topic appears explaining that many different objects have an Add method. Click Documents to see the Help for the Add method of the Documents collection. The Help topic provides you with the following important information:
  • 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.


Now examine the next line in the recorded macro: Selection.TypeText Text:="one". Highlight Selection on the code module and press the F1 key:
  • The "Selection Property" returns the Selection object that represents a selected range or the insertion point.


  • The "Selection Property" applies to the Application object.


Return to the recorded macro, highlight TypeText on the code module and press the F1 key:
  • 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.


Next, see the Help topic for TypeParagraph:
  • The "TypeParagraph Method" inserts a new blank paragraph.


  • The "TypeParagraph Method" applies to the Selection Object and has no arguments.


Examine the Help topics for the ActiveDocument property and the SaveAs method:
  • 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. This method has 11 arguments, only one of which is required. "SaveAs" applies to a Document object.


You may have noticed that the Documents property, Selection property, and ActiveDocument property are all properties that apply to the Application object but yet are not qualified with "Application" in the recorded macro. The Application object is the default object for all properties and can therefore be omitted when writing code in a VBA macro. This is not the case when writing automation code; all properties and methods should be fully qualified in your automation code.

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:

wdFormatDocument

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.


REFERENCES

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 Developers

Q219151 HOWTO: Automate Excel 97 and 2000 from Visual Basic
Q147650 HOWTO: Navigate Excel Objects from Visual Basic
Q142387 HOWTO: Create Excel Chart w/OLE Automation from Visual Basic
Q184974 WD97: How to Use (OLE) Automation with Word
Q220607 HOWTO: Automate Word to Perform Mail Merge From Visual Basic
Q222929 HOWTO: Automate PowerPoint Using Visual Basic

Visual C++ Developers (using MFC)

Q178749 HOWTO: Create Automation Project Using MFC and a Type Library
Q179706 HOWTO: Use MFC to Automate Excel and Create/Format a New Workbook
Q186120 HOWTO: Use MFC to Automate Excel and Fill a Range with an Array
Q186122 HOWTO: Use MFC to Automate Excel and Obtain an Array from a Range
Q220911 HOWTO: Automate Word 97 to Do Mail Merge with Visual C++ and MFC
Q222960 HOWTO: Automate PowerPoint Using Visual C++ w/MFC

C/C++ Developers (without MFC)

Q216686 HOWTO: Automate Excel From C++ Without Using MFC or #import
Q181473 HOWTO: Use OLE Automation from a C Application Rather Than C++

Visual J++ Developers

Q219430 HOWTO: Create and Format an Excel Workbook Using Visual J++
Q215484 HOWTO: Automate PowerPoint using Visual J++

Visual FoxPro Developers

Q142193 HOWTO: Use OLE Automation to Add Data to Excel Sheet
Q180901 HOWTO: HOWTO: Create Categorized Table in Word 97 w/ OLE Automation
Q181926 HOWTO: Automate Mail Merge to Word97 SR-1 Using OLE and ODBC
Q194306 HOWTO: Make Word Print Duplex Using Automation
Q230154 HOWTO: Automate PowerPoint Using Visual FoxPro
Q241942 HOWTO: Prevent Word Printing Error with BackgroundPrintingStatus
For additional information, including sample code and resources for automating Microsoft Office, visit the Office Development Support site.

Additional query words: references help docs kbAccess97 kbAutomation kbExcel kbFrontPage kbOutlook kbProject KbVBA kbVBp kbVBp400 kbVBp500 kbVBp600 kbVC kbVC400 kbVC500 kbVC600 kbVJ kbVJ600 kbPowerPt kbWord

Keywords : kbAccess kbVFp500a kbVFp600 kbGrpDSO kbDSupport kbexcel2000 kbword2000
Version : WINDOWS:2000,4.0,5.0,5.0a,6.0,97,98; winnt:4.0,5.0,6.0
Platform : WINDOWS winnt
Issue type : kbhowto


Last Reviewed: November 29, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.