Microsoft KB Archive/222101

From BetaArchive Wiki

Article ID: 222101

Article Last Modified on 11/22/2005



APPLIES TO

  • Microsoft Office Standard Edition 2003
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 4.0 Enterprise Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic 6.0 Enterprise Edition
  • Microsoft Visual C++ 4.0 Professional Edition
  • Microsoft Visual C++ 5.0 Professional Edition
  • Microsoft Visual C++ 6.0 Professional Edition
  • Microsoft Visual J++ 6.0 Standard Edition
  • Microsoft Visual Studio .NET 2002 Professional Edition
  • Microsoft Visual Studio .NET 2003 Professional Edition
  • Microsoft Access 2002 Standard Edition
  • Microsoft Access 2000 Standard Edition
  • Microsoft Access 97 Standard Edition
  • Microsoft Excel 2002 Standard Edition
  • Microsoft Excel 2000 Standard Edition
  • Microsoft Excel 97 Standard Edition
  • Microsoft FrontPage 2002 Standard Edition
  • Microsoft FrontPage 2000 Standard Edition
  • Microsoft Outlook 2002 Standard Edition
  • Microsoft Outlook 2000 Standard Edition
  • Microsoft Outlook 97 Standard Edition
  • Microsoft Outlook 98 Standard Edition
  • Microsoft PowerPoint 2002 Standard Edition
  • Microsoft PowerPoint 2000 Standard Edition
  • Microsoft PowerPoint 97 Standard Edition
  • Microsoft Word 2002 Standard Edition
  • Microsoft Word 2000 Standard Edition
  • Microsoft Word 97 Standard Edition
  • Microsoft Visual FoxPro 5.0a
  • Microsoft Visual FoxPro 6.0 Professional Edition
  • Microsoft MapPoint 2002 Standard Edition
  • Microsoft MapPoint 2001 Standard Edition



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:

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)
Office Vbaoff8.hlp Vbaoff9.chm Vbaof10.chm Vbaof11.chm
Access Acvba80.hlp Acmain9.chm Acmain10.chm, Vbaac10.chm ACMAIN11.chm,VBAAC10.chm
Excel VbaXl8.hlp Vbaxl9.chm Vbaxl10.chm VBAXL10.chm
MapPoint N/A Mappoint.chm (ver.2001) Mappoint.chm MAPPOINT.chmn (Ver. 2004)
Outlook Vbaoutl.hlp Vbaoutl9.chm Vbaol10.chm VBAOL11.chm
PowerPoint Vbappt.hlp Vbappt9.chm Vbapp10.chm VBAPP10.chm
Word Vbawrd8.hlp Vbawrd9.chm Vbawd10.chm VBAWD10.chm
Graph Vbagrp8.hlp Vbagrp9.chm Vbagr10.chm VBAGR10.chm
FrontPage N/A Vbafp4.chm, Vbafpom4.chm Vbafpd10.chm, vbafpw10.chm VBAFPD10.chm, VBAFPW10.chm
Binder Vbabdr8.hlp Vbabdr8.hlp Discontinued



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:


Additional query words: references help docs

Keywords: kbhowto kbautomation kbvbp400 kbvbp500 kbvbp600 _ik11561 _ik11593 kbvba kbgrpdso KB222101