How to create a COM Add-in?

COM Add-in is an ActiveX DLL that CorelDRAW loads at start-up and calls whenever a user choose an addin execution command. In fact, an addin is very similar to a normal macro that is compiled into a separate module with additional initialization code.

You can use Microsoft Visual Basic 6 to create an Add-in for CorelDRAW 10. It is also possible to use Visual Basic for Applications that comes with CorelDRAW to create an addin if you have Microsoft Office Developer installed on your system.

The following instructions are for Visual Basic 6 however you could apply them to VBA easily.

In order to create an addin for CorelDRAW 10 do the following:

1. Start Visual Basic and choose "Addin" from the New Project dialog:

This will create an Add-in project that will have an Add-in Designer and a form.

2. Go to Project Explorer and delete the form (frmAddIn) as more likely you won't need it in your project.

Note that the resulting ActiveX object for the add-in will be named  "ProjectName.DesignerName" so in the default state it would be "MyAddIn.Connect". You would probably want to change this to something more meaningful. So, go to the Properties window and rename the project from MyAddIn to something that describes your addin. Select the project node in the Project Explorer [MyAddIn (MyAddIn)] and in Properties window change the name of the project to something that is more related to your addin functionality (for example, choose "CropMarks").

3. When you double-click the Designer node in the Project Explorer [Connect (Connect)], you will see the designer form. Use this form to specify setup information for the addin. Most important part is to specify the application for which you are creating the addin. Choose "CorelDRAW" from the Application list and make sure the correct version appears in the list below. You can also specify your addin full name and description:

4. Close the Addin designer form.

5. In Project Explorer, select the Addin designer node and right-click it. In the popup menu choose "View Code":

6. Remove all the code from the code window (the default code is related to Microsoft Office, not CorelDRAW anyway).

Now you should have an empty code window entitled "CropMarks - Connect (code)"

7. Remove project reference to Microsoft Office and Visual Basic Extensibility library and add reference to CorelDRAW 10 type library. Go to Project>References dialog and remove checkmarks from "Microsoft Office 9.0 Object Library" and "Microsoft Visual Basic 6.0 Extensibility" and locate "Corel - CorelDRAW 10.0 Library" further in the list (make sure that "Microsoft Add-In Designer" remains referenced):

8. Close the References dialog by clicking OK button.

9. Go back to the code window and choose "AddinInstance" from the dropdown list in the top left corner of the code window:

This will automatically add an event handler for "OnConnection" event to your code:

10. In the second dropdown list to the right, choose "OnDisconnection" to add one more event handler. Now you have the main two handlers to the events your addin should respond to:

OnConnection is called whenever the addin is loaded and needs to be initialized. The only parameter that should interest you at this time is the first one - Application. This is the instance of the CorelDRAW's Application object. You should use this object whenever you try to access any feature in CorelDRAW object model. 

You should store a reference to the Appication object in a global variable for future reference. Note that it is not recommended to use constructions like:

Set Draw = New CorelDRAW.Application


Set Draw = CreateObject("CorelDRAW.Application.10")

because if there are more than one copy of CorelDRAW running on the system, you cannot access "the right one" from your addin. However the application object passed as a parameter to OnConnection event is the one that actually loaded the addin.

You should use OnConnection object to add an Addin Hook object to CorelDRAW addin list. The AddinHook object is an event sink that CorelDRAW uses to invokes the addin whenever the addin menu command is executed.

OnDisconnection is called before the application is about to shut down and unload the add-in module. At this time you should free any references to CorelDRAW object otherwise the application may not quit properly and stay in memory.

11. Add the code to OnConnection and OnDisconnection event handlers. Use the following example on how to do this:

Dim Draw As CorelDRAW.Application
Dim WithEvents CropMarksAddin As CorelDRAW.AddinHook

Private Sub AddinInstance_OnConnection(ByVal Application As Object, _
             ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, _
             ByVal AddInInst As Object, custom() As Variant)
    Set Draw = Application
    Set CropMarksAddin = Draw.AddIns.Attach(cdrAddinFilterNone, "Create &CropMarks")
End Sub

Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode As _
             AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
    Set CropMarksAddin = Nothing
    Set Draw = Nothing
End Sub

In the above example, the application object is set to a global variable Draw. Then a new addin hook is added to CorelDRAW by using its Addins collection and calling its Attach method.

The first parameter in Attach method specifies which events you DO NOT want to receive from CorelDRAW. Specify cdrAddinFilterNone if you want to receive all messages. The optional second parameter specifies the menu item caption that will be added to Tools>Visual Basic>Addins menu. The above example adds "Create CropMarks" menu.

Please note that the AddinHook object must be defined using WithEvents keyword so you can provide event handlers for the addin events.

12. Provide addin event handler(s). Select "CropMarksAddin" in the first dropdown list at the top left of the code window and a new event handler will be added: CropMarksAddin_Execute. This event is fired each time the addin menu item is selected.

13. Add the code to the Execute event handler to provide your addin code:

Private Sub CropMarksAddin_Execute()
    Dim x As Double, y As Double, sx As Double, sy As Double
    If Draw.Documents.Count > 0 Then
        If Draw.ActiveSelection.Shapes.Count > 0 Then
            Draw.ActiveSelection.GetBoundingBox x, y, sx, sy, True
            With Draw.ActiveLayer
                .CreateLineSegment x - 0.5, y, x, y
                .CreateLineSegment x, y - 0.5, x, y
                .CreateLineSegment x + sx, y, x + sx + 0.5, y
                .CreateLineSegment x + sx, y - 0.5, x + sx, y
                .CreateLineSegment x - 0.5, y + sy, x, y + sy
                .CreateLineSegment x, y + sy, x, y + sy + 0.5
                .CreateLineSegment x + sx, y + sy, x + sx + 0.5, y + sy
                .CreateLineSegment x + sx, y + sy, x + sx, y + sy + 0.5
            End With
        End If
    End If
End Sub

14. Save your project and compile in a DLL. Go to File>Make CropMarks.dll menu in Visual Basic and specify a folder to save the addin DLL to. When the addin is compiled it is automatically registered with CorelDRAW. 

So now you can just launch CorelDRAW and see how it works. After CorelDRAW is started, create a new document and create a shape. With the shape still selected go to Tools>Visual Basic>Addins and choose "Create CropMarks" item in the pop-up menu. The addin should be executed and crop marks should be created around the selected shape in CorelDRAW document.

You can download Visual Basic 6 sample project with the source code here: (1.5K)

[ General VBA Articles | Using VBA with Corel Applications | Oberon Home Page ]

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