Dynamically referencing CorelDRAW 22 Type Library using its GUID on a machine with 21.3 still available.

Well, there isn't a forum for 2020 yet so I am posting here in 2019.

I have a bunch of Excel Applications I've written in VBA that, among other Excel related tasks, automate Corel using the data provided in the spreads.

Because the data in the Excel files are populated by people on machines that do not have Corel installed on them, then passed to machines with Corel, I am forced to dynamically reference the CorelDRAW Type Libraries with something like the code below.

Function CorelRefAdded() As Boolean

' Iterate through the VBProjects references checking their GUIDs. Returns true if CorelDRAWs GUID is found. i.e; reference already added.

Dim ref As Reference

For Each ref In ThisWorkbook.VBProject.References

If ref.GUID = "{FBF4300F-D921-11D1-B806-00A0C90646A9}" Then

CorelRefAdded = True

Exit Function

End If

Next ref

CorelRefAdded = False

End Function
Public Sub Inits()


' If a reference to the CorelDRAW Type Library is not found then attempt to create a late bound CorelDRAW.Application object.
' If Corel is not installed on the machine this will throw an error and execution will jump over the Corel related initialisation code
' and disable all Corel related controls in the Excel Application so users without CorelDRAW don't see controls they can't use.
' If an object IS able to be instantiated then add the reference to the CorelDRAW Type Library to the project using its GUID.

On Error GoTo err

If Not CorelRefAdded() Then

Set corel = CreateObject("CorelDRAW.Application")
Set corel = Nothing

ActiveWorkbook.VBProject.References.AddFromGuid "{FBF4300F-D921-11D1-B806-00A0C90646A9}", 1, 0

End If

Call VerifyFileAndFolder_OnOpen

Call ControlInits

Exit Sub

err:

Call DisableCorelControls

End Sub

I have just installed 2020 and for some reason, the GUID is the same for the 21.3 and 22 Type Libraries. This means that when I try to add the reference to the project it just adds the old library not the new one. 

What is going on here?

The code still works, and when run it will open the 2020 version (yes I have both versions installed for now).

I don't know what differences there are between the 21.3 and 22 DLL files but what will happen if during development I am referencing the new library and I use a type or function that isn't defined in the old library or if the function definition has changed slightly and then when I deploy, because I need to remove the reference so it can be added dynamically only if CorelDRAW is installed on the machine that opens the Excel Application, and it adds per the GUID which is the same for both versions but from what I've seen so far, it adds the old one not the new one? I am going to get an error saying that the user defined type isn't found or that there are an incorrect amount of parameters, or worse yet, the parameter order has been changed (unlikely I know, but CAN happen) and I don't get an error, just incorrect results!

P.S

On the off chance that somebody may want to use my code, please note that the Reference type used in the CorelRefAdded() function is part of the Microsoft Visual Basic for Applications Extensibility X.X library, where X.X is the version number. My version is 5.3, you may have a different version, regardless, you need to add that reference to your project.

  • Problem solved.

    The call to add the reference from GUID needs to include the version number in it.

    I just had ActiveWorkbook.VBProject.References.AddFromGuid "{FBF4300F-D921-11D1-B806-00A0C90646A9}", 1, 0

    But I needed ActiveWorkbook.VBProject.References.AddFromGuid "{FBF4300F-D921-11D1-B806-00A0C90646A9}", 22, 0

    When I went from X6 to 2019 the entire GUID changed, and because I didn't have multiple versions of the library with the same GUID like I do now having the 1 for the second parameter worked, but now that I have the different versions I need to differentiate between them and the 22 refers to the Major version. technically, I should have had 21, 3 there before for the 21.3 type library.