PerfectScript Journal

January 2000: Creating a VB Macro

The Visual Basic Editor

This article is meant only to be a brief overview of how to create a Visual Basic macro in WordPerfect.

All VBA macros are created in the Visual Basic Editor. You can record some of the VBA code, but you must paste it into the editor to make the code work. (See PSJournal for August 1999)

To start a VBA macro,

Click Tools > Visual Basic > Visual Basic Editor.
The VBA editor will appear on the screen. In the Project window, you’ll see an item called GlobalMacros. Macros created in this area can be used in all WordPerfect 9 documents.

Note: If the Visual Basic Editor option is greyed out on the menu, you probably didn't install VBA with WordPerfect. It is not installed by default. Start the WordPerfect installation again, select Custom Install, adn select VBA.

You’ll also see an option for each document that is currently open. You can create a macro that is specific only to one document by selecting one of these items when you create your macro.

Global Macros

Global macros can be executed in all WordPerfect documents. Global macros are stored in a special file called WordPerfect 9.GMS. All objects that are used by a Global macro are also stored in the WordPerfect 9.GMS. For example, if a macro uses a form, then the form is stored in the WordPerfect 9.GMS.

To create a global macro in VBA,

  1. Click the [+] next to GlobalMacros in the Project view.
  2. Click the [+] next to WordPerfect Objects.
  3. Double-click ThisDocument.
    The code window will appear.
  4. Select the desired event in the left drop down box.
    Note that the event is added, as a Private subroutine of type GlobalMacros_. In image below, I’ve added an AfterNew event handler to the GlobalMacros.
  5. Type the desired code in the new routine.
  6. Click File > Save WordPerfect 9.GMS.

The routine will be saved to the GlobalMacros file.

Project Macros

To create a VBA macro for a specific document,

  1. Open the document.
  2. Click Tools > Visual Basic > Visual Basic Editor.
    The VBA editor will appear on the screen.
  3. Click the [+] next to VBAProject () in the Project view.
  4. Click the [+] next to WordPerfect Objects.
  5. Double-click ThisDocument.
    The code window will appear.
  6. Select Document in the left drop down box.
  7. Select the desired event in the right drop down box.
    Note that the event is added, as a Private subroutine of type Document_.
  8. Type the desired code in the new routine.
  9. Click File > Save, then type a document name.

                           
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 2000 by J. Jeppson.