PerfectScript Journal

February 2000: VB as an Event-driven script language

PerfectScript is fairly linear in the way it executes. Execution starts at the top and runs to the bottom of the script. You can have functions that are called, and you can nest other macros. PerfectScript does have a few events that it will respond to. For example, an Error or Not Found condition is an event that can be captured with an ONERROR or ONNOTFOUND command. And if you use dialog callbacks, events on the dialog, such as clicking a button trigger an event, which you can test for in the callback. VBA is more automatic when responding to events, however.

VBA, on the other hand, is an event-driven scripting language. Various parts of the script are executed, based on certain events that occur while the script is executing. For example, if the user clicks on a button on a form (dialog), a CLICK event is called and the corresponding routine will be executed automatically. You don’t need to do any laborious testing for the events; you just write the code that should execute when the event occurs. VBA does the rest.

Global and Project VBA Macros

When you create a VBA macro, you must specify whether it will be a Global macro or a Project macro. A Global macro can be run from any WordPerfect 9 document. Global macros are stored in a file named WordPerfect9.GMS. This file is located in the c:\Program Files\Corel\WordPerfect Office 2000\macros\VBA directory by default. Project macros can only be used in the document that they were created for. They are stored in the document file.

Events

Both the Global and Project VBA macros have their own events that they respond to. The code that you write to respond to particular events are called event handlers. The name of an event handler for a Global event begins with GlobalMacro_, followed by the name of the event. The name of an event handler for a Project event begins with Document_, followed by the name of the event. For example, GlobalMacro_BeforeTables or Document__AfterOpen. These event handlers are placed in the VBA code window.

Global Events
The events that a global macro respond to are:

BeforeNew() This event is triggered before a new document is opened.
AfterNew() This event is triggered just after a new document is opened. If you want to have the VB Script type in default text in each new document, place the code in this event handler.
BeforeClose() This event is triggered just after you click Close, and before the document is actually closed.
AfterClose() This event is triggered just after a document is closed.
BeforeSwitchDocument() This event is triggered just before you switch to a new document.
AfterSwitchDocument() This event is triggered just after you switch to a new document.
BeforeTables() This event is triggered just before a new table is created.
AfterTables() This event is triggered just after a new table is created. This is a good place to put code that formats the tables.
BeforeOpen() This event is triggered just before an existing document is opened.
AfterOpen() This event is triggered just after an existing document is opened.
BeforePrint() This event is triggered just before a document is printed.
AfterPrint() This event is triggered just after a document is sent to the print queue.
BeforeSave() This event is triggered just before a document is saved.
AfterSave() This event is triggered just after a document is saved
AfterStartup() This event is triggered just after WordPerfect is started up.

The event handler code that responds to the AfterNew() event may look like the following sample. This routine will type the current date into each new document:

Private Sub GlobalMacros_AfterNew()
PerfectScript.DateText
End Sub

Project Events
Project events are similar to Global events, but are applicable only to existing documents. They include:

BeforeOpen() This event is triggered just before an existing document is opened.
AfterOpen() This event is triggered just after an existing document is opened.
BeforePrint() This event is triggered just before a document is printed.
AfterPrint() This event is triggered just after a document is sent to the print queue.
BeforeTables() This event is triggered just before a new table is created.
AfterTables() This event is triggered just after a new table is created. This is a good place to put code that formats the tables.
BeforeSave() This event is triggered just before a document is saved.
AfterSave() This event is triggered just after a document is saved
BeforeClose() This event is triggered just after you click Close, and before the document is actually closed.

The following routine will assign a series of variables based on current system data and display them in a document before it is opened. The MsgBox, Time, Date, and Str commands are VBA programming commands.

Private Sub Document_BeforeOpen()
’*** Declare all variables
Dim myTime
Dim myDate As Date
Dim myStrTime, myStrDate, Msg As String
’**** Populate the variables
myTime = Time
myDate = Date
myStrDate = Str(myDate)
myStrTime = Str(myTime)
’*** Display the Message Box
Msg = "The date is " & myStrDate & " and the time is " & myStrTime
MsgBox Msg
End Sub


                           
Copyright Notice: The information included in the PerfectScript Journal is protected by US Copyright. The author grants you the right to use the routines in your own macros as needed. You may not sell, distribute, or publish them in any form.
If you choose to use the information here, you do so entirely at your own risk. No representations are made regarding the fitness of this information for your particular purpose, or for your ability or inability to use the information. You are advised to make backups of all relevant files before implementing any suggestion or technique.
© Copyright 1999 by J. Jeppson.