Communicating with Other Applications

In addition to working with Microsoft Office Word data, you may want your application to exchange data with other applications, such as Microsoft Excel, Microsoft PowerPoint, or Microsoft Access. You can communicate with other applications by using Automation (formerly OLE Automation) or dynamic data exchange (DDE).

Automating Word from another application

Automation allows you to return, edit, and export data by referencing another application's objects, properties, and methods. Application objects that can be referenced by another application are called Automation objects.

The first step toward making Microsoft Word available to another application for Automation is to make a reference to the Word Application object. In Microsoft Visual Basic, you use the Visual Basic CreateObject or GetObject function to return a reference to the Word Application object. For example, in a Microsoft Office Excel procedure, you could use the following instruction.

Set wrd = CreateObject("Word.Application")

This instruction makes the Application object in Word available for Automation. Using the objects, properties, and methods of the Word Application object, you can control Word. For example, the following instruction creates a new Word document.

wrd.Documents.Add

Use the Visible property to make the new document visible after creating it.

wrd.Visible = True

The CreateObject function starts a Word session that Automation will not close when the object variable that references the Application object expires. Setting the object reference to the Visual Basic Nothing keyword will not close Word. Instead, use the Quit method to close the Word application. The following Microsoft Excel example displays the Word startup path. The Quit method is used to close the new instance of Word after the startup path is displayed.

Set wrd = CreateObject("Word.Application") 
MsgBox wrd.Options.DefaultFilePath(wdStartupPath) 
wrd.Quit

Automating another application from Word

To exchange data with another application using Automation from Word, you first obtain a reference to the application using the CreateObject or GetObject function. Then, using the objects, properties, and methods of the other application, you add, change, or delete information. When you finish making your changes, close the application. The following Word example displays the Microsoft Excel startup path. You can use the Visual Basic Set statement with the Nothing keyword to clear an object variable, which has the same effect as closing the application.

Set myobject = CreateObject("Excel.Application") 
MsgBox myobject.StartupPath 
Set myobject = Nothing

Using dynamic data exchange (DDE)

If an application does not support Automation, DDE may be an alternative. DDE is a protocol that permits two applications to communicate by continuously and automatically exchanging data through a DDE "channel." To control a DDE conversation between two applications, you establish a channel, select a topic, request and send data, and then close the channel. The following table lists the tasks that Word performs with DDE and the methods used to control each task in Visual Basic.

Security noteSecurity Note
Dynamic data exchange (DDE) is an older technology that is not secure. If possible, use a more secure alternative to DDE, such as object linking and embedding (OLE).

Task

Method

Starting DDE

DDEInitiate

Getting text from another application

DDERequest

Sending text to another application

DDEPoke

Carrying out a command in another application

DDEExecute

Close DDE channel

DDETerminate

Close all DDE channels

DDETerminateAll