Creating a new VBA macro

Creating a new macro in VBA Editor is very easy. First, start VBA Editor by pressing Alt-F11 while in CorelDRAW or in Corel Photo-Paint. You will see the VBA Editor main window:

On the left-hand side of the VBA Editor you can see a Project Explorer window that lists all VBA projects currently available. You should decide where you want to place your macro in first place.

Usually you can see one or more GMS modules in the project explorer and several "VBAProject" items. Each GMS module is a separate file with .GMS extension that stores one VBA project. A VBA project can contain several forms, code modules and classes. A GMS file is load when the application (Draw or Paint) starts and is active until you quit the application. Thus, macros put in a GMS file are available at all times while you work in the application. The "VBAProject" items in Project Explorer are created for each document currently open in the application. If you don't have any document open in CorelDRAW (or Photo-Paint) you will see no VBAProject items in the tree. These VBAProjects are the same VBA Projects as those in GMS modules except that they are stored (embedded) within the document itself, not in a separate file. As a result, macros stored in these projects are available only when the document is open. As soon as you close the document, macros will "disappear" from VBA Editor and you cannot run them.

All VBA Projects in Corel applications have at least one code module named "Thisdocument". To see it, expand the corresponding VBA Project node in the Project Explorer:

To see the code in this module, just double-click it and you will see the code window open in the right-hand pane:

This code window can be empty or it can already contain some code inside. In order to add a new macro, just add a subroutine that doesn't have parameters, for example:

The above sample macro for CorelDRAW creates an ellipse in the current document and fills it with red uniform fill. To run this macro just place a cursor somewhere inside the subroutine and select Run menu>Run Sub/UserForm command or press F5. You can also run the macro from CorelDRAW (or Photo-Paint) itself. Just go to Tools>Visual Basic>Play... menu. You will see the Play macro dialog:

Select the VBA Project where you stored your macro from "Macros in:" list and the list of all available macros in that project will appear in the list above. Select your macro and click "Run" button.


[ General VBA Articles | Using VBA with Corel Applications | Oberon Home Page ]

Copyright 2001 by Alex Vakulenko. All rights reserved.
This page was last revised on 02/25/01.