How to Create CorelDRAW VBA Macro Functions

This guide provides an overview of developing custom Visual Basic for Applications (VBA) macro functions within CorelDRAW. You will learn how to open the integrated development environment, structure reusable functions and subroutines, interact with document vector objects, and execute your scripts to automate repetitive graphic design tasks.

1. Accessing the VBA Environment in CorelDRAW

CorelDRAW includes a built-in development environment for creating and debugging macros.

  1. Open CorelDRAW.
  2. Navigate to Tools > Scripts (or Macros in older versions) > Script Editor, or press Alt + F11 on your keyboard.
  3. In the Project Explorer window on the left, locate your target project:
    • GlobalMacros (GMS): Saves the macro globally so it is accessible in all CorelDRAW sessions.
    • ThisDocument: Binds the macro exclusively to the currently open file.
  4. Right-click the chosen project folder, select Insert, and click Module. A new code window will open.

2. Understanding Functions vs. Subroutines

VBA utilizes two primary procedures:

  • Sub Procedures: Execute actions without returning a value (e.g., drawing shapes, modifying colors). CorelDRAW's Macro Manager directly runs Sub routines.
  • Function Procedures: Perform calculations and return a value directly to the caller. They are ideal for modular code, such as computing geometry, converting units, or checking object types.

3. Writing a Custom Macro Function

To create a functional script, you can combine a calculation Function with an execution Sub.

The following example defines a function that calculates the total perimeter of all selected shapes, followed by a macro subroutine that displays the result in a dialog box:

' Function that calculates the total perimeter of selected shapes
Function GetTotalPerimeter(sSelection As ShapeRange) As Double
    Dim sh As Shape
    Dim totalLength As Double
    totalLength = 0
    
    ' Loop through each shape in the passed selection
    For Each sh In sSelection
        totalLength = totalLength + sh.DisplayCurve.Length
    Next sh
    
    ' Return the calculated result
    GetTotalPerimeter = totalLength
End Function

' Subroutine to execute the function and display the output
Sub CalculateSelectedPerimeter()
    Dim sel As ShapeRange
    Set sel = ActiveSelectionRange
    
    ' Check if user has selected any objects
    If sel.Count = 0 Then
        MsgBox "Please select at least one curve or shape.", vbExclamation, "No Selection"
        Exit Sub
    End If
    
    Dim total As Double
    ' Call the custom function
    total = GetTotalPerimeter(sel)
    
    ' Display the result in document units
    MsgBox "Total Perimeter of selected objects: " & Round(total, 2) & " inches", vbInformation, "Perimeter Calculator"
End Sub

4. Working with Document Objects

When writing custom functions to manipulate CorelDRAW documents, prioritize these essential application objects:

  • ActiveDocument: Represents the current file.
  • ActivePage: Represents the page currently in view.
  • ActiveSelectionRange: A collection containing all currently highlighted shapes.
  • Optimization: A property used to increase execution speed.

To prevent screen flickering and improve performance during intensive macro loops, wrap object manipulations using the Optimization property:

Sub BatchModify()
    ' Speed up macro execution and treat operations as a single Undo step
    ActiveDocument.BeginCommandGroup "Batch Process"
    CorelDRAW.Optimization = True
    
    Dim sh As Shape
    For Each sh In ActiveSelectionRange
        If sh.Type = cdrRectangleShape Then
            sh.Fill.ApplyUniformFill CreateRGBColor(255, 0, 0)
        End If
    Next sh
    
    CorelDRAW.Optimization = False
    ActiveWindow.Refresh
    ActiveDocument.EndCommandGroup
End Sub

5. Running and Testing the Macro

  1. Return to the main CorelDRAW interface.
  2. Select one or more vector shapes on the canvas.
  3. Go to Tools > Scripts > Scripts (or press Alt + Shift + F11) to display the Scripts Docker.
  4. Expand the GlobalMacros tree, locate the module you created, select your Sub, and click Run.
  5. To streamline frequent use, right-click any toolbar, select Customize > Commands, find your macro in the list, and drag it to a toolbar or assign a custom keyboard shortcut.