connecting users of the CorelDRAW family of products

Getting Started in VBA

Page Details

First published by:
Hendrik Wagenaar
on Thu, Feb 5 2009
Last revision by:
Hendrik Wagenaar
on Sat, Jun 20 2009
10 people found this article useful.

CorelDRAW on Facebook

@CorelDRAW on Twitter

Share  

100% of people found this useful
Getting Started in VBA

Filed under: , [Edit Tags]

This is written for CorelDRAW X4.  Steps may differ slightly for older versions

Lets assume that a common task that you need perform is to make an object twice as big and move it to the right by 1 inch.   If you don't already know VBA, a good place to start is to record a macro that performs the task.

  1. On a new document with a rectangle selected, go to the main menu and choose Tools>Macros>Start Recording
  2. A save Macro dialog will pop up.  Give your macro an appropriate name.  The name must begin with a letter and contain no spaces (e.g. DoubleSizeAndMove).  In the Save Macro In section, GlobalMacros is selected by default, normally this is the module you will save your macros in.  You may optionally provide a description for your macro here as well.  Press OK.
  3. You are now in Recording mode.  All actions you perform will be recorded to the new macro function. 
    • Select the rectangle with the pick tool
    • In the property bar change the scale factor from 100% to 200%
    • In the x: coordinate box type " + 1 inch" and press enter.
  4. go to the main menu and choose Tools>Macros>Stop Recording.

You have now recorded a new macro.  Lets try it out.  With your rectangle selected, go to Tools>Macros>Run Macro.  A "Run Macro" dialog will pop up.  In the Macros in: drop down choose GlobalMacros.  You shoud now see an item that says RecordedMacros.DoubleSizeAndMove (or whatever name you gave it).  Select it and press run.

We seem to have a problem.  It didn't change the rectangle.  Time to look at the code and see what happened.  Press "Alt+F11" to bring up the macro editor.

In the project pane (left hand side), Expand GlobalMacros.  Expand Modules.  Double click RecordedMacros.

 Sub DoubleSizeAndMove()
    ' Recorded 05/02/2009
    Dim OrigSelection As ShapeRange
    Set OrigSelection = ActiveSelectionRange
    ActiveDocument.ReferencePoint = cdrCenter
    OrigSelection.SetSize 2.78822, 2.78822
    OrigSelection.SetPosition 4.005394, 7.911705
End Sub

This may look a little intimidating.  Lets break it up:

"Sub DoubleSizeAndMove()"  This line tells VBA that you are starting a sub routine.  All of the code for this function will exist between "Sub DoubleSizeAndMove()"  and "End Sub".  The "()" represents an empty parameter list and can be used for functions which need variables.  You can think of this as passing settings for a function to use.

"Dim OrigSelection As ShapeRange"  This line declares a variable that will be used.  A variable holds some data.  For example, if you need to store a number or text.  In this case we're storing a ShapeRange, which is really just a collection of shapes in the document.  Think of "Dim" as "declare", i.e. you are stating that a variable exists.

"Set OrigSelection = ActiveSelectionRange"  This line assigns the selected shapes to the variable we declared.

"ActiveDocument.ReferencePoint = cdrCenter"  This tells VBA that all operations will be performed using the center of the object as the origin.  In other words, when we scale, the left and right side will change by the same amount.

"OrigSelection.SetSize 2.78822, 2.78822" and  "OrigSelection.SetPosition 4.005394, 7.911705" are the operations we're applying to the selection.

On closer inspection of the last two lines we see why our rectangle didn't change when we ran the macro;  The recorder set the size and position directly rather than multiplying the current size by 2 and moving the current position to the left by 1 inch.  Since we had already set the rectangle size and position, running the macro was applying the exact same size and position, so nothing changed.  The Recorder is very good at doing exactly what we ask, but not very good at doing what we expect.

What we need to do is modify the macro so that we first get the current size and position and then modify them accordingly.  But how do we do that?  This is where VBA shines, you can often find a function you need by using the "Auto-complete" feature.  Since there is a SetSize function, maybe there is a GetSize function too?  Try this:

Insert a line after "ActiveDocument.ReferencePoint = cdrCenter" and type "OrigSelection.G".  You should see the following:

 At the bottom you can see the "GetSize" function.  That is what we need.  Select it and press Space.

The yellow tooltip tells us that the function requires two variables.  Both variables are of type Double.  Declare them by inserting the following lines after the "Dim OrigSelection as ShapeRange"

    Dim w As Double
    Dim h As Double

And pass these variable to your GetSize function.

  OrigSelection.GetSize w, h

And finally set the new size by multiplying the current size by 2.  Modify the OrigSelection.SetSize call to look like this:

  OrigSelection.SetSize w * 2, h * 2

Repeating the same steps for position we end up with the following code:

Sub DoubleSizeAndMove()
    ' Declare all of the variables we need
    Dim OrigSelection As ShapeRange
    Dim w As Double
    Dim h As Double
    Dim x As Double
    Dim y As Double

    Set OrigSelection = ActiveSelectionRange   ' Get the active selection
    ActiveDocument.ReferencePoint = cdrCenter  ' tell VBA to perform operations relative to the object's center
    OrigSelection.GetSize w, h                 ' Get the current width and height
    OrigSelection.SetSize w * 2, h * 2         ' Set the width and height to 2 times the current width and height
    OrigSelection.GetPosition x, y             ' Do the same for position
    OrigSelection.SetPosition x + 1, y
End Sub

 

Recent Comments

By: bob Posted on Sat, Feb 7 2009 7:13

Hi Hendrick,

Thank you, this is super stuff and long overdue. You are opening up many minds thus allowing artists to take both their creativity and Draw to the next level up. You make a confusing subject very simple.

More is needed.....<G> Lets keep going. How about a "book"?????

I am impressed.

Thanks again,

Bob

By: pranderson Posted on Tue, Apr 14 2009 12:18

Hendrik,

Excellent! It's so helpful to have some of the process explained and why behind it. Ever thought of teaching an online class???

Thank you for adding this wiki,

Patti

By: Silvio Win Posted on Wed, Apr 14 2010 15:23

You should be responsible for the making of the CorelDRAW's User Guide . Yours explanations are clear and well detailed. Only those who do not pay attention will do it wrong.

© Corel Corporation. The content herein is in the form of a personal web log ("Blog") or forum posting. As such, the views expressed in this site are those of the participants and do not necessarily reflect the views of Corel Corporation, or its affiliates and their respective officers, directors, employees and agents. Terms and Conditions / User Guidelines.