Search code examples
vbaexcelcoreldraw

Dim Workbook in CorelDraw VBA Macro not compiling


I am writing a CorelDraw macro that fetches some data from Excel by opening a Workbook.

I like to declare my variables before using them (and also normally require this with Option Explicit).

In this sub, I declare my Workbook variable as:

Sub needsWorkbookType()
    Dim notCompile As workbook
End Sub

However, because I am running this from CorelDraw, I do not have access to Excel's types. When I try to run the sub, I get a compile error:

  • User-defined type not defined

I know I can get around this by turning off explicit declaration, and just not declaring any of the Excel variables. The interpreter will dynamically create them and it will work.

But I would prefer to be able to declare them.


My system is:

  • Windows 8.1
  • Excel 2013
  • Corel X6

Solution

  • You need to add references to Microsoft Excel 14.0 Object Library for early binding. Go to Tools>>References and find the library in the list and tick the box. If you have an older version of Excel the number 14 may be lower but that's not a huge deal as Excel object model hasn't changed that much over the years...

    With references added you can say:

    Dim xlApp as new Application.Excel

    You could also try late binding if you have trouble adding references:

    Dim xlApp as Object 
    Set xlApp = CreateObject("Excel.Application")
    

    but try going with early binding as it gives you intelli-sense and you can handle any errors a lot better.


    If you have Excel 2013 installed, then you would add reference to Microsoft Excel 15.0 Object Library.

    If it does not show up in the list, then you can browse to it, and install it that way.

    In order to find out what file to browse to, you can open up the Excel VBA editor, and look at the same list in this editor - because you are already in Excel, the library will already be added and will show you the path name, as below:

    enter image description here

    Switch back to the Corel VBA editor, and browse to that library.

    Early binding will now be activated.