Corel Corel Draw Graphics Suite - X3 Programmming Guide for VBA

5 (1)
Table of contents
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
About Corel Corporation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
About CorelDRAW Graphics Suite X3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
About VBA in CorelDRAW Graphics Suite X3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
About this manual . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Chapter 1
What is VBA? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
What is automation? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Who is VBA designed for?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
How does VBA compare with other programming languages?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
What are the main elements used in VBA? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
What is an object model? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7
How is VBA code structured? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Declaring variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9
Building functions and subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10
Ending lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Including comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Using memory pointers and memory allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11
Defining scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12
Using Boolean comparison and assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12
Using logical and bitwise operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13
Providing message boxes and input boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13
Chapter 2
Getting started with VBA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Installing VBA for CorelDRAW Graphics Suite X3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Using the VBA toolbars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Using the VBA toolbar in CorelDRAW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Using the VB Editor toolbar in Corel PHOTO-PAINT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Using the VB Editor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Using the Project Explorer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17
Using the Properties window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18
Using the Code window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Using the toolbars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Using the Object Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23
CorelDRAW Graphics Suite X3 Programming Guide for VBA i
Chapter 3
Working with macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Creating macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Writing macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Recording macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28
Referencing objects in macros. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Referencing collections in macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .30
Using shortcut objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32
Providing event handlers in macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Running macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Debugging macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Using the debugging windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36
Chapter 4
Creating user-interfaces for macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Creating dialog boxes for macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Coding dialog boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Designing dialog boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Creating toolbars and buttons for macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Creating toolbars for macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Adding captions and tooltips to macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .47
Associating images or icons with macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Providing user interaction for macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Capturing mouse actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48
Capturing coordinates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Providing help for macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Chapter 5
Organizing and deploying macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Organizing macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Deploying macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Deploying project files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Deploying workspaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Appendix
Understanding the CorelDRAW object model . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Working with documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Creating documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Opening documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Importing files into documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Switching between documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .58
Viewing documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .59
Changing content in documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Setting the Undo string for documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .61
Exporting files from documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Printing documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Publishing documents to PDF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Closing documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Table of contents ii
Working with pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Creating pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .65
Switching between pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66
Reordering pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .67
Resizing pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Deleting pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .68
Working with layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Creating layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .69
Activating layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Locking and hiding layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .69
Reordering layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .69
Renaming layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Importing files into layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Deleting layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Working with shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Creating shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Selecting shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Determining shape type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .79
Changing shape properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .79
Coloring shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Duplicating shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Applying effects to shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87
Glossary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Table of contents iii

Introduction

Welcome to the CorelDRAW® Graphics Suite X3 Programming Guide for VBA, your resource for developing and distributing Microsoft® Visual Basic® for Applications (VBA) solutions in CorelDRAW Graphics Suite X3.

About Corel Corporation

Corel Corporation provides innovative software solutions that help millions of value-conscious businesses and consumers in more than 75 countries improve their productivity. The company is renowned for its powerful software portfolio, which combines innovative photo-editing, graphics-creation, vector-illustration, and technical-graphics applications with office and personal productivity solutions. Corel’s flagship products include CorelDRAW Graphics Suite, WordPerfect® Office suite, Corel® Paint Shop Pro®, Corel® Painter™, and Corel DESIGNER® Technical Suite. For more information, please visit www.corel.com.

About CorelDRAW Graphics Suite X3

CorelDRAW Graphics Suite X3 is a comprehensive vector-based drawing program that makes it easy to create professional artwork, from simple logos to technical illustrations. The tools in CorelDRAW are designed to meet the demands of the graphic designer.

About VBA in CorelDRAW Graphics Suite X3

In 1995, Corel incorporated automation into CorelDRAW 6 by including its Corel SCRIPT™ language. This enabled solution developers to create intelligent mini-applications within CorelDRAW, such as ones that draw shapes, reposition and resize shapes, open and close documents, or set styles.
Corel SCRIPT was included with CorelDRAW versions 6 through 9. Although the Corel SCRIPT editor is not included with CorelDRAW in versions 9 and later, the run-time engine is included, so scripts written for earlier versions of CorelDRAW can easily be migrated to the latest versions.
In 1998, Corel took the strategic decision to augment the Corel SCRIPT functionality of CorelDRAW 9 by licensing the Microsoft Visual Basic for Applications engine to handle its behind-the-scenes automation. The addition of VBA made CorelDRAW immediately accessible to millions of VBA and Visual Basic developers around the world.
CorelDRAW Graphics Suite X3 Programming Guide for VBA 1
VBA in CorelDRAW can be used as a platform for developing powerful corporate graphical solutions, such as automated ticket generators, customized calendars, or batch-processors of files. VBA can also be used to enhance and optimize the workflow within CorelDRAW. For example, you can improve and customize some of the built­in functionality of CorelDRAW (such as object creation, alignment, or transformation) or add page layouts on­the-fly (such as to create company letterheads).
VBA comes with a fully integrated development environment (IDE) that provides contextual pop-up lists, syntax highlighting, line-by-line debugging, and visual designer windows. These helpful prompts and aids create a particularly friendly learning environment for inexperienced developers.
For CorelDRAW Graphics Suite X3, VBA is available to both CorelDRAW and Corel PHOTO-PAINT®.

About this manual

This manual, which should be read in conjunction with the CorelDRAW and Corel PHOTO-PAINT object model diagrams that install with CorelDRAW Graphics Suite X3, was designed as a resource for the following:
• exploring the VBA IDE and many of its advanced features
• understanding the most important CorelDRAW Graphics Suite X3 functions and how to use them
• examining how to package and deploy VBA solutions developed for CorelDRAW Graphics Suite X3
This manual should be used by anyone who is interested in automating simple and complex tasks in CorelDRAW Graphics Suite X3 or who is developing commercial solutions that integrate with CorelDRAW Graphics Suite X3. It is assumed that the reader already has experience with at least one other procedural programming language, such as BASIC, Visual Basic, C, C++, Java™, Pascal, Cobol, or Fortran. This manual does not describe the basics of procedural programming, such as functions, conditional branching, and looping. Non-programmers should learn the basics of programming in a language such as Visual Basic or VBA before using this document to develop CorelDRAW Graphics Suite X3 solutions.
This manual is organized into the following chapters, which deal with specific aspects of automating tasks and building solutions in CorelDRAW Graphics Suite X3:
Chapter 1: Understanding VBA provides a brief introduction to VBA.
Chapter 2: Getting started with VBA lets you explore the VBA workspace in CorelDRAW and Corel PHOTO-PAINT.
Chapter 3: Wo r k in g w i th ma c r o s shows you how to create, run, and debug macros.
Chapter 4: Creating user-interfaces for macros demonstrates how to provide dialog boxes, toolbars and buttons, user interaction, and help for your macros.
Chapter 5: Organizing and deploying macros helps you organize and deploy the macros you create.
This manual also provides an appendix (page 55), which provides detailed information on the CorelDRAWobject model.
Finally, a glossary (page 88) defines the terms used in this manual.
Documentation conventions
The following conventions are used in this manual:
Introduction 2
When you see This is
A note — presents information such as conditions for performing a procedure
A tip — presents information such as procedure shortcuts, variations, or benefits
In addition, this manual uses a few formatting conventions:
• User-interface items are displayed in boldface.
• Glossary items are displayed in italics.
• Information that can be substituted, such as a path or filename, is displayed in <italics and between angle brackets>.
• Coding is formatted in a monospace font.
For more information
VBA for CorelDRAW Graphics Suite X3 also installs with the following documentation:
• VBA object model diagrams for CorelDRAW and Corel PHOTO-PAINT — These are available as Adobe® Acrobat® Reader® (PDF) files and are installed to ..\Program Files\Corel\ CorelDRAW Graphics Suite 13\Programs.
• VBA Help files for CorelDRAW and Corel PHOTO-PAINT — These can be accessed through the VB Editor: select any item in the Object Browser and press F1 to access a Help topic on that item.
For comprehensive information about the features in CorelDRAW Graphics Suite X3, you can consult the user
guide, which is installed by default to the ..\Program Files\Corel\CorelDRAW Graphics Suite 13\
Languages\<language>\Help folder. Alternatively, you can access a Help system from within a CorelDRAW Graphics Suite X3 application by clicking Help ` Help topics.
For the latest news, tips and tricks, and product upgrade information, you can check the CorelDRAW Graphics Suite X3 Web site. Go to www.corel.com, and follow the links for CorelDRAW Graphics Suite X3.
For prompt and accurate information about product features, specifications, pricing, availability, services, and technical support, contact Corel Support Services. For the most current information on available support and professional services for your Corel product, please visit www.corel.com/support.
If you have any comments or suggestions about CorelDRAW Graphics Suite X3 or about the CorelDRAW Graphics Suite X3 Programming Guide for VBA, you can submit them by using the contact information provided at
www.corel.com/contact.
Introduction 3
Chapter 1 Understanding VBA
Before getting started with VBA in CorelDRAW Graphics Suite X3, it’s important to understand a little bit about VBA in general.
This chapter answers the following questions:
• What is VBA?
• Who is VBA designed for?
• How does VBA compare with other programming languages?
• What are the main elements used in VBA?
• How is VBA code structured?

What is VBA?

Visual Basic for Applications (more commonly known as VBA) is a built-in programming language that can automate repetitive functions and create intelligent solutions in CorelDRAW and Corel PHOTO-PAINT. CorelDRAW Graphics Suite X3 includes VBA version 6.3.
VBA is both a language and an editor. It is not possible to have the language without the editor, nor is it possible to edit VBA in anything but the VB Editor or to run VBA programs without the VB Editor.
VBA is developed by Microsoft and is built into almost all of its desktop applications, including Microsoft® Office. VBA is licensed by Microsoft to other companies, including Corel Corporation (in CorelDRAW Graphics Suite, Corel DESIGNER® Technical Suite, and WordPerfect Office), Autodesk, Inc. (in AutoCAD®), and IntelliCAD Technology Consortium (in IntelliCAD®). This makes Corel applications compatible with a wide array of applications that support VBA.
For a complete list of applications that support VBA, consult the Microsoft Web site at http://
www.msdn.microsoft.com/vba/companies/company.asp.
It is not necessary for an application to support VBA in order for the CorelDRAW Graphics Suite X3 VBA engine to control that application. That means you can build solutions in CorelDRAW Graphics Suite X3 that access databases, word processors, specialized content editors, XML documents, and more.

What is automation?

Most actions that you can do in CorelDRAW Graphics Suite X3 can be done programmatically through VBA. This programmability of CorelDRAW Graphics Suite X3 is called automation. Automating repetitive tasks can save time and reduce effort, while automating complex tasks can make possible the otherwise impossible.
CorelDRAW Graphics Suite X3 Programming Guide for VBA 4
In its simplest form, automation is simply recording a sequence of actions so that you can play them back time and again. The term macro has come to include any code that is accessible to VBA while running within the process, even though some of that code might be far more advanced than a mere set of recorded actions. For the purposes of this manual, a macro refers to VBA functions and subroutines (which are explained in “Building functions and subroutines” on page 10).
While it is possible to record a sequence of actions in CorelDRAW Graphics Suite X3, the real power of automation and VBA is that these recordings can be edited to provide conditional and looping execution. For example, a simple macro may set the selected shape’s fill color to red and apply a one-point outline; however, by adding a condition and a looping mechanism to the VBA code, the macro could, for example, be made to seek out each selected shape and apply only the fill to text shapes and only the outline to all other shape types.

Who is VBA designed for?

VBA can be used by both non-programmers and programmers alike.
VBA for non-programmers
VBA is based on Microsoft’s successful Visual Basic (VB) programming language. The main difference between VBA and VB is that you cannot create stand-alone executable (EXE) files using VBA, whereas you can with VB. That is to say, using VBA, you can create only programs that run inside the host application (in this case, CorelDRAW or Corel PHOTO-PAINT).
VB is a “visual” version of the BASIC programming language. This means that it is a very easy language to learn, particularly because it provides visual cues within the editor. Microsoft has added a great deal to the original BASIC language, and it is now a powerful and fast language (although not as powerful as Java or C++, nor as quick as C).
The aim of this manual is not to teach you how to become a programmer but instead to teach experienced programmers how to apply their skills to developing useful solutions within CorelDRAW Graphics Suite X3. If you are not a programmer, you may find it useful to refer to the many books that have been written about VBA and VB before continuing to read this manual.
VBA for programmers
VBA is an in-process automation controller. In other words, it can be used to control the functionalities of CorelDRAW Graphics Suite X3 that can be automated. In addition, because VBA runs “in-process,” it bypasses the interprocess synchronization mechanisms so as to run much more efficiently.
All of the automation that is available to the in-process VBA is also available to external out-of-process automation controllers, or OLE clients. This includes applications developed in programming languages that can be used to develop OLE clients, such as the following:
• Microsoft Visual Basic, Visual C++, and Windows® Script Host
• Borland® Delphi™ and C++
• the VBA engines of other applications
Chapter 1: Understanding VBA 5

How does VBA compare with other programming languages?

VBA has many similarities with most modern, procedural programming languages, including Java and JavaScript®, C and C++, and Windows Script Host. However, VBA runs as an in-process automation controller, whereas the other languages (apart from JavaScript) are used to compile stand-alone applications.
VBA compared with Java and JavaScript
VBA is similar to Java and JavaScript in that it is a high-level, procedural programming language that has full garbage collection and very little memory-pointer support. (See “Using memory pointers and memory allocation” on page 11 for more information.) In addition, code developed in VBA — much like code developed in Java and JavaScript — supports on-demand compilation and can be executed without being compiled.
VBA has another similarity with JavaScript in that it cannot be executed as a standalone application. JavaScript is embedded within Web pages, as a mechanism for manipulating the Web browser’s document object model (or “DOM”). Likewise, VBA programs are executed inside a host environment (in this case, CorelDRAW or Corel PHOTO-PAINT) so as to manipulate the host’s object model (which is discussed in “What is an object model?” on page 7).
Most VBA applications can be compiled to P-code so as to make them run more quickly, although the difference is hardly noticeable given the sophistication of today’s computer hardware. Java can be similarly compiled; JavaScript, however, cannot.
Finally, whereas VBA uses a single equals sign ( single equals sign (
=) for assignment and two equals signs (==) for Boolean comparison. (For more information on
=) for both comparison and assignment, Java and JavaScript use a
Boolean comparison and assignment in VBA, see “Using Boolean comparison and assignment” on page 12.)
VBA compared with C and C++
Visual Basic — similarly to C and C++ — uses functions. In VB, functions can be used to return a value but subroutines cannot. In C and C++, however, functions are used regardless of whether you want to return a value. (For more information on functions and subroutines, see “Building functions and subroutines” on page 10.)
VBA allocates and frees memory “transparently.” In C and C++, however, the developer is responsible for most memory management. This makes using strings in VBA even simpler than using the
Finally, whereas VBA uses a single equals sign ( equals sign (
=) for assignment and two equals signs (==) for Boolean comparison. (For more information on
=) for both comparison and assignment, C and C++ use a single
CString class in C++.
Boolean comparison and assignment in VBA, see “Using Boolean comparison and assignment” on page 12.)
VBA compared with Windows Script Host
Windows Script Host (WSH) is a useful addition to Windows for doing occasional scripting and automation of Windows tasks. WSH is an out-of-process automation controller that can be used to control CorelDRAW Graphics Suite X3. However, because WSH scripts cannot be compiled (and must be interpreted as they are executed) and must be run out of process, they tend to be slow.
WSH is a host for a number of scripting languages, each of which has its own syntax. However, the standard language used by WSH is a macro language resembling Visual Basic, so for standard scripts, the syntax is the same as in VBA.
Chapter 1: Understanding VBA 6

What are the main elements used in VBA?

If you’ve ever developed object-oriented code in C++, Borland Delphi, or Java, you’re already familiar with the concepts of “classes,” “objects,” “properties,” and “methods,” but let’s re-examine them in greater detail as they apply to VBA.
A class is a description of something. For example, the class “car” is a small vehicle with an engine and four wheels.
An object is an instance of a class. If we extend the car metaphor, then the actual, physical car that you go out and purchase for the purposes of driving is an object (that is, an instance of the class “car”). In the context of CorelDRAW, each open document is an instance of the instance of the
Page class, and each layer (and each shape on each layer) are more instances of more classes.
Most classes have properties. For example, 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. Some properties, called “read-only” properties, are fixed by the design of the class; for 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, name, a resolution, and horizontal and vertical ruler units; individual shapes have outline and fill properties, as well as a position and a rotation factor; and text objects have text properties, which may include the text itself.
A method is an operation that the object can have performed on itself. In the example of the 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, documents have methods for creating new pages, layers have methods for creating new shapes, and shapes have methods for applying transformations and effects.
Document class, each page in the document is an
Document objects have a
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. In the context of CorelDRAW, a document object contains page objects, which contain layer objects, which contain shape objects, some of which contain other objects. This parent/child relationship of objects is an important one to recognize, particularly when referencing an individual object.
Some classes “inherit” features from their parents. For example, in the context of CorelDRAW, the has many subtypes (or “inherited types”), including subtypes can make use of the basic members of the
Rectangle, Ellipse, Curve, and Text. All of these
Shape type, including methods for moving and transforming
Shape type
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 Text has an associated Font property.

What is an object model?

VBA relies on an application’s object model for communicating with that application and modifying its documents. Without an object model, VBA cannot query or change an application’s documents.
Object models in software provide a high level of structure to the relationship between parent and child objects. They also allow object types or classes to be used repeatedly in different ways; for example, a be of the type “group” and may contain other
Shape objects, some of which may also be of the type “group,” or
Shape object may
Chapter 1: Understanding VBA 7
“rectangle,” or “curve,” or “text.” This high level of organization and re-use makes the object model simple to use and yet powerful — and also easy to navigate by using the VBA Object Browser (which is discussed in “Using the Object Browser” on page 23).
Remember, though, that the object model is the map that the VBA language uses to access the various members — objects, methods, and properties — of a document, and to make changes to those members. Without the object model, it is simply impossible to gain access to the objects in the document.
Understanding object hierarchy
In any object model, each object is a child of another object, which is a child of another object. Also, each object has child members of its own — properties, objects, and methods. All of this comprises an object hierarchy that is the object model. In CorelDRAW, the root object of all objects is the
Application object, because all objects
are children or grandchildren of the application.
In order to “drill down” through the layers of hierarchy to get to the object or member that you want, you must use a standard notation. In VBA, as in many object-oriented languages, the notation is to use a period (
. ) 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 = "Bob"
It is not usually necessary to use the full hierarchical (or fully qualified) reference to an object or its properties. Some of the object-syntax in the fully qualified reference is mandatory or required; however, other syntax is optional (because a shortcut object for it is available, or because it is implicit or implied), and so it can either be included for clarity or omitted for brevity.
A shortcut object is merely a syntactical replacement for the long-hand version of the object. For example, the shortcut object
Application.ActiveDocument.ActivePage.ActiveLayer, while the object shortcut ActiveSelection
replaces the long-hand version
ActiveLayer replaces the long-hand version
Application.ActiveDocument.Selection. For information on the shortcut
objects that are available to CorelDRAW, see “Using shortcut objects” on page 32.
For detailed information on the CorelDRAW object model, see the Appendix.

How is VBA code structured?

Because VBA is a procedural language that shares much in common with all procedural languages, your current knowledge should help you get off to a quick start with VBA.
This section examines the following topics on VBA structure and syntax:
• Declaring variables
• Building functions and subroutines
• Ending lines
• Including comments
• Using memory pointers and memory allocation
• Defining scope
• Using Boolean comparison and assignment
• Using logical and bitwise operators
• Providing message boxes and input boxes
Chapter 1: Understanding VBA 8
The VB Editor formats all of the code for you (as discussed in “Formatting code automatically” on page 20). The only custom formatting that you can do is to change the size of the indentations.
VBA can create object-oriented classes, although these are a feature of the language and are not discussed in detail in this manual.

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 practice to declare a variable before it is used; otherwise, the compiler interprets it as a
Variant, and inefficiencies can be incurred at run time.
Date, Decimal, and Object.
Booleans take a
Long results in True being converted to a value of –1.
False to be zero and True to be any other value, although converting from a Boolean to
To get more information about one of the built-in data types, type it into the code window, select it, and then press F1.
Data structures can be built by using the following 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 using dot notation:
myTypedItem.item1 = 5
Declaring strings
Strings in VBA are much simpler 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, 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 enumerated types
To declare an enumerated type, use the following construction:
Chapter 1: Understanding VBA 9
Public Enum fooEnum ItemOne ItemTwo ItemThree End Enum
The first item in an enumerated type is assigned, by default, a value of zero.
Declaring arrays
To declare an array, use parentheses — that is, the ( and ) symbols:
Dim barArray (4) As Integer
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 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 with the functions UBound() and
LBound().
Multidimensional arrays can be declared by separating the dimension indexes with commas:
Dim barArray (4, 3)

Building functions and subroutines

VBA uses both functions and subroutines (or “subs”). Functions can be used to return a value, whereas 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 dynamic-linked libraries (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).
Chapter 1: Understanding VBA 10

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. (This is in contrast to the many programming languages that use the 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:
It is also possible to combine several statements in a single line by separating them with colons:
a = 1 : b = 2 : c = a + b
A line cannot end with a colon. Lines that end with a colon are labels 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:
a = b ' This is a really interesting piece of code that ' needs so much explanation that I have had to break ' the comment over multiple lines.
' ) and terminate at the end of the line.
To comment out large sections of code, use the following code (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

Using memory pointers and memory allocation

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 values “by reference” and “by value”
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 to the original in memory
• a reference to the original is passed
The same is true in VB, except that passing a copy of the original is called passing by value and passing a reference to the original is called passing by reference.
Chapter 1: Understanding VBA 11
By default, function and subroutine parameters are passed by reference. This means that a reference to the original variable is passed in the procedure’s argument, and so changing that argument’s value within the procedure, in effect, changes the original variable’s value as well. This is a great way of returning more than one value from a function or sub. 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 the procedure from changing the value of the original variable, you can force the copying of an argument. To do this, prefix the argument with
ByVal functionality is similar to the ability of C and C++ to pass a copy of a variable, or to pass a pointer to the
ByVal, as shown in the example that follows. This ByRef/
original variable.
Private Sub fooFunc (ByVal int1 As Integer, _ ByRef long1 As Long, _ long2 As Long) ' Passed ByRef by default
In the preceding 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

Yo u c an d ef in e t h e scope of a data type or procedure (or even an object). Data types, functions, and subs (and members of classes) that are declared as private are visible only within that module (or file), while functions that are declared as public are visible throughout all the modules; however, you may have 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 sub definition statement (that is, statement (that is,
End Function or End Sub). Any variables declared within the function are available only
{ and } symbols — to define local scope. Local scope in VBA
Function or Sub) and a matching End
within the scope of the function itself.

Using Boolean comparison and assignment

In VB, Boolean comparison and assignment are both performed by using a single equals sign (=):
If a = b Then c = d
This is in contrast to many other languages that use a double equals sign for a Boolean comparison and a single equals sign for assignment:
if( a == b ) c = d;
The following code, which is valid in C, C++, Java, and JavaScript, is invalid in VBA:
if( ( result = fooBar( ) ) == true )
This would have to be written in VBA as the following:
result = fooBar( ) If result = True Then
Chapter 1: Understanding VBA 12
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 one of the Boolean operators 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 would be written as follows in VBA:
If ( a And b ) Or ( c And d ) Then
Alternatively, the above 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 used by C, C++, Java, and JavaScript:
VBA operator C-style bitwise operator C-style Boolean operator
And & &&
Not ~ !
Or | ||
Xor ^

Providing message boxes and input boxes

You can present simple messages to the user by using the MsgBox function:
Dim retval As Long retval = MsgBox("Click OK if you agree.", _
Chapter 1: Understanding VBA 13
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 String inText = InputBox("Input some text:", "type here") If Len(inText) > 0 Then MsgBox "You typed the following: " & inText & "." End If
If the user clicks Cancel, the length of the string returned in inText is zero.
Chapter 1: Understanding VBA 14
Chapter 2 Getting started with VBA
Now that you understand a bit about VBA, you’re ready to get started with macros.
This chapter covers the following topics:
• Installing VBA for CorelDRAW Graphics Suite X3
• Using the VBA toolbars
• Using the VB Editor

Installing VBA for CorelDRAW Graphics Suite X3

Before you can develop and run macros in CorelDRAW Graphics Suite X3, you may need to install the VBA feature.
When you perform a “typical installation” of CorelDRAW Graphics Suite X3, VBA is installed by default.
To install VBA
1 Insert CorelDRAW Graphics Suite X3 Disc 1 into the CD drive.
If the installation wizard does not start automatically, click Start ` Run on the Windows taskbar, and then type X:\CGS13\Setup.exe (where X is the letter that corresponds to the CD drive).
2 Click Install CorelDRAW Graphics Suite X3, and follow the on-screen instructions.
3 On the Select which applications you would like to install page, click Advanced options, and then in the
Custom setup window that appears, set Utilities\Visual Basic for Applications 6.3 to install.
Set Utilities\Visual Basic for Applications 6.3 to install
CorelDRAW Graphics Suite X3 Programming Guide for VBA 15

Using the VBA toolbars

CorelDRAW and Corel PHOTO-PAINT feature a toolbar that lets you access common VBA functionalities.

Using the VBA toolbar in CorelDRAW

CorelDRAW has a toolbar that provides easy access to several VBA features and to the VB Editor.
The Visual Basic for Applications toolbar in CorelDRAW
The toolbar buttons provide the following functions:
•playing macros
•opening the VB Editor
• switching the VB Editor between its modes for designing and running macros
• recording macros
• pausing the recording of macros
• stopping the recording of macros
To display the Visual Basic for Applications toolbar in CorelDRAW
Click Window ` To o l b a r s ` Visual Basic for Applications.
A check mark next to the command indicates that the toolbar is displayed.

Using the VB Editor toolbar in Corel PHOTO-PAINT

Corel PHOTO-PAINT has a toolbar that provides easy access to the VB Editor.
The Visual Basic Editor toolbar in Corel PHOTO-PAINT
The toolbar buttons provide the following functions:
•playing macros
•opening the VB Editor
• switching the VB Editor between its modes for designing and running macros
To display the VB Editor toolbar in Corel PHOTO-PAINT
Click Window ` To o l b a r s ` Visual Basic Editor.
A check mark next to the command indicates that the toolbar is displayed.
Chapter 2: Getting started with VBA 16

Using the VB Editor

The VB Editor that is included with VBA is similar to the one included with full Visual Basic.
The VB Editor lets you develop code and dialog boxes, browse the object tree and the modules within each project, set individual properties for objects, and debug code. However, it’s important to note that the VB Editor for VBA cannot compile executable (EXE) program files.
The VB Editor features several windows and toolbars, all of which are discussed in this section. The three available windows are the Project Explorer (see page 17), the Properties window (see page 18), and the Code window (see page 19). The four available toolbars (see page 22), are the Standard toolbar, the Debug toolbar, the Edit toolbar, and the UserForm toolbar, of which you will use the Standard and Debug toolbars most often.
Standard
toolbar
Project
Explorer
Code
window
Debug
toolbar
Edit
toolbar
Properties
window
The VB Editor
The VB Editor also lets you access the Object Browser (see page 23).
You can invoke the VB Editor from within CorelDRAW or Corel PHOTO-PAINT. Although this starts VBA as a new application in Windows, it runs within the CorelDRAW or Corel PHOTO-PAINT process.
UserForm
toolbar
To start t he VB Edi tor
Click To o l s ` Visual Basic ` Visual Basic Editor, or press Alt + F11.
To switch between the VB Editor and CorelDRAW or Corel PHOTO-PAINT, use the Windows taskbar, or press Alt + F11 or Alt + Tab.

Using the Project Explorer

The Project Explorer is essential for navigating VBA projects and their constituent documents/objects, forms, modules, and class modules.
Chapter 2: Getting started with VBA 17
The Project Explorer with a module selected
Each type of item in the Project Explorer has an icon assigned to it:
Icon Meaning
project
folder
document/object
form
module
class module
To display or hide the Project Explorer
Click View ` Project Explorer, or press Ctrl + R.

Using the Properties window

The Properties window lists all of the editable properties for the currently selected object. Many objects in VBA — including projects, modules, and forms and their controls — have property sheets that can be modified.
Chapter 2: Getting started with VBA 18
The Properties window, showing the properties of a form
The Properties window is automatically updated when you select an object, or when you change the properties of the selected object by using other methods (for example, by using the mouse to move and resize form controls).
To display or hide the Properties window
Click View ` Properties window, or press F4.

Using the Code window

The Code window is where you spend most of your time when working on macros. A standard code editor in the style of Microsoft® Visual Studio®, the Code window lets you format code automatically, color and check syntax automatically, jump to definitions, and use contextual pop-up lists and automatic completion.
If you are already familiar with any of the Microsoft Visual Studio editors, the VB Editor’s Code window will be entirely familiar to you.
The VBA Code window
Chapter 2: Getting started with VBA 19
Formatting code automatically
The VB Editor formats code automatically for you — even the capitalization of keywords, functions, subroutines, and variables is taken care of by the VB Editor, irrespective of what you type.
You cannot custom-format code, although you can set the indentation for each line, as well as the placing of custom line breaks.
When it comes to calling functions and subs, you must adhere to the following rules:
• If you are calling a function and you are using the returned value, the parentheses around the parameters are mandatory (just as in most modern programming languages):
a = fooFunc (b, c)
• However, if the returned value from a function call is being discarded, or if you are calling a sub, the parentheses must be left out (unlike in most other languages):
barFunc d, e fooBarSub f
• If you prefer always to see the parentheses, use the Call keyword before the function or sub call:
Call barFunc (d, e) Call fooBarSub (f)
Coloring syntax automatically
When you develop code in the Code window, the editor colorizes each word according to its classification:
•VBA keywords and programming statements are usually displayed in blue.
• Comments are displayed in green.
• All other text is displayed in black.
This colorization makes the code much easier to read.
Syntax highlighting and coloring
The Code window also uses the following colorization techniques:
• Lines of code containing errors are displayed in red.
• Selected text is white on blue.
• The line where execution paused for debugging is shown as a yellow highlight.
• Breakpoints that you set for debugging purposes are shown as a red dot in the left margin with the code in white on a red background.
Chapter 2: Getting started with VBA 20
• Bookmarks (which you set in the code) are indicated by a blue dot in the left margin.
Breakpoints (along with bookmarks) are lost when you quit the application. For more information on them, see “Setting breakpoints” on page 35.
You can modify the default colors for syntax highlighting by clicking Tools ` Options, clicking the Editor format tab, and making your changes.
Checking syntax automatically
Every time you move the cursor out of a line of code, the editor checks the syntax of the code in that line; if it finds an error, it changes the color of the text of that line to red and displays a pop-up a warning. This real-time checking is useful (particularly when you are learning VBA) because it indicates many possible errors in the code without having you run the code.
You can disable pop-up warnings by clicking Tools ` Options, clicking the Editor tab, and then disabling the Auto syntax check check box. The VB Editor still checks the syntax and highlights erroneous lines in red, but it stops displaying a warning when you paste text from another line of code.
Jumping to definitions
You can jump directly to the definition of a variable, procedure, or object by right-clicking the item in the Code window and then clicking Definition. This takes you either to the definition of the variable or function in the code or to the object’s definition in the Object Browser.
To return to where you requested the definition, right-click, and then click Last position in the Code window.
Using contextual pop-up lists and automatic completion
As you write procedures and define variables, the VB Editor adds these items to an internal list that already contains all of its built-in keywords and enumerated values. As you type, the VB Editor presents you with a list of candidate words that you may want to insert at the current position; this list is contextual, so the VB Editor usually presents only the words that are valid for the current position.
Auto-completion pop-up menu
Chapter 2: Getting started with VBA 21
This list makes code development quicker and more convenient, particularly because you do not need to remember every function and variable name but can instead choose them from the list provided. If you type the first few characters of the word you want to use, the list advances to the nearest candidate that matches the characters you’ve entered. Select the word you want to use, and either type the character that you want to have follow the word (typically a space, line feed, parenthesis, period, or comma) or press Ta b or Ctrl + Enter to enter only the word.
To force the pop-up menu display, you can press Ctrl + Spacebar. The menu scrolls to the word that most closely matches the characters that you have typed so far. This technique is also useful for filling parameter lists when calling a function or subroutine. If there is only one exact match, the VB Editor inserts the word without popping up the list. To display the pop-up list for the selected keyword at any time without auto-filling it, press Ctrl + J.

Using the toolbars

The VB Editor features four toolbars that you can use to carry out your VBA tasks.
The Standard toolbar is the default toolbar.
The Standard toolbar
The Debug toolbar contains buttons for common debugging tasks (as discussed in “Debugging macros” on page 35).
The Debug toolbar
The Edit toolbar contains buttons for common editing tasks.
The Edit toolbar
The UserForm toolbar contains buttons specific to designing forms (as discussed in “Designing dialog boxes” on page 42).
The UserForm toolbar
You can choose to display or hide each toolbar.
To display or hide a toolbar
Click View ` To o l b a r s , and then click the command that corresponds to the toolbar you want to display or
hide.
A check mark next to a command indicates that its toolbar is currently displayed.
Chapter 2: Getting started with VBA 22
You can “float” a toolbar by dragging it from the menu bar.
You can dock a toolbar by dragging it to the menu bar.

Using the Object Browser

The Object Browser is one of the most useful tools provided by the VB Editor. The Object Browser displays the entire object model of all referenced components (that is, all ActiveX® or OLE objects that are used by the project) and, most importantly, the object model of CorelDRAW or Corel PHOTO-PAINT — all in an easy-to­use, structured format.
Project/Library list box
search controls
Search Results
window
Help button
View definition
button
Class list
Copy to
Clipboard
button
Member list
Information
window
The Object Browser window
navigation
buttons
To open the Object Browser, click View ` Object Browser, or press F2.
To reference the object models for other applications, click Tools ` References. Referenced components can be accessed by the VBA code.
All of the referenced objects — plus the current module — are listed in the Project/Library list box in the upper-left corner of the Object Browser. By default, all of the member classes for the referenced objects are provided in the Class list.
It is easier to use the Object Browser when only one project or library is displayed. To display only one project or library, choose it from the Project/Library list box.
More detailed information follows about certain Object Browser elements.
Using the Class list
The Class list shows all of the classes in the current project or library.
When you select a class in the Class list, the members of that class are shown in the Member list.
Chapter 2: Getting started with VBA 23
Every project or library has an object model that contains a number of member classes. Next to each item in the Class list, an icon depicts its class type:
Class icon Type
global value
module
enumerated type
type
class module
Global values (which apply to the selected project in its entirety) include individual members from enumerated types (such as text paragraph alignments, shape types, and import/export filters).
Member classes of an object have their own members.
To receive detailed information about a selected item, click the Help button at the top of the Object Browser.
Using the Member list
The Member list shows all of the properties, methods, and events that are members of the current class. Each member is given an icon according to its type:
Class icon Type
property
implied or default property
method
event
constant
Property members may be simple types (such as Booleans, integers, or strings), or they may be a class or enumerated type from the Class list. A property that is based on a class from the Class list inherits all the members of that class.
Chapter 2: Getting started with VBA 24
Many classes have a default property, indicated by a blue dot in their icon. The default property is implied if no property name is given when getting or setting the value of the parent object. For example, collection types have the default property
Item, which can be indexed. However, because Item is usually the default property, in such
instances, it is not necessary to specify the item property. Here,
ActiveSelection.Shapes.Item(1).Selected = False
is the same as the shorter
ActiveSelection.Shapes(1).Selected = False
because Item is the default or implied property of a collection of shapes.
Methods are commonly known as “member functions” — functions that the class can perform on itself. A good example is the vector. The following code moves the selected shapes
Move method of the Shape class, which is used to move a CorelDRAW shape using an [x, y]
2 measurement units to the right and 3 measurement units
upwards:
ActiveSelection.Move 2, 3
If the return value of a function is not used, the function call does not take parentheses around the argument list unless the
Call keyword is used.
For more information about
ActiveSelection, see the Appendix.
Some classes have various events associated with them. By setting up an event handler for a class’s event, when that event occurs in the application, the event’s handler is called. This functionality enables sophisticated applications to be developed that respond automatically to what is happening within the application.
Commonly handled events include the
PageActivate, and SelectionChange events of the Document class.
Only the
AddinHook class is not discussed in this manual.
Document, GlobalMacroStorage, and AddinHook classes have events in CorelDRAW. The
The constants listed in the Member list are either members of enumerated types or defined as
BeforeClose, BeforePrint, BeforeSave, ShapeMove,
Public in a
module. Enumerated types are used to group related items from a closed list, such as CorelDRAW shape types, import/export filters, and alignments. These constants can be used anywhere an integer value is required.
Most CorelDRAW constants begin with
cdrOutlineBevelLineJoin, cdrCurveShape, and cdrSymmetricalNode). Some constants begin with prn
or
pdf. Visual Basic also has its own constants, including ones for keystrokes (such as vbKeyEnter) and dialog
box buttons (such as
vbOK).
cdr (for example, cdrLeftAlignment, cdrEPS,
To receive detailed information about a selected item, click the Help button at the top of the Object Browser.
Using the Information window
The Information window gives information about the selected class or class member. This information includes a “prototype” of the member, its parent, and a short description of the member, and it also states whether the member is a read-only property.
Chapter 2: Getting started with VBA 25
The Information window
The types of any function parameters and properties are given as hyperlinks to the type definition or class itself, if the type is defined within the current object model. For example, the figure is a member of the
PHOTOPAINT class, so to view the class and its members, you can click the Application hyperlink.
it is a
PHOTOPAINT Document class; however, Application is also the property’s type, and
Application property in the preceding
To increase the height of the Information window, drag the top border of the window upwards to reveal its contents, or scroll down using the scrolls bar at the right of the window.
Using the search controls
You can search the object model for a matching string. This is useful for finding a class or member whose name you can only partly remember, or for finding classes and members that have similar names (such as names based on, or containing, the word “open”).
Searching an object model
To search an object model’s classes and members, type a string into the Search box, and then click the Search button. The Search Results list appears, displaying all of the found matches in alphabetical order. Clicking a found match advances the Class and Member lists to that item and displays its information in the Information window.
Matching class names have a blank Member column in the Search Results window.
To hide the Search Results window, click the Hide search results button .
Chapter 2: Getting started with VBA 26
Loading...
+ 71 hidden pages