Global macros can be executed in all WordPerfect documents. Global macros are stored in a special file called WordPerfect11.GMS. All objects that are used by a Global macro are also stored in the WordPerfect11.GMS. For example, if a macro uses a form, then the form is stored in the WordPerfect11.GMS.
2. Double-click on Global Macros in the Project view.
3. Click Insert, User Form.
4. Change the form so that it resembles the following diagram:
5. Change the name of the TextBox control to NameBox as illustrated in the following list:
Command Button - CommandButton1
TextBox - NameBox
You can change all of the control's attributes, including the name of the control, in the Property dialog box located in the Visual Basic Editor.
6. Double-click the CommandButton1 control to create a new method for the UserForm1 class, then type the following lines of code in the CommandButton1_Click method:
Private Sub CommandButton1_Click()
Dim myname, Msg As String
myname = NameBox
Msg = "Hello " & myname
MsgBox Msg
End
End Sub
This code responds to the event that occurs when the command button is clicked.
7 Double-click the WordPerfect Objects folder in the Project view.
8. Double-click ThisDocument, then type the following lines of code in the Editor window:
Public Sub ShowForm()
End Sub
This code will add a new method to the ThisDocument class. If you declare ShowForm as a Private method, you will not be able to run it from WordPerfect.
9. Type the following line of code in the ShowForm method:
Public Sub ShowForm()
UserForm1.Show
End Sub
Note
The name of this macro is ShowForm. The name of the form is UserForm1, which is the default name. UserForm1, along with this macro would be stored in WordPerfect11.GMS.
WordPerfect VBA Events
Visual Basic for Applications is an event-driven programming language. Most of the code you create is written to respond to an event. An event is an action that is recognized by VBA; for example, clicking a button or choosing an option from a list box. Unlike traditional procedural programming, in which the program starts at line 1 and executes line by line, event-driven programming executes code in response to events.
WordPerfect document events can be broken down into two categories: Global and Project. A Global event is recognized by all WordPerfect documents. For example, if you create a VBA macro in the AfterOpen event, this macro is valid for any WordPerfect document. In contrast to Global events there are Project events. A Project event is only recognized by the local document. Project events are members of the Document class. The name of the object is the same as the class.
Global events are members of the GlobalMacros class. The name of the object is the same as the class. Global macros are stored in a special file named WordPerfect11.GMS. All global events are stored in the WordPerfect11.GMS file.
Events in WordPerfect are code placeholders. It is up to you to code the response. Events are called in response to a specific action. When an action calls an event, the code located within the event is executed. You can create simple or complex events. You can code a single line that displays a Message Box or write an entire procedure that interacts with a database.
Global Events
BeforeNew()
AfterNew()
BeforeClose()
AfterClose()
BeforeSwitchDocument()
AfterSwitchDocument()
BeforeTables()
AfterTables()
BeforeOpen()
AfterOpen()
BeforePrint()
AfterPrint()
BeforeSave()
AfterSave()
AfterStartup()
Project Events
BeforeOpen()
AfterOpen()
BeforePrint()
AfterPrint()
BeforeTables()
AfterTables()
BeforeSave()
AfterSave()
BeforeClose()
Document.BeforeOpen()
Syntax
Private Sub Document_BeforeOpen()
Description
This event is called when you open a document. The code in the event is executed before the document appears.
Example
In the code fragment below, a Message Box appears which displays the time and date. This data can be written to a database that keeps track of file activities.
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
Note
The Message Box will appear before the document is opened. After the Message Box is closed, the document will open.
Document.AfterOpen()
Syntax
Private Sub Document_AfterOpen()
Description
This event is called as soon as an existing document opens.
Example
You can customize your document by inserting a watermark. You can insert a watermark into every odd page of your document by coding the following into the AfterOpen() event:
You need a graphic file for this procedure. The name of this file is:ROSE2.WPG. This file is located in the "C:\Corel\WordPerfect Office 2002\graphics\clipart\basic clipart" folder. It is recommended that you place this file in the C:\.
Document.AfterPrint()
Syntax
Private Sub Document_AfterPrint()
Description
This event is called after you print your document.
Example
You can inform the user that the document is printed by coding a Message Box.
Private Sub Document_AfterPrint()
MsgBox ("You have just printed this document")
End Sub
Document.BeforePrint()
Syntax
Private Sub Document_BeforePrint()
Description
This event is called when you send a document to the printer. This enables you to customize your document before it is printed.
Example
You can call a form which displays an image.
Private Sub Document_BeforePrint()
'******* Call the Form
FallsForm.Show
End Sub
Note
FallsForm is a VBA Form which contains an image control.
Document.AfterSave()
Syntax
Private Sub Document_AfterSave()
Description
This event is called after you save your document.
Example
You can create a message box that displays the time and date. This data can be stored to a database which keeps track of file activities.
Private Sub Document_AfterSave()
'*** 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
Document.BeforeSave()
Syntax
Private Sub Document_BeforeSave()
Description
This event is called when you save the document. This enables you to customize your document before you save it.
Example
In the following example, the zoom setting is changed to page view each time the document is saved. This setting persists until it is changed.
Private Sub Document_BeforeSave()
PerfectScript.ZoomToFullPage
End Sub
Document.AfterTables()
Syntax
Private Sub Document_AfterTables()
Description
This event is called after you insert a table into your document. This is a useful event to customize tables.
Example
You can create a macro that will format a table that is inserted into the document. In the following example, the table's border is changed. Add the following to the AfterTables() event:
In the above code fragment, the value 5 corresponds to a thick border. All PerfectScript class members that require any for an argument, must have the numeric value passed.
Document.BeforeTables()
Syntax
Private Document_BeforeTables()
Description
This event is called before the table appears in your document.
Example
In the following example, a new form appears that allows the user to select a choice of three border styles. The result is stored in a global variable called g_borderStyle.
Private Sub Document_BeforeTables()
'*** Call the New Form
UserForm1.Show
g_borderStyle = UserForm1.myBorder
End Sub
Note
You can pass g_borderStyle to the PerfectScript.TableBorder()
Document.BeforeClose()
Syntax
Private Sub Document_BeforeClose()
Description
This event is called when you close the document; however, this code is executed before the document is actually closed.
Example
You can create a message box that informs the user that the document will close. The message box will appear before the document is closed.
Private Sub Document_BeforeClose()
MsgBox "You are about to close this document"
End Sub
GlobalMacros.BeforeSwitchDocument()
Syntax
Private Sub GlobalMacros_BeforeSwitchDocument()
Description
This event is called when you switch documents. The code in this event is executed before the new document appears.
Example
In the following code fragment, a Message Box appears with the time and date. This data can be stored to a database which keeps track of file activities.
Private Sub GlobalMacros_BeforeSwitchDocument())
'*** 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 MessageBox
Msg = "The date is " & myStrDate & " and the time is " & myStrTime
MsgBox Msg
End Sub
GlobalMacros.AfterSwitchDocument()
Syntax
Private Sub GlobalMacros_AfterSwitchDocument()
Description
This event is called after you switch documents. In contrast to BeforeSwitchDocument, this event is executed after the new document appears.
Example
In the following code fragment, a Message Box appears with the time and date. This data can be stored to a database which keeps track of file activities.
Private Sub GlobalMacros_AfterSwitchDocument()
'*** 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 MessageBox
Msg = "The date is " & myStrDate & " and the time is " & myStrTime
MsgBox Msg
End Sub
GlobalMacros.BeforeNew()
Syntax
Private Sub GlobalMacros_BeforeNew()
Description
This event is called when you select a new document; however, the code is executed before the new document appears.
Example
In the following example, a simple string "Hello" is inserted into the document. The important concept to note is that the text will be entered into the existing document. This will not effect the new document.
Private Sub GlobalMacros_BeforeNew()
PerfectScript.KeyType "Hello"
End Sub
GlobalMacros.AfterNew()
Syntax
Private Sub GlobalMacros_AfterNew()
Description
This event is called when you select a new document; however, in contrast to BeforeNew() the code is executed after the new document appears.
Example
In the following example, a simple string "Hello" is inserted into the document. The important concept to note is that the text will be entered into the new document.
Private Sub GlobalMacros_AfterNew()
PerfectScript.KeyType "Hello"
End Sub
GlobalMacros.BeforeClose()
Syntax
Private Sub GlobalMacros_BeforeClose()
Description
This event is called when you close the document; however, the code is executed before the "Save changes to" dialog box appears.
Example
In the following example, a form is called which displays an image. The important concept to note is that this image will be displayed before the "Save changes to" dialog box appears.
Private Sub GlobalMacros_BeforeClose()
'**** Call the new form
FallsForm.Show
End Sub
Note
FallsForm is a VBA Form which contains an image control. You must create a form called FallsForm to implement this example.
GlobalMacros.AfterClose()
Syntax
Private Sub GlobalMacros_AfterClose()
Description
This event is called when you close the document; however, the code is executed after the "Save changes to" dialog appears.
Example
In the following example, a new form is called which simply displays an image. This image will appear after the "Save changes to" dialog box appears.
Private Sub GlobalMacros_AfterClose()
TOForm.show
End Sub
Note
TOForm is a VBA Form which contains an image control. You must create a form called TOForm to implement this example.
GlobalMacros.BeforeOpen()
Syntax
Private Sub GlobalMacros_BeforeOpen()
Description
This event is called when you open a document; however, this code is executed before the document appears.
Example
You can code a message box that displays the time and date. This data can be stored to a database which keeps track of file activities.
Private Sub GlobalMacros_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
GlobalMacros.AfterOpen()
Syntax
Private Sub GlobalMacros_AfterOpen()
Description
This event is called when you open a document; however, this code is executed after the document appears.
Example
You can customize your document by inserting a watermark as soon as the document opens. You can insert a watermark into every odd page of your document by coding the following into this event:
You need a graphic file for this procedure. The name of this file is:ROSE2.WPG. This file is located in the "C:\Corel\WordPerfect Office 2002\graphics\clipart\basic clipart" folder. It is recommended that you place this file in the C:\.
GlobalMacros.BeforePrint()
Syntax
Private Sub GlobalMacros_BeforePrint()
Description
This event is called when you print the document. The code is executed after the "Print to" dialog box is closed, but before the document is sent to the printer.
GlobalMacros.AfterPrint()
Syntax
Private Sub GlobalMacros_AfterPrint()
Description
This event is called when you print the document. The code is executed after the document is sent to the printer.
GlobalMacros.BeforeTables()
Syntax
Private Sub GlobalMacros_BeforeTables()
Description
This event is called when you insert a table into the document. This code is executed before the table is inserted into the document.
Example
In the following example, a new form will appear that will allow the user to select a border style. The user can select one of three styles. The result will be stored in a global variable called g_borderStyle.
Private Sub Document_BeforeTables()
'*** Call the New Form
UserForm1.Show
g_borderStyle = UserForm1.myBorder
End Sub
Note
You can pass g_borderStyle to the PerfectScript.TableBorder()
GlobalMacros.AfterTables()
Syntax
Private Sub GlobalMacros_AfterTables()
Description
This event is called after you insert a table into your document. This is a useful event to customize tables.
Example
You can create a macro that will format a table that is inserted into the document. In the following example, the table's border is changed. Add the following to the AfterTables() event:
In the above code fragment, the value 5 corresponds to a thick border. Refer to Product Command Values to obtain the correct integer value.
GlobalMacros.BeforeSave()
Syntax
Private Sub GlobalMacros_BeforeSave()
Description
This event is called just before the document is saved. This gives you a chance to customize your document before it is saved.
Example
In the following example, the zoom setting is changed to full page every time the document is saved. This setting will last until it is changed.
Private Sub Document_BeforeSave()
PerfectScript.ZoomToFullPage
End Sub
GlobalMacros.AfterSave()
Syntax
Private Sub GlobalMacros_AfterSave()
Description
This event is called after you have saved your document.
Example
In the following code fragment, a Message Box appears with the time and date. This data can be stored to a database which keeps track of file activities.
Private Sub GlobalMacros_AfterSave()
'*** 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
GlobalMacros.AfterStartup()
Syntax
Private Sub GlobalMacros_AfterStartup()
Description
This event is called when WordPerfect starts. The code in this event is executed while the splash screen is active.
Example
You can create a macro that will call a message box asking you if you want a watermark in the WordPerfect document. If you select Yes, then a watermark will be inserted.
You need a graphic file for this procedure. The name of this file is:ROSE2.WPG. This file is located in the "C:\Corel\WordPerfect Office 2002\graphics\clipart\basic clipart" folder. It is recommended that you place this file in the C:\.
Working with product commands that use a Variable
You must declare a variable that you pass to a product command as a Variant.
A Boolean value is returned to myAnswer. If the directory exists, then myAnswer will be assigned the value True. If the directory does not exist, then myAnswer will be assigned False.
WordPerfect product commands that use Raw Binary Data
The following product commands use raw binary data:
ThirdPartyCreate
ThirdPartyFindByID
WordPerfect product commands that use WordPerfect unit values
There are many product commands which require a WordPerfect unit as an argument. You must calculate any numeric value into a WordPerfect unit. The formula to calculate a WordPerfect unit is:
WordPerfect unit = (n*1200), where n is an inch.
Note
The easiest way to convert to a WordPerfect unit is to convert all measurements to inches and multiply 1200.
WordPerfect product commands that use WordPerfect Codes
WordPerfect codes are used by WordPerfect to complete a specific objective. For example, a hyphen is a WordPerfect code. You can check the Reveal Codes for other codes. PerfectScript recognizes WordPerfect codes; however, the VBA compiler does not. The following two product commands use WordPerfect codes:
WordPerfect product commands that use WordPerfect Codes:
DateFormat
PrinterCommand
Product Commands that use Any
You must pass the integer value of any parameter that uses any. The VBA compiler does not recognize the string value. For example, the product command TableBorder() uses any for an argument. You can pass ThinBorder! or ThickBorder! when you are creating macros using PerfectScript. However, when you are using VBA to create macros, you must pass the integer value which corresponds to the style you want.
Product Commands that use Any:
Product command that use Any
Code Example
Working with repeating parameters
To use product commands in VBA that have repeating parameters an array must be used. You should declare a variant and assign an array to the variant. Refer to the following code example, which illustrates how to use PreTaskBar:
You must define the box style, the icon style, and the width for every item that you want to appear on the application bar. In the example above, there are six elements in each array, meaning that six items will appear on the application bar.
Three variants are created. Each variant is defined as an array, with each element corresponds to the application bar item. The boxes array is populated with the specific box style. All values in the textIcon array are 0, representing icons as opposed to 1 which represents text. The values in the widths array specifies the width of each item. All three variables are passed after they have been populated.
In the second example, all the arrays are populated during the product command call. The benefit of this method is it decreases the lines of code in your macro.
Note
You must use the integer values when populating an array used for repeating parameters.
WordPerfect product commands that are reserved words in VBA
There are two product commands that are reserved words in VBA. You have to use the VBA equivalent. Refer to the following list:
Product command VBA equivalent
Type() KeyType
Print() WPPrint
Product commands with repeating parameters
To use product commands in VBA with repeating parameters, you must declare an array. Values for each repetitive parameter must be loaded into the array. After the array is populated, you have to pass the array.
Product commands with repeating parameters:
Product command that have Repeating Parameters
Code Example
Note
You must use the integer values when populating an array used for repeating parameters.VBA Programming issues relating to PerfectScript class members
There are several issues that must be discussed with respect to programming with product commands in the VBA environment. You can click on any of the following for a detailed explanation:
Product commands that use Any
Product commands with Repeating Parameters
Product commands that require a Variable
Product commands that use WordPerfect Codes
Product commands that use Raw Binary Data
Product commands that are Reserved Words in VBA
Product commands that use WordPerfect Units
WordPerfect product commands that use a VARIABLE as a parameter
You must use a Variant for any WordPerfect product command that requires a variable as a parameter. The Variant data type is the data type for all variables that are not declared as another specific type. If you do not declare the variable as a Variant, then your VBA macro will not function properly . The following list is all the product commands in WordPerfect which use Variable as a parameter:
Product commands that use a VARIABLE as a parameter: