Corel Corel Draw Graphics Suite - X6 Macro Programming Guide

Macro Programming Guide
Contents
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
About this resource . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
About additional resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Understanding automation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
What is automation? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
What is VBA?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6
What is VSTA?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
What are the main elements of automation? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
What is an object model? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10
What is a class?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
What is a collection? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11
What is a property?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
What is a method? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12
What is an event? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12
What is an enumeration? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
What is a constant?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12
How is automation coding structured? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Declaring variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13
Using functions and subroutines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Ending lines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Including comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Allocating memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Defining scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17
Using Boolean comparison and assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17
Using logical and bitwise operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18
Providing message boxes and input boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19
Referencing objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Referencing collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20
Using object shortcuts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Providing event handlers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Getting started with macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Setting up the automation feature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
To install the VBA and VSTA features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
To specify VBA options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25
Using the Macros toolbar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Using the Macro Manager docker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Using the Add-in Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Using the Macro Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Using the Project Explorer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .30
Using the Code window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Using the Properties window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Using the Macro Editor toolbars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35
Using the Object Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35
Using the VSTA Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Macro Programming Guide Contents i
Creating macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Creating macro projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
To create a macro project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
To add a dialog box to a macro project. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44
To add a code module to a macro project. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
To add a class module to a macro project. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45
Writing macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
To add a macro to a macro project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
To edit a VBA macro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
To delete a VBA macro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Recording macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
To record and save a macro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .47
To record a temporary macro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48
Running macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
To run a saved macro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
To run a temporary macro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .50
Debugging macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Making macros user-friendly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Providing toolbars for macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
To create a macro toolbar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .55
To add buttons to a macro toolbar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .55
To associate an image or icon with a macro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .55
To set a caption for a macro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .55
To set a tooltip for a macro. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Providing dialog boxes for macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Setting up dialog boxes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .57
Coding dialog boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .59
Providing user interaction for macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Capturing mouse clicks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .64
Capturing mouse drags. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Capturing coordinates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Providing documentation for macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Organizing and deploying macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Organizing macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Deploying macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
To export a GMS file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
To import a GMS file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .69
To export workspace features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .69
To import workspace features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .69
Understanding the CorelDRAW object model . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Understanding the object-model hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Working with the Application object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Working with documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Creating documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Opening documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Activating documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .81
Setting document properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .82
Displaying documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Modifying documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .84
Creating command groups for documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Macro Programming Guide Contents ii
Saving documents. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Exporting files from documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Publishing documents to PDF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Printing documents. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Closing documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Working with pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Creating pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .93
Activating pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .94
Reordering pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .95
Sizing pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .95
Modifying pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .96
Deleting pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .97
Working with layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Creating layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .99
Activating layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Locking and hiding layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .99
Reordering layers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .100
Renaming layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Importing files into layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Deleting layers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Working with shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Creating shapes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Determining shape type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .121
Selecting shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Duplicating shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Transforming shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Coloring shapes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .130
Applying effects to shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .135
Searching for shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138
Deleting shapes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138
Working with import filters and export filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Working with import filters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .139
Working with export filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .141
Glossary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Macro Programming Guide Contents iii

Introduction

Welcome to the Macro Programming Guide!
This resource can help you explore the macro-related features and functions of CorelDRAW® and Corel® PHOTO-PAINT™. An understanding of these features and functions can help you automate tasks or develop commercial solutions that integrate with the software.
In this resource
This resource contains the following sections.
Section Description
“Understanding automation” on page 4 Introduction to the concepts of automation and macros,
and to the macro-programming formats supported by the software
“Getting started with macros” on page 25 Overview of the macro-related tools and features of the
software
“Creating macros” on page 42 How to write, record, run, and debug macros
“Making macros user-friendly” on page 54 How to enhance the usability of macros through dialog
boxes, toolbar buttons, user interaction, and documentation
“Organizing and deploying macros” on page 68 How to organize and deploy the macros you create
“Understanding the CorelDRAW object model” on page 71
Overview of the most important features and functions of the CorelDRAW object model
Also included is a glossary (see page 144), which defines many of the key terms used in this documentation.
Most of the code examples provided in this documentation are written in VBA.
In this section
This section contains the following topics:
• “About this resource” on page 1
• “About additional resources” on page 2

About this resource

This resource assumes that the reader has experience with at least one procedural programming language, such as BASIC, Microsoft® Visual Basic® (VB), C, C++, Java™, Pascal, Cobol, or Fortran.
Macro Programming Guide Introduction 1
This resource does not describe the basics of procedural programming (such as functions, conditional branching, and looping). Therefore, before using this documentation, non-programmers are strongly advised to learn basic programming in a language such as Microsoft® Visual Basic® for Applications (VBA).
Most of the code examples provided in this documentation are written in VBA.
For more detailed instruction on the VB programming environment and on VBA, see Microsoft Visual Basic Help, which is available from the Help menu in the Macro Editor.
For a more basic introduction to macros, please see the topic “Working with macros” in the main Help file for the application. You can access the main Help from within an application by clicking Help ` Help topics.
Documentation conventions
The following table explains the documentation conventions used in this resource.
Wherever you see this You’ll find
A note — describes required conditions for performing a procedure or presents other essential information
A tip — describes helpful information such as shortcuts, alternate methods, or benefits that are related to a procedure
bold text The name of a control or other element on the user interface
<text in italics and between angle brackets> A placeholder for user-specified information, such as a path or filename
monospace text
A reference to coding

About additional resources

This topic describes how to get even more information about macros or the software.
For more information about macros
This software provides additional resources that contain helpful information about macros. These additional resources, located in the Data folder for the installed software, are described in the following table.
Resource Description and filename
Macro Help for CorelDRAW Provides comprehensive information about the CorelDRAW
object model and the macro-related features and functions of the application
draw_om.chm
Macro Programming Guide Introduction 2
Resource Description and filename
Macro Help for Corel PHOTO-PAINT Provides comprehensive information about the Corel PHOTO-
PAINT object model and the macro-related features and functions of the application
pp_om.chm
Object-model diagram for CorelDRAW Provides a hierarchical representation of the CorelDRAW object
model
CorelDRAW Object Model Diagram.pdf
Object-model diagram for Corel PHOTO-PAINT Provides a hierarchical representation of the Corel PHOTO-
PAIN T obj e ct m o del
Corel PHOTO-PAINT Object Model Diagram.pdf
For a more basic introduction to macros, please see the topic “Working with macros” in the main Help file for the application. You can access the main Help from within an application by clicking Help ` Help topics.
For more information about the software
A variety of additional resources for the software are also available to you.
For comprehensive information about the features in the software, you can consult its installed documentation:
• The program group for the software (on the Windows® Start menu) includes a Documentation folder, which provides easy access to various installed resources.
• Each program offers in-product Help, displayed by clicking Help ` Help topics.
For even more information about the software, see the following Web-based resources.
Resource Description and URL
CorelDRAW website Provides the latest news, tips and tricks, and information about upgrades
www.corel.com/coreldraw
Corel® Support Services website Provides prompt and accurate information about product features,
specifications, pricing, availability, services, and technical support
www.corel.com/support
Corel® Knowledge Base™ Provides a repository of articles written by the Corel Technical Support
Services team in response to questions by users
www.corel.com/knowledgebase
CorelDRAW online community Provides interaction with other users through sharing experiences, asking
questions, and receiving help and suggestions
www.coreldraw.com
You can submit any comments or suggestions about the software by using the contact information provided at www.corel.com/contact.
Macro Programming Guide Introduction 3

Understanding automation

Before you begin to work with macros, you need to understand the concept of automation. This section provides basic information about automation and about the macro-programming formats that are supported by the software.
In this section
This section contains the following topics:
• “What is automation?” on page 4
• “Which automation environments are supported?” on page 5
• “What are the main elements of automation?” on page 10
• “How is automation coding structured?” on page 13

What is automation?

Many actions that you perform in the software can be combined with other, related actions into a single automated solution. Automating repetitive tasks saves time, reduces effort, and lets you perform operations that are too complex to perform manually.
Automation can be used by programmers and nonprogrammers alike.
This documentation does not teach programming skills to nonprogrammers; rather, it helps experienced programmers develop useful solutions within the software. If you are not a programmer, you may want to refer to other programming-related resources before continuing to read this documentation.
What is a macro?
Most macros are created to automate a series of tasks within an application. The simplest meaning of the term “macro” is the recording of a group of related actions that can be played back automatically, in sequence, whenever you need to perform them. Macros consist of instructions that are written in a programming language, and some programming languages provide access to additional, more advanced, actions that cannot be recorded.
For the purposes of this documentation, a macro refers to a coded solution that performs tasks in the application by automating functions and subroutines (see “Using functions and subroutines” on page 15).
Although you can record a sequence of actions in the software, the real power of automation is that you can add conditions and looping mechanisms to a recording. As an example, let’s consider a simple macro that applies a red fill and a 1-point outline to a selected shape. By adding a condition and a looping mechanism to the code, you can produce a macro that seeks out each selected shape and applies only the fill to text shapes and only the outline to all other shape types.
After you create a macro, you can ensure that it has the desired outcome by setpping through its code one line a a time, or “debugging” it. When you are happy with the macro, you can keep it for future use and even share it with others.
Macro Programming Guide Understanding automation 4
Which sample macros are available?
The software includes sample VBA macros, which supply additional functionality, demonstrate automation in the suite, and provide sample code.
The following sample VBA macros are included for CorelDRAW:
• File converter (FileConverter.gms) — converts a vector or bitmap to a specified vector or bitmap format. You can choose export parameters by using dialog boxes associated with particular filters. You can also save each page as a separate file and set various page properties, such as size, orientation, and background color. The following file formats are supported: AI, BMP, CDR, CGM, CMX, CPT, DSF, DXF, EPS, GIF, JPEG, PCT, PNG, PPF, SVG, SWF, TIF, WMF, and WPG.
• Calendar wizard (CalendarWizard.gms) — generates customized calendars. You can choose the date range, layout, font, color, language, and many other options. You can also add holidays and moon phases.
The following sample VBA macro is included for Corel PHOTO-PAINT:
• HTML slide show creator (Slideshow.gms) — generates an HTML slide show from the images you specify. Open files can be added, ordered, and published as a series of HTML files, each of which displays one image and provides navigation buttons. You can create a title, alternate text, and a name for each slide. You can also choose a location and a name for the delineation folder; select the image folder; and specify notes, a caption, a URL, and more.

Which automation environments are supported?

For CorelDRAW versions 6 through 9, the only method of automating tasks was using the Corel SCRIPT™ language. Solution developers used Corel SCRIPT to create intelligent mini-applications for drawing shapes, repositioning and resizing shapes, opening and closing documents, and setting styles within CorelDRAW.
Although the Corel SCRIPT language was useful for automating basic tasks, a more flexible and powerful solution became necessary. For version 10, CorelDRAW was enhanced with support for the Microsoft Visual Basic for Applications (VBA) engine, which handled behind-the-scenes automation. The addition of VBA made CorelDRAW immediately accessible to millions of VBA and Microsoft Visual Basic (VB) developers around the world. Since then, VBA has been supported by every version of the CorelDRAW software suite.
More recently, the software suite added support for Microsoft® Visual Studio® Tools for Applications (VSTA), the successor to VBA.
Although CorelDRAW no longer includes the Corel SCRIPT editor, it does include the Corel SCRIPT run-time engine. Therefore, you can easily migrate scripts that were written for earlier versions of CorelDRAW to later versions of the software. For information on using Corel SCRIPT with CorelDRAW, see “Working with scripts” in the main Help file for CorelDRAW (draw.chm).
In Corel PHOTO-PAINT, you can automate tasks by using VBA or VSTA to create a macro, or by using Corel SCRIPT to create a script. A macro is the better choice if you want to write the code that is required to carry out the task (by using VBA or VSTA), while a script is the better choice if you want to record the steps that are required to carry out the task (by using Corel SCRIPT). For information on using
Macro Programming Guide Understanding automation 5
Corel SCRIPT with Corel PHOTO-PAINT, see “Working with scripts” in the main Help file for Corel PHOTO-PAINT (corelpp.chm).
By supporting VBA and VSTA, the software suite offers a platform for the following:
• developing powerful corporate graphical solutions — such as automated ticket generators, customized calendars, and batch file-processors
• streamlining workflows — such as with on-the-fly page-layout mechanisms
• customizing default software features — such as the creation, alignment, or transformation of objects
• ...and much more!
VBA and VSTA each provide their own fully integrated development environment (IDE), with contextual pop­up lists, syntax highlighting, line-by-line debugging, and visual designer windows. These features are particularly helpful to inexperienced developers.
For more information on VBA and VSTA, see the following topics:
• “What is VBA?” on page 6
• “What is VSTA?” on page 8

What is VBA?

Microsoft Visual Basic for Applications (VBA) is a built-in programming environment that can be used to automate repetitive functions and create intelligent solutions in the software that supports it. VBA is a subset of the Microsoft Visual Basic (VB) object-driven programming environment. Usually, VBA is integrated into another application to customize functionality within that application.
VBA is both a language and an editor. The VBA language cannot be used without its editor, and the VBA editor is the only utility in which VBA code can be edited or VBA programs can be run.
The VBA language is an event-driven programming language. In other words, it is used to write code that produces a response to an action, such as clicking a button or choosing an option from a list box. When the action occurs, the appropriate event is called, and the code for that event is executed. Events can be simple or complex. For instance, you can code a single line that displays a message box or write an entire procedure that interacts with a database.
With traditional procedural programming (or “object-driven programming”), the program starts at the first line and executes one line at a time. VB provides an example of an object-driven programming environment.
Most of the code examples provided in this documentation are written in VBA.
The VBA editor — called the “Macro Editor” (formerly the “Visual Basic Editor”) in CorelDRAW and Corel PHOTO-PAINT — is an integrated development environment (IDE) that lets you manipulate the objects that are exposed by the object model of the application. To help you code macros, the Macro Editor provides context-sensitive Help for all the object-model elements that are available to you.
VBA is an in-process automation controller. In other words, VBA can be used to control software features that can be automated, and VBA runs efficiently by bypassing the interprocess synchronization mechanisms. However, the automation that the in-process VBA can access can also be accessed by the following:
• external out-of-process automation controllers (OLE clients)
• applications that are developed in programming languages (such as VB, Visual C++, Windows® Script Host, and C++) that can be used to develop OLE clients
Macro Programming Guide Understanding automation 6
• the VBA engines of other applications
VBA provides a set of tools for customizing the graphical user interface of the software. These tools let you process and present data efficiently and effectively. Advantages of using VBA include the following:
• familiarity of the VB language
• rapid application development (RAD) IDE
• fast run-time performance of the resulting integrated solutions
• extensible forms package that supports ActiveX® controls for creating user interfaces
• access to the full Windows application programming interface (API) and the underlying file system
• connectivity to corporate data
• integration with other software that is based on component object models (COMs)
VBA lets you customize an application to suit your needs, or even integrate it with another VBA-enabled application by referencing the object-model components of the second application. Although VBA was developed by Microsoft and is built into almost all its desktop applications (including Microsoft Office), Microsoft licenses the technology to other companies (including Corel Corporation; Autodesk, Inc., in AutoCAD®; and IntelliCAD Technology Consortium, in IntelliCAD®). Software products that support VBA can typically be used to control each other, and they can even be used to control some software products that do not support VBA. Consequently, you can use VBA to build solutions in CorelDRAW and Corel PHOTO-PAINT that access a wide variety of other software products: databases, word processors, XML editors, and more.
For a complete list of applications that support VBA, see the Microsoft website.
How does VBA differ from VB and VBScript?
The VB programming system is an advanced set of programming tools that provides advanced functionality and components for the Windows operating system and other Windows-based programs. For example, unlike VBA or VBScript, VB lets you create application extensions (DLL files) and stand-alone executable programs (EXE files). (The programs that you create with VBA must run inside the host application.)
VB is a “visual”version of the BASIC programming language — that is, it provides visual cues within the editor. As a result, VB is an easy language to learn. In addition, Microsoft has greatly enhanced the original BASIC language, so that VB is both powerful and fast (although not as powerful as Java or C++, nor as fast as C).
VBA is a subset of the VB programming language, and it uses the programming structure of VB to manipulate the object-model elements that are exposed by an application. The manipulation of these objects results in small packets of code procedures within the application. These code procedures and resulting projects are called “add­ins.”
VBScript (sometimes referred to as Microsoft Visual Basic, Scripting Edition) is also a subset of the VB programming language. VBScript is a Web-based HTML document scripting language.
How does VBA differ from Java and JavaScript?
VBA is similar to Java and JavaScript® in that it is a high-level, procedural programming language with full garbage collection and very little memory-pointer support. (For more information, see “Allocating memory” on
Macro Programming Guide Understanding automation 7
page 16.) In addition, code that is developed in VBA, much like code developed in Java and JavaScript, supports on-demand compilation and can be executed without being compiled.
VBA is also similar to JavaScript in that it cannot be executed as a stand-alone application. JavaScript is embedded within Web pages as a mechanism for manipulating the document object model (DOM) of the Web browser. Likewise, VBA programs are executed inside a host environment (such as CorelDRAW or Corel PHOTO-PAINT) to manipulate the object model of the host.
Most VBA applications can be compiled to p-code to make them run more quickly, although the difference is hardly noticeable because of the sophistication of today’s computer hardware. Similar compilation is possible with Java, but not with JavaScript.
Finally, whereas VBA uses a single equals sign ( a single equals sign (
= ) for assignment and two equals signs ( == ) for Boolean comparison. (For more
= ) for both comparison and assignment, Java and JavaScript use
information, see “Using Boolean comparison and assignment” on page 17.)
How does VBA differ from C and C++?
Like C and C++, VB uses functions. In VB, functions can be used to return a value, but subroutines cannot be used in that way. However, functions are used in C and C++, regardless of whether you want to return a value. (For more information, see “Using functions and subroutines” on page 15.)
VBA allocates and frees memory transparently. In C and C++, however, the developer is responsible for most memory management. As a result, using strings in VBA is even simpler than using the CString class in C++.
Finally, whereas VBA uses a single equals sign ( single equals sign (
= ) for assignment and two equals signs ( == ) for Boolean comparison. (For more
= ) for both comparison and assignment, C and C++ use a
information,see “Using Boolean comparison and assignment” on page 17.)
How does VBA differ from WSH?
Windows Script Host (WSH) is an out-of-process automation controller that lets you do occasional scripting and automation of Windows tasks and can be used to control software. Although WSH is a useful addition to the Windows operating system, WSH scripts tend to be slow because they must run out of process, and they cannot be compiled (and must be interpreted as they are executed).
WSH is a host for a number of scripting languages, each of which has its own syntax. However, the standard language that WSH uses is a macro language that resembles VB, so for standard scripts, the syntax is the same as in VBA.

What is VSTA?

The successor to VBA, Microsoft Visual Studio Tools for Applications (VSTA) is based on Microsoft Visual Studio 2008. In CorelDRAW and Corel PHOTO-PAINT, the VSTA feature supports the .NET framework and enables development in two programming languages: Visual Basic .NET and Visual C#.
Most of the code examples provided in this documentation are written in VBA.
The VSTA Editor in CorelDRAW and Corel PHOTO-PAINT is an integrated development environment (IDE) that lets you create VSTA solutions for the software.
Macro Programming Guide Understanding automation 8
How does VSTA compare with VBA?
Both VSTA and VBA allow you to create powerful macro solutions. With VSTA, you use the VSTA Editor as an IDE, and you use Visual Basic .NET or Visual C# as a programming language. With VBA, you use the Macro Editor as an IDE, and you use VBA as a programming language.
If you want to perform any of the following tasks, you can use either VSTA or VBA:
• customize or extend the features of the software
• interact with other applications that use Visual Basic 6 — or with other compatible components that are external to the software
• interact with Web-based services
• customize the IDE with add-ins
• create macro projects — with multi-threading support, if desired
• access macro projects programmatically
• generate macro code dynamically
• store macro code in a pre-compiled format
• hide macro code from other macro authors
• debug macro projects
• create customized user interfaces for macro projects
However, if you want to perform any of the following tasks, you must use VSTA:
• access the .NET framework natively — to support using Managed Add-in Framework (MAF), referencing .NET assemblies directly, running customized code on the Common Language Runtime (CLR), enforcing .NET security policies, or creating user interfaces by using .NET WinForms
• fully customize the IDE
• create macro projects that are certified to run on Windows Vista
• create macro projects that support 64-bit processors
• create macro projects that support server-side customizations
• create macro projects that support all data types, including
BigDecimal and Int64
• create macro projects and macro assemblies that persist without the use of structured storage
• open and modify macro projects in Visual Studio
• compile macro projects to DLL assemblies
• run macro projects out of process
• run macro projects without causing the host application to stop execution at errors or breakpoints
• isolate macro projects from one another; run macro projects independently, and stop them during runtime without affecting other running projects
• author macros within managed code
• prevent servers from running customized user interfaces for macro projects
Macro Programming Guide Understanding automation 9

What are the main elements of automation?

If you’ve ever developed object-oriented code in C++, Borland Delphi, or Java, you’re already familiar with programming-related concepts such as “objects,” “classes,” “properties,” and “methods.” However, let’s reexamine these terms as they apply to automating CorelDRAW and Corel PHOTO-PAINT.
In this topic
This topic contains the following subtopics:
• “What is an object model?” on page 10
• “What is a class?” on page 11
• “What is a collection?” on page 11
• “What is a property?” on page 11
• “What is a method?” on page 12
• “What is an event?” on page 12
• “What is an enumeration?” on page 12
• “What is a constant?” on page 12

What is an object model?

An object model represents the hierarchy of items (or “objects”) that make up an application and defines the interrelationships of the objects within that hierarchy. In an object model, each object is a child of another object, which is a child of yet another object, and so on. Furthermore, each object in an object model is defined by a property, a method, or an event, or a combination of these items.
Besides providing a high level of structure, an object model also lets you use object types (or “classes”) in various ways. For example, a Shape object of type “group” is used to contain other Shape objects, each of which is from type “group” or some other type, such as “rectangle,” “curve,” or “text.”
This high level of organization makes the object model easy to use, yet powerful.
How is an object model used in automation?
Automating CorelDRAW or Corel PHOTO-PAINT is accomplished by using the object model of the application to access the various objects in a document and make changes to those objects.
In CorelDRAW and Corel PHOTO-PAINT, the Application object represents the top of the object hierarchy: the program itself. All objects are children or grandchildren (or great-grandchildren, and so on) of the application.
Starting with the Application object, you can “drill down” through the layers of hierarchy in the object model until you find the desired, and usually the more specific, object. To reference the desired object, you must use a standard notation to separate each level of the object hierarchy. As in many object-oriented languages, the automation environment requires the use of a period or “dot operator” (
. ) to indicate that the object on the
right is a member (or child) of the object on the left.
Application.Documents(1).Pages(1).Layers(1).Shapes(1).Name = "ABC"
Macro Programming Guide Understanding automation 10
An object requires its full hierarchical (or “fully qualified”) reference unless a shortcut is available to it (or unless it has an implicit or implied meaning). An object shortcut is merely a syntactic replacement for the long-hand version of an object. For example, the shortcut object
Application.ActiveDocument.ActivePage.ActiveLayer, while the object shortcut ActiveSelection
replaces the long-hand version
Application.ActiveDocument.Selection.
ActiveLayer replaces the long-hand version
For more information on object shortcuts, see “Using object shortcuts” on page 22.

What is a class?

A class is the definition or description of an object. A class outlines the properties, methods, and events that apply to a type of object in an application; it acts as a template for all objects of that type class. To use a metaphor, the class “car” is a small vehicle with an engine and four wheels.
An object is an instance of a class. To extend the car metaphor, the actual, physical car purchased for the purposes of driving is an object (that is, an instance of the class “car”).
In the context of CorelDRAW and Corel PHOTO-PAINT, each open document is an instance of the Document class, each page in the document is an instance of the Page class, and each layer (and each shape on each layer) are more instances of more classes. For example, program. However,
ActiveDocument represents an object within that class because it makes specific reference
to one object.
As previously discussed, objects are often made up of other smaller objects. For example, a car contains four objects of the class “wheel,” two objects of the class “headlight,” and so on. Each of these child objects has the same properties and methods of its class-type. This parent/child relationship of objects is an important one to recognize, particularly when referencing an individual object.
Document represents the Document class in the software
Some classes “inherit” features from their parents. For example, in the context of CorelDRAW and Corel PHOTO-PAINT, the Shape type has many subtypes (or “inherited types”), including Rectangle, Ellipse, Curve, and Te x t . All these subtypes can make use of the basic members of the Shape type, including methods for moving and transforming the shape and for setting its color. However, the subtypes also have their own specialist members; for example, a Rectangle can have corner radii, whereas Te x t has an associated Font property.

What is a collection?

A collection is similar to an array of objects; it is an object that contains a group of objects that are similar in type. These objects share the same properties, methods, and events, and they are uniquely identified within the collection by their index number or their name. Collection objects act in the same manner and are always plural.
For example,
Documents.Item (1) references the first Document object in that collection.
Documents represent the Documents collection class in the software program. However,

What is a property?

A property is like an adjective in that it represents an attribute or characteristic quality of an object. Properties can be returned or set, or they can be read-only.
Most classes have properties. As an illustration, the properties of the class “car” are that it is small, it has an engine, and it has four wheels. Every instance of the class “car” (that is, every object in that class) also has properties such as color, speed, and number of seats. Read-only properties are fixed by the design of the class; for
Macro Programming Guide Understanding automation 11
example, the number of wheels or seats does not (usually) vary from car to car. However, other properties can be changed after the object has been created; for example, the speed of the car can go up and down, and, with a bit of help, its color can be changed.
In the context of CorelDRAW and Corel PHOTO-PAINT, Document objects have a name, a resolution, and horizontal and vertical ruler units; individual shapes have outline properties and fill properties, as well as a position and a rotation factor; and text objects have text properties, which may include the text itself. For example,
ActiveDocument.Name represents the Name property of a Document object; it specifies the name of
the active document.

What is a method?

A method is like a verb in that it represents an action that can be performed by or on an object. In the example of a class “car,” the car can be made to go faster and slower, so two methods for the class are “accelerate” and “decelerate.”
In the context of CorelDRAW and Corel PHOTO-PAINT, documents have methods for creating new pages, layers have methods for creating new shapes, and shapes have methods for applying transformations and effects. For example,
ActiveDocument.Close represents the Close method of a Document object; it closes the active
document.

What is an event?

An event is like a noun in that it represents an action that takes place within an object. An event is triggered by an action, such as a mouse click, a key press, or a system timer. An event can be coded to trigger appropriate response in its object.
For example, the
ActiveDocument.AfterSave event triggers an action in the Document object after it has
been saved.

What is an enumeration?

An enumeration (also called an “enumerated type”) represents a fixed value in the procedures and functions of the coding for a macro. Whereas a variable temporarily stores a changing data value, the value of an enumeration does not change.

What is a constant?

A constant is an instance of an enumeration, and an enumeration groups similar constants together.
For example,
AddinFilterNew.
and
AddinFilter is an enumeration, yet it contains several constants, including AddinFilterNone
Macro Programming Guide Understanding automation 12

How is automation coding structured?

Your programming knowledge should help you learn to automate the software, regardless of your level of experience with Microsoft Visual Basic for Applications (VBA) or Microsoft Visual Studio Tools for Applications (VSTA).
In this topic
This topic contains the following subtopics:
• “Declaring variables” on page 13
• “Using functions and subroutines” on page 15
• “Ending lines” on page 16
• “Including comments” on page 16
• “Allocating memory” on page 16
• “Defining scope” on page 17
• “Using Boolean comparison and assignment” on page 17
• “Using logical and bitwise operators” on page 18
• “Providing message boxes and input boxes” on page 19
• “Referencing objects” on page 19
• “Referencing collections” on page 20
• “Using object shortcuts” on page 22
• “Providing event handlers” on page 23
The Macro Editor formats all VBA coding for you (see “Formatting code automatically” on page 31). The only way to customize the formatting is to change the size of the indentations.
VBA can be used to create object-oriented classes. However, this function is a feature of the programming language and is therefore not discussed in detail in this documentation.

Declaring variables

In VBA, the construction for declaring variables is as follows:
Dim foobar As Integer
The built-in data types are Byte, Boolean, Integer, Long, Single, Double, String, Variant, and several other less-used types including
Variables can be declared anywhere within the body of a function, or at the top of the current module. However, it is generally a good idea to declare a variable before it is used; otherwise, the compiler interprets it as a
Variant, and inefficiencies can be incurred at run time.
Booleans take
Long results in True being converted to a value of –1.
False to be 0 and True to be any other value, although converting from a Boolean to a
To get more information about one of the built-in data types, type it in the Code window of the Macro Editor, select it, and then press F1.
Date, Decimal, and Object.
Macro Programming Guide Understanding automation 13
Data structures can be built by using the following VBA syntax:
Public Type fooType
item1 As Integer
item2 As String
End Type
Dim myTypedItem As fooType
The items within a variable declared as type fooType are accessed by using dot notation:
myTypedItem.item1 = 5
Declaring strings
Using strings is much simpler in VBA than in C. In VBA, strings can be added together, truncated, searched forwards and backwards, and passed as simple arguments to functions.
To add two strings together in VBA, simply use the concatenation operator (
Dim string1 As String, string2 As String
string2 = string1 & " more text" + " even more text"
& ) or the addition operator ( + ):
In VBA, there are many functions for manipulating strings, including InStr(), Left(), Mid(), Right(),
Len(), and Trim().
Declaring enumerations
To declare an enumeration in VBA, use the following construction:
Public Enum fooEnum
ItemOne
ItemTwo
ItemThree
End Enum
By default, the first item in an enumerated type is assigned a value of 0.
Declaring arrays
To declare an array in VBA, use parentheses — that is, the ( and ) symbols:
Dim barArray (4) As Integer
Macro Programming Guide Understanding automation 14
The value defines the index of the last item in the array. Because array indexes are zero-based by default, there are five elements in the preceding sample array (that is, elements
0 thru 4, inclusive).
Arrays can be resized by using
ReDim. For example, the following VBA code adds an extra element to barArray
but preserves the existing contents of the original five elements:
ReDim Preserve barArray (6)
Upper and lower bounds for an array can be determined at run time by using the functions UBound() and
LBound().
Multidimensional arrays can be declared by separating the dimension indexes with commas, as in the following VBA example:
Dim barArray (4, 3)

Using functions and subroutines

VBA uses both functions and subroutines (or “subs”). Functions can be used to return a value, but subs cannot.
In VBA, functions and subs do not need to be declared before they are used, nor before they are defined. In fact, functions and subs need to be declared only if they actually exist in external system DLLs.
Typical functions in a language such as Java or C++ can be structured as follows:
void foo( string stringItem ) {
// The body of the function goes here
}
double bar( int numItem ) { return 23.2; }
In VBA, however, functions are structured as in the following example:
Public Sub foo (stringItem As String)
' The body of the subroutine goes here
End Sub
Public Function bar (numItem As Integer) As Double bar = 23.2
End Function
To force a function or sub to exit immediately, you can use Exit Function or Exit Sub (respectively).
Macro Programming Guide Understanding automation 15

Ending lines

In VBA, each statement must exist on its own line, but no special character is required to denote the end of each line. (In contrast, many other programming languages use a semicolon to separate individual statements.)
To break a long VBA statement over two or more lines, each of the lines (other than the last line) must end with an underscore (
newString = fooFunction ("This is a string", _
5, 10, 2)
_ ) preceded by at least one space:
You can combine several statements in a single VBA line by separating them with colons:
a = 1 : b = 2 : c = a + b
A VBA line cannot end with a colon. VBA lines that end with a colon are labels that are used by the Goto statement.

Including comments

Comments in VBA — similarly to in ANSI, C++, and Java — can be created only at the end of a line. Comments begin with an apostrophe (
Each line of a multi-line comment must begin with its own apostrophe in VBA:
' ) and terminate at the end of the line.
a = b ' This is a really interesting piece of code that
' requires so much explanation that I needed to break
' the comment over multiple lines.
To comment out large sections of VBA code, use the following syntax (similarly to in C or C++):
#If 0 Then ' That's a zero, not the letter 'oh'.
' All this code will be ignored by
' the compiler at run time!
#End If

Allocating memory

VBA does not support C-style memory pointers. Memory allocation and garbage collection are automatic and transparent, just as in Java and JavaScript (and some C++ code).
Passing arguments
Most languages, including C++ and Java, pass an argument to a procedure as a copy of the original. If the original must be passed, then one of two things can happen:
• a memory pointer is passed that directs the procedure to the original argument in memory
• a reference to the original argument is passed
Macro Programming Guide Understanding automation 16
Microsoft Visual Basic (VB) has the same requirements for passing arguments. In VB, passing a copy of the original argument is called “passing by value” and passing a reference to the original is called “passing by reference.”
By default, function and subroutine parameters are passed by reference. A reference to the original variable is passed in the argument of the procedure; changing the value of that argument, in effect, changes the value of the original variable value as well. In this way, more than one value can be returned from a function or subroutine. To explicitly annotate the code to indicate that an argument is being passed by reference, you can prefix the argument with
ByRef.
If you want to prevent a procedure from changing the value of the original variable, you can force the copying of an argument. To do this in VBA, prefix the argument with functionality of
ByRef and ByVal is similar to the ability of C and C++ to pass a copy of a variable, or to pass a
ByVal, as shown in the example that follows. The
pointer to the original variable.
Private Sub fooFunc (ByVal int1 As Integer, _
ByRef long1 As Long, _
long2 As Long) ' Passed ByRef by default
In the preceding VBA example, arguments long1 and long2 are both, by default, passed by reference. Modifying either argument within the body of the function modifies the original variable; however, modifying
int1 does not modify the original because it is a copy of the original.

Defining scope

You can define the scope of a data type or procedure (or even an object). Data types, functions, and subroutines (and members of classes) that are declared as private are visible only within that module (or file). By contrast, functions that are declared as public are visible throughout all the modules; however, you may need to use fully qualified referencing if the modules are almost out of scope — for example, if you are referencing a function in a different project.
Unlike C, VBA does not use braces — that is, the is defined by an opening function or subroutine definition statement (that is,
End statement (that is, End Function or End Sub). Any variables declared within the function are available
{ and } symbols — to define local scope. Local scope in VBA
Function or Sub) and a matching
only within the scope of the function itself.

Using Boolean comparison and assignment

In Microsoft Visual Basic (VB), Boolean comparison and Boolean assignment are both performed by using a single equals sign (
If a = b Then c = d
By contrast, many other languages use a double equals sign for Boolean comparison and a single equals sign for Boolean assignment:
if( a == b ) c = d;
= ):
Macro Programming Guide Understanding automation 17
The following code, which is valid in C, C++, Java, and JavaScript, is invalid in VBA:
if( ( result = fooBar( ) ) == true )
The preceding example would be written in VBA as the following:
result = fooBar( )
If result = True Then
For other Boolean comparisons, VBA uses the same operators as other languages (except for the operators for “is equal to” and “is not equal to”). All the Boolean-comparison operators are provided in the following table.
Comparison VBA operator C-style operator
Is equal to
Is not equal to
Is greater than
Is less than
Is greater than or equal to
Is less than or equal to
The result of using a Boolean operator is always either
===
<> !=
>>
<<
>= >=
<= <=
True or False.

Using logical and bitwise operators

In VBA, logical operations are performed by using the keywords And, Not, Or, Xor, Imp, and Eqv, which perform the logical operations AND, NOT, OR, Exclusive-OR, logical implication, and logical equivalence (respectively). These operators also perform Boolean comparisons.
The following code shows a comparison written in C or a similar language:
if( ( a && b ) || ( c && d ) )
This example would be written as follows in VBA:
If ( a And b ) Or ( c And d ) Then
Alternatively, the preceding VBA code could be written in the following full long-hand form:
If ( a And b = True ) Or ( c And d = True ) = True Then
The following table provides a comparison of the four common VBA logical and bitwise operators, and the C­style logical and bitwise operators that are used by C, C++, Java, and JavaScript.
VBA operator C-style bitwise operator C-style Boolean operator
And & &&
Macro Programming Guide Understanding automation 18
VBA operator C-style bitwise operator C-style Boolean operator
Not ~ !
Or | ||
Xor ^

Providing message boxes and input boxes

In VBA, you can present simple messages to the user by using the MsgBox function:
Dim retval As Long
retval = MsgBox("Click OK if you agree.", _
vbOKCancel, "Easy Message")
If retval = vbOK Then
MsgBox "You clicked OK.", vbOK, "Affirmative"
End If
You can also get strings from the user by using InputBox function:
Dim inText As
inText = InputBox("Input some text:", "type here") If Len(inText) > 0 Then MsgBox "You typed the following: " & inText & "." End If
String
If the user clicks Cancel, the length of the string returned in inText is zero.
For information on creating more complex user interfaces, see “Making macros user-friendly” on page 54.

Referencing objects

If you want to create a reference to an object so that you can treat that reference like a variable (sh, in the following VBA example for CorelDRAW), you can use the
Dim sh As Shape
Set sh = ActiveSelection.Shapes.Item(1)
After you create this reference, you can treat it as though it were the object itself.
sh.Outline.Color.GrayAssign 35
Set keyword.
If the selection is changed while sh is still in scope, sh references the original shape from the old selection and is
Macro Programming Guide Understanding automation 19
unaffected by the new selection. You cannot simply assign the object to the variable as in the following example:
Dim sh As Shape
sh = ActiveSelection.Shapes.Item(1)
To release an object, you must set its reference value to Nothing.
Set sh = Nothing
You can also test whether a variable references a valid object by using the Nothing keyword.
If sh Is Nothing Then MsgBox "sh is de-referenced."
Objects do not need to be explicitly released. In most cases, VB releases the object upon disposal of the variable when you exit the function or subroutine.

Referencing collections

Many objects are members of collections. A collection is similar to an array, except that it contains objects rather than values. However, members of collections can be accessed in the same way as arrays. For example, a collection that is used frequently in CorelDRAW is the collection of shapes on a layer: The object references either the current layer or the layer that is selected in the Object Manager docker.
CorelDRAW contains many collections: A document contains pages, a page contains layers, a layer contains shapes, a curve contains subpaths, a subpath contains segments and nodes, a text range contains lines and words, a group contains shapes, and the application contains windows. All these collections are handled by VBA in the same way.
ActiveLayer
Referencing collection items
To reference the shapes on a layer, the collection of shapes for that layer is used: ActiveLayer.Shapes. To reference the individual shapes in the collection, the
Item() property is used. Here is a VBA example for
CorelDRAW:
Dim sh As Shape
Set sh = ActiveLayer.Shapes.Item(1)
Most elements of a collection start at 1 and increase. For the collection ActiveLayer.Shapes, Item(1) is the item at the “top” or “front” of the layer — in other words, it is the item that is in front of all other shapes. Furthermore, because each item in the
ActiveLayer collection is an object of type Shape, you can reference any
item in VBA merely by appending the appropriate dot-notated member:
ActiveLayer.Shapes.Item(1).Outline.ConvertToObject
Sometimes, individual items have names. If the item you are looking for has an associated name (and you know what the name is and which collection the item is in), you can use that name to reference the item directly, as in the following VBA example for CorelDRAW:
Dim sh1 As Shape, sh2 As Shape
Macro Programming Guide Understanding automation 20
Set sh1 = ActiveLayer.CreateRectangle(0, 5, 7, 0)
sh1.Name = "myShape"
Set sh2 = ActiveLayer.Shapes.Item("myShape")
Also, because an item is usually the implied or default member of a collection, it is not strictly required. For this reason, the last line of the preceding VBA code can be rewritten as follows:
Set sh2 = ActiveLayer.Shapes("myShape")
Counting collection items
All collections have a property called Count. This read-only property gives the number of members in the collection, as in the following VBA example for CorelDRAW:
Dim count As Long
count = ActiveLayer.Shapes.Count
The returned value is not only the number of items in the collection: Because the collection starts from 1, it is also the index of the last item.
Parsing collection items
It is often necessary to parse through the members of a collection to check or change the properties of each item.
By using the
Item() and Count members, it is straightforward to step through a collection of items. With each
iteration, it is possible to test the properties of the current item, or to call its methods. The following VBA code for CorelDRAW restricts all shapes on the layer to no wider than ten units:
Dim I As Long, count As Long
count = ActiveLayer.Shapes.Count
For I = 1 to count
If ActiveLayer.Shapes.Item(i).SizeWidth > 10 Then
ActiveLayer.Shapes.Item(i).SizeWidth = 10
End If
Next I
There is, however, a more convenient way of parsing a collection in VBA. Instead of using the Count property and a For-Next loop, this technique uses a For-Each-In loop:
Dim sh As Shape
For Each sh In ActiveLayer.Shapes
If sh.SizeWidth > 10 Then
Macro Programming Guide Understanding automation 21
sh.SizeWidth = 10
End If
Next sh
If you want to copy the selection and then parse it later when it is no longer selected, copy the selection into a
ShapeRange object:
Dim sr As ShapeRange
Dim sh As Shape
Set sr = ActiveSelectionRange
For Each sh In sr
' Do something with each shape
Next sh

Using object shortcuts

Shortcuts are provided for some frequently accessed objects. Using shortcuts requires less typing, so shortcuts are easier to use than their longhand versions. (Also, using shortcuts can improve run-time performance because the compiler does not need to determine every object in a long dot-separated reference.)
For CorelDRAW, a shortcut can be used on its own as a property of the
Application object. The following
table provides these shortcuts and their long forms. (For a description of any item, see the “Object Model Reference” section of the Macros Help file for CorelDRAW [draw_om.chm].)
Shortcut Long form
ActiveLayer ActivePage.ActiveLayer
ActivePage ActiveDocument.ActivePage
ActiveSelection ActiveDocument.Selection
ActiveSelectionRange ActiveDocument.SelectionRange
ActiveShape ActiveDocument.Selection.Shapes(1)
ActiveView ActiveWindow.ActiveView
ActiveWindow ActiveDocument.ActiveWindow
For Corel PHOTO-PAINT, a shortcut can be used on its own as a property of the Corel PHOTO-PAINT
Application object. The following table provides these shortcuts and their long forms. (For a description of any
item, see the “Object Model Reference” section of the Corel PHOTO-PAINT Macros Help file [pp_om.chm].)
Shortcut Long form
ActiveLayer ActivePage.ActiveLayer
Macro Programming Guide Understanding automation 22
Shortcut Long form
ActiveWindow ActiveDocument.ActiveWindow
For CorelDRAW, the following shortcuts can also be used as members of a given Document object:
• ActiveLayer
• ActivePage
• ActiveShape
• ActiveWindow
For Corel PHOTO-PAINT, the following shortcuts can also be used as members of a given Document object:
• ActiveLayer
• ActiveWindow
For CorelDRAW, the Document object also has the properties Selection and SelectionRange, which let you get the selection or selection range (respectively) from a specified document regardless of whether that document is active.

Providing event handlers

While running, CorelDRAW and Corel PHOTO-PAINT raise various events to which macros can respond through the use of event handlers — subroutines with specific, defined names. Each macro project defines its event handlers in one of the following code modules:
ThisMacroStorage — for macro projects that are stored in Global Macro Storage (GMS) files
ThisDocument — for macro projects that are stored in documents
The GlobalMacroStorage object is a virtual object that represents each and all open documents. The GlobalMacroStorage object has several events that are raised at the time of any event, such as opening, printing, saving, or closing a document (although the range of events is actually greater than this because each one has a “before” and “after” event).
To respond to an event, you must provide an event handler — a subroutine in any ThisMacroStorage module with a specific name for which the application is pre-programmed to search. However, the application does check all ThisMacroStorage modules in all installed projects; for this reason, you can create an event-driven solution and distribute it as a single project file just as you would provide any other solution. Each project can have only one ThisMacroStorage module, and it is automatically created when the project is first created.
In VBA, you can add event handlers to a ThisMacroStorage module by using the Macro Editor. For example, a CorelDRAW macro solution may need to respond to the closing of a document by logging the closure in a file as part of a workflow-management system. To respond to the opening of a document, the solution must respond to
OpenDocument event for the GlobalMacroStorage class. To create this event handler in VBA, do the
the following:
•Open a ThisMacroStorage module for editing in the Macro Editor.
• Next, choose GlobalMacroStorage from the Object list box at the top of the Code window, and then choose DocumentOpen from the Procedure list box.
The Macro Editor creates a new, empty subroutine called
GlobalMacroStorage_DocumentOpen()— or, if that
subroutine already exists, the Macro Editor places the cursor into it. To then add the name of the opened file to the log, you need only write some code. To reduce the size of the ThisMacroStorage module, you can assign this
Macro Programming Guide Understanding automation 23
event-logging task to a public subroutine in another module. This technique lets the run-time interpreter more easily parse all the ThisMacroStorage modules each time an event is raised. The following VBA code illustrates this example for CorelDRAW:
Private Sub GlobalMacroStorage_OpenDocument(ByVal Doc As Document, _
ByVal FileName As String)
Call LogFileOpen(FileName)
End Sub
Here is a small sample of the events available in CorelDRAW:
Event Description
Start
DocumentNew
DocumentOpen
DocumentBeforeSave
DocumentAfterSave
DocumentBeforePrint
DocumentAfterPrint
SelectionChange
DocumentClose
Quit
Event handlers for frequent events — such as events related to the Shape class — should be as efficient as possible, to keep the application running as quickly as possible.
Raised when the user starts the application
Raised when a document is created; passes a reference to the document
Raised when a document is opened; passes a reference to the document
Raised before a document is saved; passes the file name of the document as a parameter
Raised after a document is saved; passes the file name of the document as a parameter
Raised before the Print dialog box is displayed
Raised after a document is printed
Raised when a selection changes
Raised before a document is closed
Raised when the user quits the application
Macro Programming Guide Understanding automation 24

Getting started with macros

Now that you understand a bit about automation, you’re ready to get started with macros. This section provides an overview of the macro-related tools and features of CorelDRAW and Corel PHOTO-PAINT.
In this section
This section contains the following topics:
• “Setting up the automation feature” on page 25
• “Using the Macros toolbar” on page 26
• “Using the Macro Manager docker” on page 27
• “Using the Add-in Manager” on page 28
• “Using the Macro Editor” on page 28
• “Using the VSTA Editor” on page 40

Setting up the automation feature

Before you can develop and run macros in the software, you may need to set up the automation features for VBA and VSTA.
When you perform a “typical installation” of the software, the VBA and VSTA features are installed by default. However, you can manually install them if necessary. You can also specify various options for VBA.
For details on setting up the automation feature, see the following procedures:
• “To install the VBA and VSTA features” on page 25
• “To specify VBA options” on page 25

To install the VBA and VSTA features

1 Access the setup, and follow the on-screen instructions for modifying the software.
2 On the Features page of the setup, enable the following check boxes in the Utilities list box:
• Visual Basic for Applications
• Visual Studio Tools for Applications

To specify VBA options

1 Click Tools ` Options.
2 In the Wo r k sp a ce list of categories, click VBA.
3 In the Security area, specify how to control the risk of running malicious macros by clicking Security
options. If you want to bypass this security feature, enable the Trust all installed GMS modules check box, and then
proceed to step 6.
4 On the Security level page of the Security dialog box, enable one of the following options:
Macro Programming Guide Getting started with macros 25
Ver y hi gh — allows only macros installed in trusted locations to run. All other signed and unsigned macros
are disabled.
High — allows only signed macros from trusted sources to run. Unsigned macros are automatically
disabled.
Medium — lets you choose which macros run, even if they are potentially harmful.
Low (not recommended) — allows all potentially unsafe macros to run. Enable this setting if you have
virus-scanning software installed, or if you check the safety of all documents that you open.
5 On the Trusted publishers page of the Security dialog box, review which macro publishers are trusted. Click
View to display details on the selected macro publisher, or click Remove to delete the selected macro
publisher from the list. If desired, you can enable or disable the Trust access to Visual Basic project check box for the selected
macro publisher.
6 Disable the Delay load VBA check box if you want to load the VBA feature at start-up.

Using the Macros toolbar

CorelDRAW and Corel PHOTO-PAINT feature a Macros toolbar that provides easy access to several macro­related featues, such as the Macro Editor.
The Macros toolbar in CorelDRAW
The Macros toolbar in Corel PHOTO-PAINT
Using the Macros toolbar in CorelDRAW
In CorelDRAW, the Macros toolbar features the following buttons:
Macro Manager button — opens the Macro Manager docker
Run Macro button — runs a macro
Macro Editor button — opens the Macro Editor
Disable application events button — switches the Macro Editor between its modes for designing and running macros
Start Recording button — records a macro
Pause Recording button — pauses the recording of a macro
Stop Recording button — stops the recording of a macro
To d is pl a y t h e Macros toolbar in CorelDRAW, click Window ` To o l b a r s ` Macros. A check mark next to the command indicates that the toolbar is displayed.
Macro Programming Guide Getting started with macros 26
Loading...
+ 130 hidden pages