Gordon McComb

Using OLE Automation to Control WordPerfect

In this document:

WPWIN 7 and later provide for a more robust means of automation using the OLE (Object Linking/Embedding) feature that is a standard part of Windows. While WPWin 7 and 8 continue to support DDE automation, you may wish to use OLE Automation instead, as it is generally easier to program. Additionally, as OLE Automation is now the accepted standard among all modern-day Windows applications, it is the method most likely to be supported in future versions of WPWIN. The DDE automation interface may be eventually replaced, and no longer available.

As with DDE automation using DDE Execute strings, in OLE Automation the program sending the commands is the client; the program receiving the commands and executing them is the server. In OLE Automation circles the client is often referred to as the "controller," because it is controlling the operating of another application.

In order to use OLE Automation the controller must have an OLE Automation interface. Visual Basic (versions 3.0 and higher) provide this, as do most other modern Windows 95 applications. For example, you can write a OLE application for Microsoft Word 97 that remotely controls WPWIN. This same application can be run from Microsoft Access, Visual Basic, and most any other program that supports the Visual Basic for Applications interface. You may also use Delphi, Paradox, and QuattroPro as an OLE Automation client. The examples below assume you are using Visual Basic.


An OLE Automation Example

The discussion that follows is intentionally on the simple side, because though most people don't know it, full documentation on using WPWin 7 and 8 for OLE Automation comes on the WPWin CD-ROMs (look under the SDK directory, then find the OLEAUTO directory; also look in the SKD\DOCS directory for WPOLEAUT.HTM.HTM, which provides some written documentation). The examples provided on the CD-ROM are a bit sophisticated, so I'll provide a simple demonstration that will be easier for most people to duplicate.

Create the following routine in Visual Basic (or any VBA-complaint application, such as Word). You can execute the code directly, or from a button, described below.

Sub OleAutoTest()
Dim WP As Object
Set WP = CreateObject("WordPerfect.PerfectScript")
With WP
.Type "This is a test"
.AppMaximize .Quit
End With
Set WP = Nothing End Sub

If you wish to trigger the code from a form, create a form with a single button on it (we'll assume the default name of CommandButton1 for this button). Add the following code for the click event for this button:

Sub CommandButton1_Click()
End Sub

Program Elements of OLE Automation

As you can see in the above example, there isn't much to using OLE Automation to control WPWIN. In fact, it is laughably simple. Every OLE Automation task you create for WPWin will consist of the following main elements:

  • Create an object for WPWIN. This consists of declaring a variable as an object type (done with the Dim WP As Object), then setting that variable to be a WordPerfect PerfectScript object (done with the CreateObject... statement).
  • Send one or more commands to WPWIN. These commands are WordPerfect product command tokens, such as Type, HardReturn, Tab, and so forth. However, not all WordPerfect commands can be used with OLE Automation. See the discussion in the DDE section earlier in this chapter for the command types that can be used.
  • The last OLE Automation command sent to WPWin must be Quit. If this is not sent WPWin may not reactivate.
  • The Set WP=Nothing "releases" the memory used by the WordPerfect object.

Note that the With WP block merely makes it easier when sending two more commands to WPWIN. If you wish, you may dispense with the With block, and send commands using the WP object name, as in

WP.Type "This is a test"


Starting a Macro with OLE Automation

A common task for OLE Automation is starting a macro within WPWIN. The OLE Automation script may send WPWin some initial commands, then complete its chores by running some specified macro. This can readily be done with the MacroFilePlay command. Note that the following example also demonstrates how to change focus (activate) WordPerfect.

Sub RunAMacro()
Dim WP As Object
Set WP = CreateObject("WordPerfect.PerfectScript")
AppActivate "Corel WordPerfect"
With WP
.MacroFilePlay "test.wcm"
.Quit End With Set WP = Nothing End Sub

This Visual Basic example first activates WPWin (with the Visual Basic AppActivate statement), then runs a macro called "test.wcm." Because no path is defined for test.wcm, it is assumed to be in the default macros directory. If this is not the case, you need to provide an absolute path to the macro, or else WPWin won't be able to find it.

The MacroFilePlay command is particularly useful in OLE Automation tasks that require interaction with PerfectScript commands not available via the OLE Automation interface. These include dialog boxes, the MessageBox statement, and other advanced programming commands.


Setting Variables With OLE Automation

Another common task for OLE Automation is sending a variable to WPWin for use either in a macro or a merge. There are numerous ways this can be done; the following example shows how to send WPWin a "persistent" variable, which can then be used in either a macro or a merge. You can send as many variables as you wish using this method. Note that because the variable is persistent, it is retained in memory until WordPerfect (actually the PerfectScript program) is terminated.

Sub SendVariable()
Dim WP As Object
Set WP = CreateObject("WordPerfect.PerfectScript")
With WP
	.VarAssign "MyVar", "This is the contents"
End With
Set WP = Nothing
End Sub

In this example, through OLE Automation Visual Basic creates a persistent variable named MyVar, and fills it with the text, "This is the contents."