DetailPage-MSS-KB

Microsoft small business knowledge base

Article ID: 184974 - Last Review: January 22, 2007 - Revision: 4.3

 
This article was previously published under Q184974

On This Page

SUMMARY

This article contains a brief description and a sample macro detailing how to automate Microsoft Word from another program. For more detailed information, see the "References" sub-section at the end of this article.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs.
If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:

Microsoft Certified Partners - https://partner.microsoft.com/global/30000104 (https://partner.microsoft.com/global/30000104)

Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice (http://support.microsoft.com/gp/advisoryservice)

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS (http://support.microsoft.com/default.aspx?scid=fh;en-us;cntactms)
Automation (formerly OLE Automation) is a feature that programs use to expose their objects to development tools, macro languages, and other programs that support Automation. For example, a spreadsheet program may expose a worksheet, chart, cell, or range of cells, each as a different type of object. A word processor might expose objects such as an application, a document, a paragraph, a sentence, a bookmark, or a selection.

When a program supports Automation, you can use Visual Basic for Applications to access the objects it exposes. You manipulate these objects in Visual Basic by invoking methods on the object or by getting and setting the object's properties.

You can use the code samples in this article to control Microsoft Word from Microsoft Access 97, Microsoft Excel 97, Microsoft PowerPoint 97, Microsoft Visual Basic for Applications, or any other client that supports Automation to control Word.

Getting Started

There are four main steps to automate Word for Windows.
  1. Add a reference to the Microsoft Word 8.0 Object library.
  2. Declare a variable as a Word object type.
  3. Assign the object returned by the CreateObject function to the object variable you declared in step 2.
  4. Use the properties and methods of the object variable to automate Word.

Step 1: Adding a Reference to the Word 8.0 Object Library

To add a reference to the Microsoft Word 8.0 Object Library using Microsoft Access 97, Microsoft PowerPoint 97, or Microsoft Excel 97, follow these steps:
  1. In Microsoft Access, PowerPoint, or Excel, on the Tools menu, point to Macros, and then click Visual Basic Editor.
  2. In the Visual Basic Editor, on the Tools menu, click References.
  3. In the list of Available References, click to select (check) the Microsoft Word 8.0 Object Library check box.
NOTE: To add the reference using Microsoft Visual Basic 5.0, click References on the Project menu.

Adding the Microsoft Word 8.0 Object Library reference allows your program to access Microsoft Word Online Help and the Microsoft Word Visual Basic for Applications constants, properties, and methods. Note that the Word 8.0 Object Library reference is required to automate the Word object types directly.

Adding a reference to the Word 8.0 Object Library is called early binding.

For more information about early binding, please see the following article in the Microsoft Knowledge Base:
138138  (http://support.microsoft.com/kb/138138/EN-US/ ) INFO: Late, ID, Early Binding Types Possible in VB for Apps

Step 2: Declaring the Object Variable

To declare an object variable, you dimension the variable just as you dimension any variable, except that you specify the type when declaring the object. For example, Word.Application, Document, and Paragraph are separate Word Objects.

The following sample command line declares the variable objWD as an object of type Word.Application:

   Dim objWD as Word.Application

				

Step 3: Setting the Variable

There are two Visual Basic functions you can use to "bind" the already declared object variable to Word: CreateObject and GetObject. The primary differences are that the CreateObject function creates a new instance of Word, while the GetObject function uses an existing, or already running instance of Word. You can also use GetObject to bind your object variable to a specific Word document.

The following sample command lines bind the objWD variable to Word using the CreateObject function:

   Dim objWD as Word.Application
   Set objWD = CreateObject("Word.Application")

				
The following sample command lines bind the objWdDoc variable to a specific Word document:
   Dim objWdDoc As Word.Document
   Set objWdDoc = GetObject("c:\my documents\doc1.doc")
				
NOTE: It is recommended to use only the CreateObject function to automate Word for Windows. The GetObject function can cause unpredictable behavior if WordMail is running or if a Word document is embedded inside of another program.

For more information about getting help with Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base:
185232  (http://support.microsoft.com/kb/185232/EN-US/ ) WD97: Error Message Using WordMail: 'This method or property is not available'

Step 4: Use Properties and Methods to Automate Word

When you complete steps 1-3, you can use the object variable to automate Word.

The following sample macro uses automation to create a Word object, create a new document, add some text, and save the document.
Sub AutomateWord()
   ' Declare the variable.
   Dim objWD As Word.Application
   ' Set the variable (runs new instance of Word.)
   Set objWD = CreateObject("Word.Application")
   ' Add a new document.
   objWD.Documents.Add
   ' Add some text.
   objWD.Selection.TypeText "This is some text."
   ' Save the document.
   objWD.ActiveDocument.SaveAs filename:="mydoc.doc"
   ' Quit Word.
   objWD.Quit
   ' Clear the variable from memory.
   Set objWD = Nothing
End Sub
				
NOTE: The following sample macro duplicates the process described in the AutomateWord macro and runs directly in Word:
Sub WordMacro()
   Documents.Add
   Selection.TypeText "This is some text"
   ActiveDocument.SaveAs filename:="mydoc.doc"
   Quit
End Sub
				

References

For more information specific to automating Word using Visual Basic for Applications, please see the following resources.

Microsoft Office Developer Web Site

http://www.msdn.microsoft.com/office (http://www.msdn.microsoft.com/office)

Microsoft Office 97/Visual Basic Programmer's Guide

Using Automation in the Microsoft Word Objects chapter of the Microsoft Office 97/Visual Basic Programmer's Guide (ISBN: 1-57231-340-4).

-or-

View the guide online at:
http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/office97/html/web/fulltoc.asp (http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/office97/html/web/fulltoc.asp)

Newsgroups

The following peer-to-peer newsgroups are available to help you interact with other users of Visual Basic for Applications:
microsoft.public.word.vba.addins
microsoft.public.word.vba.beginners
microsoft.public.word.vba.customization
microsoft.public.vb.ole.automation

Knowledge Base

For more information about getting help with Visual Basic for Applications, please see the following articles in the Microsoft Knowledge Base:
163435  (http://support.microsoft.com/kb/163435/EN-US/ ) VBA: Programming Resources for Visual Basic for Applications

173707  (http://support.microsoft.com/kb/173707/EN-US/ ) OFF97: How to Run Sample Code from Knowledge Base Articles

163425  (http://support.microsoft.com/kb/163425/EN-US/ ) WD97: Macro Programming Resources

Office Assistant

For more information about Automation, in the Visual Basic Editor, click the Office Assistant, type "Automation", click Search, and then click to view "Communicating with other applications."

NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If the Assistant is not able to answer your query, please see the following article in the Microsoft Knowledge Base:
176476  (http://support.microsoft.com/kb/176476/EN-US/ ) OFF: Office Assistant Not Answering Visual Basic Questions

APPLIES TO
  • Microsoft Excel 97 Standard Edition
  • Microsoft Word 97 Standard Edition
  • Microsoft Visual Basic for Applications 5.0
Keywords: 
kbdtacode kbhowto kbinterop kbprogramming KB184974
Retired KB ArticleRetired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
Share
Additional support options
Ask The Microsoft Small Business Support Community
Contact Microsoft Small Business Support
Find Microsoft Small Business Support Certified Partner
Find a Microsoft Store For In-Person Small Business Support