CorelDRAW Community
CorelDRAW Community
  • Site
  • User
  • Site
  • Search
  • User
Wikis
Wikis
How-to and Tips&Tricks Getting Started in VBA
  • Wikis
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • DUPLICATE
  • The Clone Tool
  • Corel DRAW: Halftone
  • +Corel PHOTO-PAINT
  • Corel PHOTO-PAINT for Adobe PhotoShop users
  • +CorelDRAW
  • CorelDRAW for Adobe Illustrator users
  • +CorelDRAW.com
  • Designer's Guide to Color Management
  • Exporting ai files from CDX6 to Adobe InDesign CS6 - not working!
  • GDG Macros VBA Lessons
  • +Hidden Gems, Keyboard Tricks in CorelDRAW Graphics Suite
  • How and where save globalmacros.gms
  • +How to show Thumbnail Preview
  • +Making flag in CD/PP by Jesus Cota
  • MANUAL FLATTENING
  • Mesh fill tool structure on circular shapes
  • Selection Groups
  • Soft Bevel Macro for CorelDRAW
  • Thumbnails Everywhere
  • Troubleshooting - Computer says No
  • -VBA
    • Getting Started in VBA
    • getting-started
    • How do I make a macro easily accessible
    • How to create a menu or a button for your macro with XML in CorelDRAW X5 automatically
    • how-do-i-make-a-macro-easily-accessible
    • What is VBA
    • Where can I find exisiting Macros?
  • +Water Effect by Jesus Cota
  • ALIGN CENTER
  • COMMAND BUTTON EXAMPLES
  • +SIM Model
  • +USING PHOTOSHOP AS A BITMAP EDITOR FOR COREL DRAW
  • VSTA and working VSTA examples

Getting Started in VBA

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

 

  • VBA
  • VBA Editor
  • Share
  • History
  • More
  • Cancel
Related
Recommended

© Corel Corporation. All rights reserved. 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 of Use / Privacy​ ​/ ​Cookies / Terms and Conditions / User Guidelines.