Hello
I am using CorelDRAW API within my VBA Application since years.For easier compiling and different version issues I recently changed from early to late binding. So now there is no reference to CorelDRAW.tlb necessary. After rebuilding hundreds of subs/functions everything works fine. But at the end, when exporting a document, the error 13 "type missmatch" occurs. I made many tests but could not find a problem in my code. Maybe this is a bug in the API itself?
Below a simple code, which could be tested in CorelDRAW VBA. From my point of view this code should normally work.
Private Sub Test_ExportEx_oDoc_as_Object() 'Late Binding Dim oApp As Object Dim oDoc As Object Dim oExpOpt As Object Dim oExpFlt As Object Dim sFile As String Set oApp = Application Set oDoc = oApp.ActiveDocument Set oExpOpt = oApp.CreateStructExportOptions oExpOpt.UseColorProfile = True sFile = "C:\temp\Test.eps" Set oExpFlt = oDoc.ExportEx(sFile, cdrEPS, cdrCurrentPage, oExpOpt) With oExpFlt .Finish End With End Sub
Has anybody experienced the same problem or a workaround? Please also respond if you can confirm, that this is likely might be a problem of the API itself.
I tried this with X7 and 2020.
Best RegardsKlaus
Private Sub Test_ExportEx_oDoc_as_Object()'Late BindingDim oApp As CorelDRAW.ApplicationDim oDoc As CorelDRAW.DocumentDim oExpOpt As CorelDRAW.StructExportOptionsDim oExpFlt As CorelDRAW.ExportFilterDim sFile As StringSet oApp = CorelDRAW.ApplicationSet oDoc = oApp.ActiveDocumentSet oExpOpt = oApp.CreateStructExportOptionsoExpOpt.UseColorProfile = TruesFile = "C:\temp\Test.eps"Set oExpFlt = oDoc.ExportEx(sFile, cdrEPS, cdrCurrentPage, oExpOpt)With oExpFlt.FinishEnd WithEnd Sub
Thank you for your answer. But as I wrote, all is about late binding. So with late binding you do not have any Corelobjects available - you have to define Corelobjects like CorelDRAW.Application or .Document as a "general" object. What you are showing is early binding, with a reference (Verweise).
So if you are using Excel, the you just have to change in my script the line Set oApp = Application to Set oApp = CreateObject("CorelDRAW.Application")
Then you are using late binding and you do not need to have a reference to a Corel Library.
The advantage of late binding is, that your (compiled) code will also run on machines without installed CorelDRAW. So you can add an error routine to tell the user, that he has to install Corel first. With early binding your complete code will likely not run a line, without having Corel installed.
The pseudo advantage of using late binding does not mean that the code will run on computers not having Corel installed... It can be working independent of the installed Corel version, indeed. You will have a missing reference to be changed. But you looses the advantage of intellisense, so I would not recommend it. The fact that it runs some lines should be irrelevant, I think...
Now, please specify in which application do you use that VBA code. Don't you use it from outside Corel? If yes, how `Set oApp = Application` can work?
Anyhow, coming back to your question, without a reference to Correl COM object, the code does not know anything about the function constants parameters as `cdrEPS` and `cdrCurrentPage`. If you create those constants as `Const cdrEPS As Long = 1289` and `Const cdrCurrentPage As Long = 1` the code may work, I think. But if you try the code inside Corel, only to avoid the `oApp` object creation, Corel should recognize the above constants and I cannot understand why it does not work...
Edited: Now I could see that the code has been adapted in a way to work under Corel environment. I will try testing it...
Tested...
I am afraid it is not a matter of late binding...
You simple missed the `paletteOption` parameter. In order to work, you should use:
`Set oExpFlt = oDoc.ExportEx(sFile, cdrEPS, cdrCurrentPage, oExpOpt, pal)`
This is a piece of working code, tested in Excel:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Private Sub Test_ExportEx_oDoc_as_Object() 'Late Binding Const cdrEPS As Long = 1289 Const cdrCurrentPage As Long = 1 Const cdrPaletteOptimized As Long = 3 Dim oApp As Object, oDoc As Object, pal As Object Dim oExpOpt As Object, oExpFlt As Object, sFile As String Set oApp = CreateObject("CorelDRAW.Application.22") 'find Corel 2020 session Set oDoc = oApp.ActiveDocument 'an activedocument must exist... Set oExpOpt = oApp.CreateStructExportOptions Set pal = oApp.CreateStructPaletteOptions: pal.PaletteType = cdrPaletteOptimized oExpOpt.UseColorProfile = True sFile = "C:\temp\Test.eps" Set oExpFlt = oDoc.ExportEx(sFile, cdrEPS, cdrCurrentPage, oExpOpt, pal) oExpFlt.Finish End Sub
I adapted the above code. I observed that I used to have a reference to Corel in my test Excel workbook and it could interpret the constants without a preliminary definition. Now, after removing the Corel reference, it needs to also `clarify` what `cdrPaletteOptimized` constant means...
Hello FaneDuru
Thank you for your very helpfull comments!! Yes I do use it outside Corel within an Access VBA. Beside other things, with this Application I do also automatise almost everything like SAP, CorelDRAW, PHOTOPAINT, almost all Office 365 Apps and others. Now, if you use early binding you have to set references to almost all of these Apps. A client who does not have all referenced Applications installed lead to broken references and that has also a bad effect to the rest of the App. Having late bindung, everything works and you can exclude some modules/functionalities "on-the-fly" for that client.
It is also correct that you have to define all needed Corel ENUMs manually for late binding. To make the Test Sub as simple as possible, I just tried it within Corel VBA....
If you use conditional compiling within your code you can still use intellisense for programming. This makes programming a bit more complicated, but without intellisense programming with these many APIs would be almost not possible.
But, your comment about a missing parameter was very very helpfull! However the missing 5th parameter is the PaletteOptions and due to documentation (Document.ExportEx method - Developer Area - CorelDRAW Community) this parameter is optional. Due to that I have not even thought about adding this parameter. I also never recognised such a different behavior with other API and also with other COREL functions.
I have already added the PaletteOptions Parameter in my App and now also the ExportEx function works.
Thank you very much for that hint!!