Writing macros

Writing macros

Before you can begin writing a macro, you must create a Global Macro Storage (GMS) file — also known as a project file — for it. A GMS file is stored in its application’s GMS folder, which is typically located at X:\Program Files\Corel\CorelDRAW Graphics Suite X4\\<application>\ (where X is the letter that corresponds to the drive to which you’ve installed CorelDRAW Graphics Suite X4). The Macro Editor stores all of the modules for that project in the project’s GMS file.

In some network scenarios, access to the Program Files folder requires administrator-level rights. In such scenarios, users can store their GMS files at X:\Users\<user name>\AppData\Roaming\ Corel\Graphics X4\User Draw\GMS.

Each project that you create can contain several modules. The Project Explorer (see page 18) presents each module type in its own folder. You cannot move a module from one folder to another within the same project, but you can drag a module to another project to make a copy of it there. There are four types of modules:

• CorelDRAW X4 Objects or Corel PHOTO-PAINT X4 Objects — used mostly for event handling,

contains a single item ( ThisMacroStorage or ThisDocument , respectively) and should not be used for normal code

• forms — used for custom dialog boxes and user interfaces, including the code to control them • modules — used for general code and macros • class modules — used for object-oriented Visual Basic classes (which are not discussed in this guide)

After you create a GMS file, you can change its project name and add modules to it. To write the macro, you must use the Macro Editor. Macros that are developed in the Macro Editor can take

advantage of full programming control, including conditional execution, looping, and branching. In effect, you can write macros that are programs in their own right. (For the purposes of this guide, however, all macro code is referred to as a macro even though, in some contexts, a macro is just those parts of that code that can be launched by CorelDRAW or Corel PHOTO-PAINT.)

To create a project file

1 Close the application (that is, CorelDRAW or Corel PHOTO-PAINT) for which you want to create the project file.

2 In Windows Explorer, navigate to the GMS folder for the application for which you want to create the project file.

3 Click File ` New ` Text document.

A new, empty text document is created in this folder.

4 Rename the file as <filename>.gms, where <filename> is any valid Windows filename.

5 Restart the application for which you’ve created the project file. When you launch the Macro Editor, the project file you’ve created is displayed in the Project Explorer as

Global Macros (<filename>.gms).

To rename a project

1 In the Project Explorer, select the project you want to rename.

2 In the Properties window, edit the (Name) value. Names must follow normal variable-naming conventions, so they must begin with an alphabetic character,

and they must not contain spaces nor special characters other than underscores ( _ ).

3 Press Enter to commit your changes.

To add a module to a project

1 In the Project Explorer, right-click the project to which you want to add a module.

2 Click one of the following • Insert ` Module — inserts a normal code module • Insert ` UserForm — inserts a new form (that is, dialog box) • Insert ` Class Module — inserts a new class code module The new module is stored in the project’s folder for that type of module.