WD2000: How to Use (OLE) Automation with Word

This article was previously published under Q237337
This article has been archived. It is offered "as is" and will no longer be updated.
This article contains a brief description and a sample macro detailing howto automate Microsoft Word from another program. For more detailedinformation, see the "References" section at the end of this article.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers 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 requirements.

Automation (formerly OLE Automation) is a feature that programs use toexpose their objects to development tools, macro languages, and otherprograms that support automation. For example, a spreadsheet program mayexpose a worksheet, chart, cell, or range of cells, each as a differenttype of object. A word processor might expose objects such as anapplication, a document, a paragraph, a sentence, a bookmark, or aselection.

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

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

Getting Started

There are four main steps to automate Word for Windows:
  1. Add a reference to the Microsoft Word 9.0 Object Library.
  2. Declare a variable as a Word object type (Application, Document, and so on).
  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: Add a Reference to the Word 9.0 Object Library

To add a reference to the Microsoft Word 9.0 Object Library using Microsoft Access 2000, Microsoft PowerPoint 2000, or Microsoft Excel 2000, 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 the Microsoft Word 9.0 Object Library check box.
NOTE: To add the reference using Microsoft Visual Basic 6.0, clickReferences on the Project menu.

Adding the Microsoft Word 9.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 Microsoft Word 9.0 Object Library reference is required to automate the Word object types directly.

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

For more information about Object Binding, please see the following articlein the Microsoft Knowledge Base:

138138 INFO: Late, ID, Early Binding Types Possible in VB for Apps

Step 2: Declare 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 objectof type Word.Application:

Dim objWD as Word.Application				

Step 3: Set the Variable

There are two Visual Basic functions you can use to "bind" the alreadydeclared object variable to Word: CreateObject and GetObject. The primary differences are that the CreateObject function creates a new instance of Word, and 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 usingthe CreateObject function:
Dim objWD as Word.Application   Set objWD = CreateObject("Word.Application")				
The following sample command lines bind the objWdDoc variable to a specificWord 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 another program.

For more information about getting help with Visual Basic for Applications,please see the following article in the Microsoft Knowledge Base:
237338 WD2000: How to Check for WordMail Using Automation

Step 4: Use Properties and Methods to Automate Word

When you complete steps 1 through 3, you can use the object variable to automateWord.

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 = NothingEnd 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"   QuitEnd Sub				
For more information specific to automating Word using Visual Basic forApplications, please see the following resources.

Microsoft Office Developer Web Site



The following peer-to-peer newsgroup is available to help you interactwith other users of Visual Basic for Applications:


Knowledge Base

For more information about getting help with Visual Basic for Applications,please see the following articles in the Microsoft Knowledge Base:

226118 OFF2000: Programming Resources for Visual Basic for Applications
For more information about how to use the sample code in this article, clickthe article number below to view the article in the Microsoft Knowledge Base:
212536 OFF2000: How to Run Sample Code from Knowledge Base Articles

Office Assistant

For more information about Automation, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type Communicating with other applications in the Office Assistant or the Answer Wizard, and then click Search to view the topic.
vba OFF2000

Article ID: 237337 - Last Review: 12/05/2015 15:14:18 - Revision: 2.0

Microsoft Word 2000 Standard Edition

  • kbnosurvey kbarchive kbinfo kbprogramming KB237337
ERROR: at System.Diagnostics.Process.Kill() at Microsoft.Support.SEOInfrastructureService.PhantomJS.PhantomJSRunner.WaitForExit(Process process, Int32 waitTime, StringBuilder dataBuilder, Boolean isTotalProcessTimeout)