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.
You must not declare everything as Object!
I have no clue which VBA you are using, I tested it with Excel 2013. Here I faced even earlier a problem. But this was clear to me, because your script created – in my case – an Excel-Object. And Excel does not know many of the CorelDraw.Events, methods and properties.
So you first have to declare in your 'Master'-VBA the CorelDraw you are using. Go for that in VBA under Tools/References to the CorelDraw-Version in question and activate this reference.
Then you have to change your undefined objects in CorelDraw – Definitions. Then it should run:
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.
try to use ExportBitmap instead of ExportEx. ExportBitmap does not require StructExportOptions
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...
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:
Private Sub Test_ExportEx_oDoc_as_Object()
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)
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...