General
CorelDRAW

Photo-Paint
Mailing List

Using VBA with Corel Applications

Microsoft Visual Basic for Applications (VBA) is an industry-standard automation tool on Windows platform. It is used by more than 3 million developers worldwide and integrates power of COM technology, ease of use of BASIC programming language, fast visual application development process and huge base of available code and modules to reuse.

First it was used solely in Microsoft products, but then the popularity of the tool increased immensely and now it is integrated into a lot of programs including MS Office, AutoCAD, Visio, and many others.

VBA is also part of CorelDRAW Graphics Suite starting from version 9.

VBA integration into CorelDRAW means a lot. VBA uses different automation interface than Corel SCRIPT used to use. This new interface has hierarchical structure of objects starting from Application, going to Document, Page, Layer, Shape, and their different properties such as shape fill, outline, and so on. This structure makes the program more logical and easier to create as well as allows to increase performance.

Since its introduction in version 6, Corel SCRIPT didn't change much, except that some new commands were added. With VBA situation is improved greatly. Putting aside technical details, I just want to note, that VBA macro runs about 5-10 times faster than equivalent Corel SCRIPT program. And taking into account that VBA object model is much more advanced (which offers access to wider range of object properties), VBA macro can achieve the same result in lesser number of steps, which by itself increases performance even more.

Just take a look at this simple VBA program:

Sub FillAllWithRed()
    Dim s As Shape
    For Each s In ActiveDocument.ActivePage.Shapes
        s.Fill.UniformColor.RGBAssign 255, 0, 0
    Next s
End Sub

This tiny function looks through all objects on the current page of the active document and fills all objects with red.

Now here is the same program written in Corel SCRIPT:

SUB FillAllWithRed()
WITHOBJECT "CorelDRAW.Automation.9"
    .SuppressPainting
    .UnSelectAll
    .SelectNextObject 0
    ID&=.GetObjectsCDRStaticID()
    DO
        .StoreColor 5005,255,0,0
        .ApplyUniformFillColor
        .SelectNextObject 0
    LOOP UNTIL .GetObjectsCDRStaticID() = ID
    .ResumePainting
END WITHOBJECT
END SUB

It is more complex, isn't it? Now if you notice the time it takes to process the document, you would probably notice that VBA variant is MUCH faster (on my machine, it took about 25 seconds to process 100 objects with Corel SCRIPT program, while it was less than 1 second to run though them with VBA macro).

I believe you are already convinced that there is good reason to look into it, right? OK. Let's move to the interesting part... But before we do this, I would like to note one important thing: VBA is new in CorelDRAW. And version 9 is only a starter, so don't expect a full implementation. Most likely, you still will need to use Corel SCRIPT to create certain effects which are not implemented in VBA object model yet. Fortunately, there's CorelScript object available for VBA macros to run Corel SCRIPT commands for features missing in VBA. For more complete VBA support in CorelDRAW you will need to get CorelDRAW 10. CorelDRAW 10 has the most advanced automation interface ever with over 2,500 methods and functions. Some features are still not covered by automation but the improvement is drastic. You now can do the things you never thought you could.

Please keep in mind, that if you have any questions, or comments, you can always send me a message. Let me know what you think. It is always good to know that what one is doing is helpful for other people. Or is it?

For technical questions, you can use CDRVBA mailing list or post to corelsupport.draw-script.


[ Oberon Home Page ]

Copyright 2000 by Alex Vakulenko. All rights reserved.
This page was last revised on 11/25/02.