How to find and use Office object model documentation (222101)



The information in this article applies to:

  • Microsoft Office 2003, All Editions
  • Microsoft Visual Basic Professional Edition for Windows 4.0
  • Microsoft Visual Basic Professional Edition for Windows 5.0
  • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual Basic Enterprise Edition for Windows 4.0
  • Microsoft Visual Basic Enterprise Edition for Windows 5.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • Microsoft Visual C++, 32-bit Professional Edition 4.0
  • Microsoft Visual C++, 32-bit Professional Edition 5.0
  • Microsoft Visual C++, 32-bit Professional Edition 6.0
  • Microsoft Visual J++ 6.0
  • Microsoft Visual Studio .NET (2002), Professional Edition SP1
  • Microsoft Visual Studio .NET (2003), Professional Edition
  • Microsoft Access 2002
  • Microsoft Access 2000
  • Microsoft Access 97
  • Microsoft Excel 2002
  • Microsoft Excel 2000
  • Microsoft Excel 97 for Windows
  • Microsoft FrontPage 2002
  • Microsoft FrontPage 2000
  • Microsoft Outlook 2002
  • Microsoft Outlook 2000
  • Microsoft Outlook 97
  • Microsoft Outlook 98
  • Microsoft PowerPoint 2002
  • Microsoft PowerPoint 2000
  • Microsoft PowerPoint 97 for Windows
  • Microsoft Word 2002
  • Microsoft Word 2000
  • Microsoft Word 97 for Windows
  • Microsoft Visual FoxPro for Windows 5.0a
  • Microsoft Visual FoxPro for Windows 6.0
  • Microsoft MapPoint 2002
  • Microsoft MapPoint 2001

This article was previously published under Q222101

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.

ApplicationVersion 97 (or 8.0)Version 2000 (or 9.0)Version 2002 (or 10)Version 2003 (or 11)
OfficeVbaoff8.hlpVbaoff9.chmVbaof10.chmVbaof11.chm
AccessAcvba80.hlpAcmain9.chmAcmain10.chm, Vbaac10.chmACMAIN11.chm,VBAAC10.chm
ExcelVbaXl8.hlpVbaxl9.chmVbaxl10.chmVBAXL10.chm
MapPointN/AMappoint.chm (ver.2001)Mappoint.chmMAPPOINT.chmn (Ver. 2004)
OutlookVbaoutl.hlpVbaoutl9.chmVbaol10.chmVBAOL11.chm
PowerPointVbappt.hlpVbappt9.chmVbapp10.chmVBAPP10.chm
WordVbawrd8.hlpVbawrd9.chmVbawd10.chmVBAWD10.chm
GraphVbagrp8.hlpVbagrp9.chmVbagr10.chmVBAGR10.chm
FrontPageN/AVbafp4.chm, Vbafpom4.chmVbafpd10.chm, vbafpw10.chmVBAFPD10.chm, VBAFPW10.chm
BinderVbabdr8.hlpVbabdr8.hlpDiscontinued


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:

166738 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

The 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\1033

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

For 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++ developers

For 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 developers

For 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 property

For additional information, including sample code and resources for automating Microsoft Office, visit the following Microsoft Web site:

Modification Type:MajorLast Reviewed:11/22/2005
Keywords:_IK11561 _IK11593 kbAutomation kbGrpDSO kbhowto KbVBA kbVBp400 kbVBp500 kbVBp600 KB222101 kbAudDeveloper