Integrating with Microsoft Visual Basic for Applications

You can use Microsoft Visual Basic for Applications (VBA) with WordPerfect Office. VBA is an object oriented programming language that lets you create VBA macros to automate tasks. You can, for example, create a macro in WordPerfect that changes the color of the headings. VBA is not part of the install of WordPerfect Office; to use VBA, you must separately install Microsoft Visual Basic for Applications.

You can program and play a project or a global VBA macro in WordPerfect. You can play a project macro only from the document that was used to create it. If the macro does not perform the desired action, you can debug the macro.

You can edit a VBA macro. Editing a macro involves going directly to the start of the code, whereas debugging a macro involves stepping through each line of the code.

Debugging is an extremely important part of the programming process. Debugging lets you step through your code line by line to ensure that the macro executes properly. This feature of programming is known as single-stepping. For example, if you are at a point in the code where a variable is about to be assigned a value, you can step into this line of code to determine the value of the variable at run-time.

For more information about VBA, see the Microsoft Visual Basic Help in the Visual Basic Editor.


Event-driven programming

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.


The difference between PerfectScript and VBA

Previously, you could only use the PerfectScript language to automate specific tasks. Both product commands and programming commands are used in conjunction with the PerfectScript language. The PerfectScript language is useful for developing simple macros. VBA offers more flexibility and power. When you use VBA to create macros, you are assisted by the Visual Basic compiler. The compiler helps you by providing context-sensitive Help when you are coding a VBA macro. You can combine the power of VBA with the PerfectScript product commands to create powerful macros. You must use the Visual Basic Editor to create VBA macros; however, PerfectScript macros are developed from the WordPerfect Editor. You can access the Visual Basic Editor only when you are working in an active document.


The PerfectScript class

All product commands used in WordPerfect, Quattro Pro, and Presentations can be used in VBA macros. You must call the PerfectScript object to access a product command. An object is an instance of a class. All objects have properties; for example, the name of a document is a property that belongs to the document object. You can set or change an object’s properties by calling certain methods. A method performs a specific action, such as inserting a table into a document. You can, for example, close a document by calling the Close method, or you can save a document by calling the Save method. The term “class” is a VBA programming term and means a collection of related methods, which are referred to as class members. The product commands are the class members that belong to the PerfectScript class. WordPerfect, Quattro Pro, and Presentations have their own PerfectScript class because the class members, which are the product commands, are different depending on the application.

All members of the PerfectScript class will perform a specific action. For more information about product commands, see the macros manual.

The syntax for the product commands and the PerfectScript members is usually the same; however, there may be some differences.


Working in the VBA Editor

In most cases, the name of the class is the same as the name of the object. WordPerfect Office has one object, which is the document object. In WordPerfect, the name of the document object is ThisDocument, which is the same as the class. In Presentations, the name of the document object is ThisSlideShow, which is the same as the class. In Quattro Pro, the name of the document object is ThisDocument, which is the same as the class. The document object is the host application that is open. In future versions, there will be many more objects, such as a table object or a cell object.

You can create a new object, such as a dialog box, which is known as a form. You can add controls, such as a check box or a text box. You can set the object’s properties in the Property dialog box located in the Visual Basic Editor. You can also set the object’s properties at run time by programming a method. For example, you can color a form red when you click on it. For more information about setting an object’s properties, see the Microsoft Visual Basic Help in the Visual Basic Editor.

An experienced programmer can work directly with the Windows API (Application Programming Interface) and, for example, be able to determine how much disk space is free or gain access to the system’s time and date values. Most VBA users will want to automate specific and repetitive tasks.


Accessing an Application from another Application’s macro

You can access and change an application from another application’s macro. For example, you could create and use a Quattro Pro object from a WordPerfect VBA macro. This allows you to change and save a Quattro Pro document from a WordPerfect VBA macro.


Creating VBA macros

You can create two kinds of VBA macros: a global and a project macro. You can play a global macro from more than one document; however, a project macro can be played only in the document which was used to create it.

You can create a project VBA macro by creating a public method for the ThisDocument object. The term public refers to the access level. If a method is public, it can be accessed from outside the class. This means that you can call it from the host application. If a method is private, only methods in the same class can access it. Every public method that belongs to the ThisDocument class is a project macro. You can create a VBA macro called ShowMessage and code one line as follows: MsgBox “Hello”. This VBA macro calls a Message Box that displays the string “Hello.”


Using VBA macros

You can store a VBA macro in the document by saving the document. Once you have saved the document, you can close and reopen the document and access the macro. After you have developed the macro, you should debug it. You can step through each macro line by line. This is a useful exercise to ensure that the macro will have the desired outcome. A project macro is not available if the document is closed. After you have debugged the macro, you can play the macro.


Assigning security

When you open Wor
dPerfect Office documents created by other users, be aware that a document could contain a macro virus. As a screening measure, VBA includes a security mechanism to help manage document security. You determine the security level used when opening or loading documents and add-ins containing embedded VBA macros. You can review the assigned security level and collection of trusted certificates that come from reputable sources. A VBA developer can attach his digital signature to a macro which creates a certificate when the document is opened. The fact that a document has a digital signature does not automatically ensure the document to be free of viruses, so that the onus is on you to find out if the developer scans for viruses before adding his certificate to your trusted sources.

There are three security level settings available: High, Medium, and Low. With the High setting, you can open a signed VBA project from a trusted source with the macros enabled. Otherwise, the document is opened with the macros disabled. The default security setting is Medium. A Medium setting indicates that a warning displays if you try to load a VBA document that is not on your trusted source list. You can choose to open the document with the macros enabled or disabled. The Low setting allows you to open all VBA projects without applying any security (which disables all virus protection).


To start VBA from WordPerfect

WordPerfect Office btnbacktotopproc Integrating with Microsoft Visual Basic for Applications
 
Click Tools WordPerfect Office onestep Integrating with Microsoft Visual Basic for Applications Visual Basic WordPerfect Office onestep Integrating with Microsoft Visual Basic for Applications Visual Basic Editor.

WordPerfect Office note Integrating with Microsoft Visual Basic for Applications

 
The members of the PerfectScript class are applicable only to WordPerfect.

To display the VBA toolbar

WordPerfect Office btnbacktotopproc Integrating with Microsoft Visual Basic for Applications
1.
 
Click View WordPerfect Office onestep Integrating with Microsoft Visual Basic for Applications Toolbars.
2.
 
Enable the Visual Basic check box.

To create a VBA project macro in WordPerfect

WordPerfect Office btnbacktotopproc Integrating with Microsoft Visual Basic for Applications
1.
 
Click Tools WordPerfect Office onestep Integrating with Microsoft Visual Basic for Applications Visual Basic WordPerfect Office onestep Integrating with Microsoft Visual Basic for Applications Visual Basic Editor.
2.
 
Double-click Project (Document1) in the Project view.
3.
 
Click Insert WordPerfect Office onestep Integrating with Microsoft Visual Basic for Applications User form.
4.
 
Change the form so that it resembles the following diagram:

WordPerfect Office vba2 Integrating with Microsoft Visual Basic for Applications

5.
 
Change the name of the TextBox control to NameBox, as illustrated in the following list:
Command ButtonCommandButton1
TextBoxNameBox
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, and 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, and type the following lines of code in the Editor box:
Public Sub ShowForm()
End Sub
This code adds 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

To create a VBA global macro in WordPerfect

WordPerfect Office btnbacktotopproc Integrating with Microsoft Visual Basic for Applications
1.
 
Click Tools WordPerfect Office onestep Integrating with Microsoft Visual Basic for Applications Visual Basic WordPerfect Office onestep Integrating with Microsoft Visual Basic for Applications Visual Basic Editor.
2.
 
Double-click Global macros in the Project view.
3.
 
Click Insert WordPerfect Office onestep Integrating with Microsoft Visual Basic for Applications User form.
4.
 
Change the form so that it resembles the diagram below the procedure.
5.
 
Change the name of the TextBox control to NameBox as illustrated in the following list:
Command ButtonCommandButton1
TextBoxNameBox
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, and 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, and type the following lines of code in the Editor box:
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

WordPerfect Office vba2 Integrating with Microsoft Visual Basic for Applications

WordPerfect Office note Integrating with Microsoft Visual Basic for Applications

 
The name of this macro is ShowForm. For more information about VBA, see the Microsoft Visual Basic Help in the Visual Basic Editor.

To play a VBA macro in WordPerfect

WordPerfect Office btnbacktotopproc Integrating with Microsoft Visual Basic for Applications
1.
 
Click Tools WordPerfect Office onestep Integrating with Microsoft Visual Basic for Applications Visual Basic WordPerfect Office onestep Integrating with Microsoft Visual Basic for Applications Play.
2.
 
Choose one of the following macros from the Macros in list box:
ThisDocument — lets you play a project VBA macro
Global — lets you play a global VBA macro
3.
 
Choose a macro from the Macro name list box.
4.
 
Click Run.

WordPerfect Office tip Integrating with Microsoft Visual Basic for Applications

 
To start a VBA macro, you can also click the Play WordPerfect Office pgx play Integrating with Microsoft Visual Basic for Applications button on the Visual Basic toolbar.

To edit a VBA macro

WordPerfect Office btnbacktotopproc Integrating with Microsoft Visual Basic for Applications
1.
 
Click Tools WordPerfect Office  Integrating with Microsoft Visual Basic for Applications images/onestep.jpg" height="14" width="9" border="0" hspace="0" vspace="0" align="absmiddle"> Visual Basic WordPerfect Office onestep Integrating with Microsoft Visual Basic for Applications Play.
2.
 
Choose a macro from the Macro name list box.
3.
 
Click Edit.
4.
 
Edit the code as necessary.

To debug a macro created in VBA

WordPerfect Office btnbacktotopproc Integrating with Microsoft Visual Basic for Applications
1.
 
Click Tools WordPerfect Office onestep Integrating with Microsoft Visual Basic for Applications Visual Basic WordPerfect Office onestep Integrating with Microsoft Visual Basic for Applications Play.
2.
 
Choose a macro from the Macro name list box.
3.
 
Click Step into.
4.
 
For each line of code you want to step into, click Debug WordPerfect Office onestep Integrating with Microsoft Visual Basic for Applications Step into.
5.
 
Change the code as necessary.

WordPerfect Office note Integrating with Microsoft Visual Basic for Applications

 
After you have stepped into a line of code, you can point to a specific variable to determine the value of the variable.

Integrating with Microsoft Visual Basic for Applications