Using the Immediate window

The Immediate window allows you to type in and run arbitrary lines of code while a macro is paused. This is useful for getting or setting the property of an object in the document, or for setting the value of a variable in the code. To run a piece of code, type it in the Immediate window, and then press Enter. The code is executed immediately. To display the Immediate window, click View Immediate window.

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...

Providing buttons

As previously discussed, you can add a button to a form by using the CommandButton control. Click the form to add a default-sized button, or drag to create one to your own specifications. Click the caption to edit it, or select the button and edit its Caption property in the Properties window. You might also want to change the name of the button to something more descriptive, such as buttonOK or buttonCancel. Designing buttons in the Form Designer Designing buttons in the Form Designer Most...

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...

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 Shape object may be of the type group and may contain other Shape objects, some of which may also...

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...

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. 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...

Capturing mouse drags

To get from the user the position of a mouse drag, or an area or rectangle, use the GetUserArea member function of the Document object. This function pauses the macro for a specified period of time or until the user clicks, drags, and releases somewhere in the document, or until the user presses Escape. Here is an example that uses the GetUserArea function Dim doc As Document, retval As Long, shift As Long Dim x1 As Double, y1 As Double, x2 As Double, y2 As Double retval doc.GetUserArea x1, y1,...

Parsing selected shapes

Parsing through the selected shapes is done in the same way for both selections and selection ranges. Here is a code sample for selections Dim shs As Shapes, sh As Shape Set shs ActiveSelection.Shapes For Each sh In shs And here is a code sample for selection ranges Dim sRange As ShapeRange, sh As Shape Set sRange ActiveSelectionRange For Each sh In sRange ' Do something with the shape, sh Next sh However, selection ranges have the advantage that even if the selection subsequently changes, the...

Using the Locals window

The Locals window displays all of the variables and objects that exist within the current scope. Each variable's type and value are listed in the columns next to the variable's name. Some variables and objects may have several children, which can be displayed by clicking the expand tree button next to the parent. Many variables let you edit their value by clicking it. To display the Locals window, click View Locals window.

Resizing pages

You can resize pages and set their orientation, set the default page size, and use defined page sizes. Resizing pages and setting their orientation Pages can be individually resized by using the SetSize member function of the Page class. This function takes two size values width and height and applies them to the page. The following code changes the size of the active page in the active document to A4 ActivePage.Orientation cdrLandscape For the SetSize method, the first number is always the...

Description

DocumentOpen Raised when a document is opened passes a reference to the document DocumentOpen 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 Raised after a document is saved passes the file name of the document as a Raised before the Print dialog box is displayed Raised when the user quits CorelDRAW Event handlers for frequent events such as events related to the Shape class should be as efficient as...

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 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 UserForm toolbar contains buttons specific to designing forms as discussed in Designing dialog boxes on page 42 . You can choose to display or hide each toolbar. Click View...

Declaring enumerated types

To declare an enumerated type, use the following construction 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,...

Setting the Undo string for documents

Two very useful member functions of the Document object allow any number of programmed CorelDRAW actions to appear as a single action on the undo list. These methods are BeginCommandGroup and EndCommandGroup , as in the following code sample ActiveDocument.BeginCommandGroup CreateCurveEllipse Set sh ActiveLayer.CreateEllipse 0, 1, 1, 0 sh.ConvertToCurves ActiveDocument.EndCommandGroup After running this code, the Undo string on the Edit menu displays Undo CreateCurveEllipse, and clicking Undo...

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 Building functions and subroutines Using memory pointers and memory allocation Using Boolean comparison and assignment Using logical and bitwise operators Providing message boxes and input boxes The VB Editor formats all of the code for you as discussed in...

Positioning shapes

The position of a Shape object can be determined by using the properties PositionX and PositionY, and by using the methods GetPosition and GetBoundingBox. The position of a shape can be specified by setting the properties PositionX and PositionY, or by using the methods SetPosition, SetSizeEx, and SetBoundingBox. The following code gets the position of the ActiveSelection shape relative to the current ReferencePoint property of ActiveDocument, which the code explicitly sets to the lower-left...

Click Workspace Customization Commands 1

2 Choose Macros from the Commands list box. The list displays the fully qualified names of all of the public, parameter-free subs from all of the installed project GMS files. 3 Select a macro in the Command list. 5 Type the caption in the Caption box. To associate with a caption an accelerator key that can be activated in combination with the Alt key, type an ampersand amp in front of the character in the caption that you want to set as the accelerator. This accelerator key applies only to menu...

Working with documents

Whenever a CorelDRAW file is opened, a new Document object is created in the Application object for that document. The Application object contains a Documents collection, which provides access to all of the open documents. The order of the documents in the collection is set to the order in which the documents were created and opened. You can browse the object model for the complete list of Document objects, but here is a list of the most useful ones some of which are explained in greater detail...

Publishing documents to PDF

Publishing to Adobe Acrobat Reader PDF format is a two-stage process. The first step is to specify the PDF settings, but this step can be skipped if the user specifies the document's settings from within CorelDRAW or chooses to use the default settings. The second step is to export the file. To set the PDF settings, modify the properties of the Document object's PDFSettings member. This member is an object of type PDFVBASettings and has properties for all of the PDF settings that can be set in...

constant

A named item that keeps a constant value while a macro is being executed enumerated type enumeration A data type that lists all the possible values for the variables that use it event An action that is recognized by a form or control event handler A subroutine that is programmed to cause the application to respond to a specific event function A procedure that performs a given task in a macro and that can be used to return a value. A function procedure begins with a Function statement and ends...

Changing content in documents

You can modify content documents regardless of whether they are active. For example, if you have a reference to a document, you can add a new layer called fooLayer by using the following code Dim doc As Document Set doc Documents 3 doc.ActivePage.CreateLayer fooLayer If you want to create the new layer in an inactive document whose name you know in the following example, it is barDoc.cdr , you might use the following code to call the function findDocument Set doc If Not doc Is Nothing Then...

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 . 1 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...

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...

Switching between documents

The ActiveDocument property provides direct access to the active document that is, the document that's in front of all the other documents in the CorelDRAW window. ActiveDocument is an object of type Document and, therefore, has all of the same members properties, objects, and methods as the Document class. If there are no open documents, ActiveDocument returns Nothing. You should test for this with the following code MsgBox There aren't any open documents., vbOK, No Docs Exit Sub End If The...

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...

Providing event handlers in macros

While running, CorelDRAW raises various events to which VBA can respond through the use of event handlers subroutines with specific, defined names within a ThisMacroStorage module in CorelDRAW. Every CorelDRAW VBA project that is, GMS file has one ThisMacroStorage module within its CorelDRAW X3 Objects subfolder for the project. Similarly, every Corel PHOTO-PAINT VBA project has one ThisDocument module within its Corel PHOTO-PAINT X3 Objects subfolder for the project. The GlobalMacroStorage...

Deploying workspaces

When you have developed a customized workspace that contains customized toolbars, menus, and shortcut keys, these customizations can form an intrinsic part of your macro solution. You can deploy the entire workspace to users for installation. Alternatively, you can export some features of your workspace so that users can import only those features into their workspace. You can distribute individual toolbars and menus, as well as complete sets of shortcut keys. Users can import a workspace or...

Importing files into layers

Files of all supported formats can be imported into CorelDRAW. Files are imported onto layers therefore, the Import and ImportEx functions are members of the Layer object. The following code imports the file C logotype.gif onto the active layer at the center of the page ActiveLayer.Import C logotype.gif When importing, any shapes that were previously selected are deselected, and the contents of the imported file are selected. To reposition or resize the imported shapes, get the document's...

Understanding the CorelDRAW object model

In CorelDRAW, the Application object is the root object for all other objects. To reference the CorelDRAW object model from an out-of-process controller, you use its Application object. For example, in VB you would use the following code Set cdr Although you can use the above code in VBA, it's not required for CorelDRAW because the Application object is used by default if no other root object is specified. The Application object contains all of the Document objects that are created when...

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. By default, function and subroutine parameters are passed by...

Referencing the ActiveSelectionRange object

ActiveSelectionRange is the shortcut for ActiveDocument.SelectionRange. This is a property of the document of type ShapeRange Set selRange ActiveDocument.SelectionRange The ShapeRange object returned by ActiveSelectionRange contains a collection of references to the shapes that were selected at the moment when the property was read. Because these references are to the shapes themselves and not to the selection , if you change the selection, the ShapeRange is not updated. The shapes referenced...

Stretching and scaling shapes

Shapes can be stretched and scaled by a proportional amount. The Shape object has two member functions Stretch and StretchEx that perform this operation. Both functions take a decimal value for horizontal and vertical stretching, where 1 is 100 or no change you cannot use zero, so you must use a very small value instead. The following code stretches the selection to half its current height and twice its width, about the midpoint of the bottom edge of its bounding box...

Switching between pages

To access the active page of the active document, use Application.ActivePage, ActiveDocument.ActivePage, or simply ActivePage. These return a reference to the active page in the active document, of type Page To access the active page of any document regardless of whether it is active , use the property Document.ActivePage of the given document Public Function getDocsActivePage doc As Document As Page Set getDocsActivePage doc.ActivePage To switch between pages, find the page that you want to...

Icon Control

Lets you provide the user with static text for example, instructions or captions Lets you provide an area into which the user can type text Lets you provide a list from which the user can select an item and optionally into which the user can also type text Lets you provide a list from which the user can select multiple items CheckBox Lets you provide a check box that the user can enable by clicking to insert a check mark into it or disable by clicking to remove the check mark from it , or that...

Referencing objects in macros

If you want to create a reference to an object so that you can treat that reference like a variable sh, in the example that follows , you can use the Set keyword Set sh ActiveSelection.Shapes.Item l After you create this reference, you can treat it as if it were the object itself If the selection is changed while sh is still in scope, sh references the original shape from the old selection and is unaffected by the new selection. You cannot simply assign the object to the variable as in the...

Locking and hiding layers

Layer objects have the properties Enabled and Visible that control respectively whether you can edit the layer and whether its contents are visible in CorelDRAW. Both properties are Boolean. By setting both the properties to True, you unlock and display the layer for editing. By setting either property to False, however, you lock the layer such that it cannot be edited. The following sample code locks, but displays, the layer on the active page ActivePage.Layers Layer 1 .Visible True...

Creating dialog boxes for macros

Corel Vba Guide

All dialog boxes should abide by the following guidelines They should have a meaningful title. They should provide an obvious functionality for cancelling or closing them. Their layout should make them easy to use, but they should also provide a Help button from which users can access how-to documentation. Their every control should feature a ControlTipText string, so that users can receive information about each control by passing the pointer over it. However, there are two kinds dialog boxes...

Deleting pages

Pages can be deleted by naming each page's Delete member function as follows ActivePage.Delete This deletes all of the shapes that exist on that page and deletes the page from the document's Pages collection. The collection is immediately updated to reflect the change. Delete must be called individually for each page that you want to delete. You cannot delete all of the pages in a document. By using the following code, you can avoid trying to delete the last remaining page in a document If...

Running macros

Corel Macros

You can run macros either from directly within CorelDRAW or Corel PHOTO-PAINT or from within the VB Editor. CorelDRAW X3 Visual Basic for Applications Macros Macio 'irii FileConverter FileConverter.gmsl The CorelDRAW X3 Visual Basic for Applications Macros dialog box The CorelDRAW X3 Visual Basic for Applications Macros dialog box To run a macro from within CorelDRAW 1 Click Tools Visual Basic Play, or click the Play button on the Visual Basic for Applications toolbar. 2 From the Macros in list...

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 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. Many classes have a default property, indicated by a blue dot in their icon. The default property is...

Selecting shapes

To determine whether a Shape is selected, you can test its Selected Boolean property Dim sh As Shape Set sh ActivePage.Shapes 1 If sh.Selected False Then sh.CreateSelection You can add a Shape to the selection simply by setting its Selected property to True this selects the shape without deselecting all the other shapes. To select just one shape without any other shapes, use the CreateSelection method, as in the preceding code. To deselect all the shapes, call the document's ClearSelection...

Testing coordinate placement

You can test whether a set of coordinates that is, a point is inside, outside, or on the outline of a curve by using the shape's IsOnShape member function. This function takes a document coordinate and returns cdrlnsideShape if the coordinate is inside the shape, cdrOutsideShape if the coordinate is outside the shape, or cdrOnMarginOfShape if the coordinate is on or near the outline of the shape. For example, the following code tests where the point 4, 6 is in relation to the ActiveShape Dim...

Creating rectangles

There are two functions for creating new rectangle shapes CreateRectangle and CreateRectangle2 both of which return a reference to the new Shape object. The two functions differ only in the parameters that they take. For example, the following code, which uses CreateRectangle, creates a simple two-by-one-inch rectangle positioned six inches up from the bottom and three inches in from the left of the page Set sh ActiveLayer.CreateRectangle 3, 7, 6, 5 The parameters are given as left, top, right,...

Exporting files from documents

Files of all supported formats can be exported from CorelDRAW. Files are exported from the Document object not from Layer objects because the range of shapes exported often extends over multiple layers or even over multiple pages . The Document object has three export functions Export, ExportEx, and ExportBitmap all of which can be used to export to bitmap or vector format. To export a page, you require only a filename and a filter type. The following code exports the current page to a TIFF...

Determining shape type

Each Shape object has a property Type that returns the shape's subtype that is, what type of shape it is rectangle, ellipse, curve, text, group, and so on . This is a read-only property. Because some types of shapes have member functions and properties that others do not, it is often necessary to test the shape's type before calling a method that doesn't necessarily apply to it. The following sample code tests a shape to determine whether it is text. It then tests to determine whether it is...

Filling shapes

There are many types of fills in CorelDRAW, including uniform fills, fountain fills, PostScript fills, pattern fills, and texture fills, and each type needs to be handled differently. Only uniform and fountain fills are discussed here. The read-only Type property of a Fill object gives the type of the fill, or whether there is no fill. The following code gets the fill type The fill type cannot be set with this property it is set when the fill is created and applied. To remove any type of fill,...

Changing shape properties

You can modify the properties of the shapes you create by using VBA. Sizing shapes To get the width or height of a Shape, test its SizeWidth or SizeHeight properties Dim width As Double, height As Double ActiveDocument.Unit cdrMillimeter width ActiveShape.SizeWidth height ActiveShape.SizeHeight The returned Double value depends on the setting of ActiveDocument.Unit, so if you need the size in a specific unit, begin by setting it. The same applies for all of the size properties and methods in...

Printing documents

Printing documents with VBA is straightforward almost all settings that are available in the CorelDRAW Print dialog box are available as properties of the Document object's PrintSettings member. When the properties are set, printing the document is simply a matter of calling the document's PrintOut member function. For example, the following code prints 3 copies of pages 1, 3, and 4, to a level 3 PostScript printer With ActiveDocument.PrintSettings .Copies 3 .PrintRange prnPageRange .PageRange...

Creating ellipses

There are two methods for creating ellipses CreateEllipse and CreateEllipse2. They differ in the parameters that they take, so you can create an ellipse based on its bounding box or instead on its center point and radius. Both functions also create arcs or partial ellipses, or segments or pie slices. The CreateEllipse method takes four parameters that define its bounding box in the same way as for CreateRectangle in other words, left, top, right, bottom, in the document's units. The following...