RU
EN

Woodman Docker reference

Bulk Operations with Objects in VBA

Using relative resizing as an example

For anyone just starting out with process automation in CorelDRAW, there are many questions that come up. One common task is repeating the same action for each individual object. How do you resize all selected objects at once? How can you add a few millimeters to the width of every object? How do you change both the width and height of all objects while still processing each object individually? And how do you actually loop through all selected objects one by one using VBA?

In this simple example, we'll look at the basic principle of processing objects in CorelDRAW using a VBA macro.

I wrote two VBA functions for CorelDRAW that do practically the same thing: they resize each selected object by +5 mm in width and -7 mm in height.

The simple version

The first version is simple. It works, but has no error protection.

Sub resize_nocorrect()

	For Each s In ActiveSelectionRange.Shapes
		s.SetSize s.SizeWidth + 5, s.SizeHeight - 7
	Next s
	
End Sub

A more robust approach

The second version is more robust:

Sub resize_good()

	Dim s As Shape
	Dim OS As ShapeRange

	WI_plus = 5
	HI_plus = -7

On Error GoTo errn

	ActiveDocument.Unit = cdrMillimeter

	Set OS = ActiveSelectionRange
	If OS.Shapes.Count > 0 Then
		ActiveDocument.BeginCommandGroup

		For Each s In OS
			s.SetSize s.SizeWidth + WI_plus, s.SizeHeight + HI_plus
		Next s
	Else
		Exit Sub
	End If

errn:
	ActiveDocument.EndCommandGroup

End Sub

It may look like a lot more code for such a simple task, but most of it isn't actually related to resizing itself.

It makes sure we're working in millimeters, checks whether anything is selected, groups the entire operation into a single Undo step, and provides basic error handling.

I also use this kind of approach in my own CorelDRAW macro collection.

How do you usually write small CorelDRAW macros: keep them simple, or add all those extra checks?

Support Derevyashkin's project and get an exclusive macro panel!
Additional CorelDRAW macros from other authors:

Tools, generators for the docker, and standalone macros.

Statistics
since 23.04.2022
Users
11 649
Active
908
Clicks
5 592 583
Verification
Confirm this value: