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.
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.911705End 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 DoubleAnd 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, yEnd Sub