VBA in Coreldraw

CQL

The Corel Query Language is an incredibly useful tool when automating Coreldraw.  Unfortunately I found that though there was documentation on using CQL, I couldn't always connect to dots with what I was trying to do.  So I've decided to make a cheat-sheet with my interpretation of how things work.

One of the most common things CQL can be used for is selecting a whole bunch of items from a drawing using just a single line of code, which you can then have as a ShapeRange.  Once things are in a ShapeRange there are so many things that you can do!

A very basic example might be:

Private Sub examplesTest()
    Dim testRange As ShapeRange
    Set testRange = ActiveDocument.ActivePage.Shapes.FindShapes(query:="@type = 'text:artistic'")
    MsgBox ("testRange has " & testRange.Count & " items.")
End Sub

This checks the active page in your document and adds a reference to every artistic text item to your "testRange", which you can then work with.

Fine, but one thing that really got to me is that I couldn't figure out how to find types that weren't referenced in the bit of documentation that exists.

One of the hits in the CQL documentation was that all the objects have a "help" function that can list all available properties and methods.  Awesome, right?  Except their example was for an "int" type, and I wasn't sure how to get from there to querying an actual shape. 

In the "Immediate Window" of the VBA editor you could type:

? Evaluate("int.help")

And that would give you everything you needed about "int".  But what about other objects?

This first became an issue for me when trying to hunt down dimension shapes... how does one magically know the @type of dimension?  It's definitely not cdrLinearDimension!

Well, creating a little method that can query a selected shape seemed to be the best way to go about this:

Private Sub evalShapes()
    Dim workRange As ShapeRange
    Dim textDump As String
    Dim dumpSh As Shape
    
    ' Get all the currently selected shapes
    Set workRange = ActiveDocument.SelectionRange
    
    For Each dumpSh In workRange.Shapes
        textDump = textDump & dumpSh.Evaluate("@type") & vbCr
    Next dumpSh
    
    MsgBox ("Selected shapes:" & vbCr & textDump)
End Sub

This method will give us the @type of all selected objects, that we can then use in our CQL queries.

Styles

In VBA, accessing Coreldraw's Styles can be a very useful way of figuring out what the user's default settings look like for some items, such as the default size and style of fonts.  But where to start?

One quick trick I learned from folks on the Corel developer's forum is that you can start by trying to query the styles directly with a method such as this:

Private Sub StyleTest()
    Dim styleStr As String
    styleStr = ActiveDocument.StyleSheet.FindStyle("~dimension~").GetPropertyAsString("dimension")
    MsgBox ("Styles: " & styleStr)
End Sub

Here we can query the style "~dimension~" and then get the properties associated with "dimension".  Or... what?  Why are there two tilde in the style, and how do we know the property is called "dimension"?

This takes a little more work, but can be worth it.

If you look at your "Object Styles" docker, you can see the "Default Object Properties", which will include things like "artistic text", "callouts", and "dimensions", among others.

From this docker you can export your styles to a file, and this is the key to figuring out the key terms we need to use.

When you export a style from the docker it ends it in .cdss

This a compressed file containing all the information Corel needs to store the styles.  First, we can change the ".cdss" to ".zip" and now we can unzip the file to see the contents.

With-in the zip file is a folder names "styles" which contains "document.cdss".  Opening this file with a text editor (such as notepad) will show all the plain-text entries.  The "parent" entries have tilde before and after the name such as "~dimension~" or "~callout~".  This would correspond to our "FindStyle" entry above.

Below the parent items are the children items which do not have the tilde in their name.  So under the dimension style we can see "dimension", "fill", and "outline", where you can see all relevant settings below.