Thanks for your help.
Best Regards.
Emmanuel (Lyon - France)
Hi Emmanuel,
You say:
> It works fine on my computer, but I am not sure that it will work on others.
> Using Microsoft.Office.Interop.Excel 16.0 (present on my computer), the application will not work on computer having an ealier version of Excel (2013 or 2010).
It should in fact work fine on other computers. If your Reference to the Microsoft.Office.Interop.Excel assembly has the “Ember Interop Types” set to “true” then you don’t have to distribute anything extra for the COM interop to work.
You should find that, as long as you use the parts of the object model that are present in the older Excel versions, that the code runs on old version too – this is exactly like VBA. If you write VBA code in Excel 2016 it will work on Excel 2010 as long as you don’t use features that are new only for Excel 2016. But it sounds like you are using standard types like Workbook that will be compatible with old versions too.
So your first piece of code is good and should work under old versions too.
If you’re worried about compatibility, you can remove the reference to the version 16.0 object model, and add the NuGet package “ExcelDna.Interop” which includes only the Excel 2010 object model, so anything you see on the IntelliSense should already be present in Excel 2010. But you don’t need to do that for it to work, it just protects you from mistakenly using something that was added in the newest version.
For macros, the C API (which you use through the XlCall call) is more limited than the COM object model, since Microsoft hasn’t really updated it in 20 years.
There is an overview and links to the only help file that exists for the XlCall methods from here: https://github.com/Excel-DNA/ExcelDna/wiki/Excel-C-API
-Govert
Thanks Govert.
Emmanuel
ExcelDna.Interop is a NuGet package that adds a reference to the older version of Microsoft.Office.Interop.Excel.
For macros that run from a ribbon button or event handlers, the COM interface is more powerful than the C API and usually the better way to do it.
Excel-DNA internally uses the C API because that’s the best way to add user-defined functions to Excel. For your own add-in you typically don’t have to use the C API, since Excel-DNA has implemented all the integration already.
There are very few examples of using the C API (XlCall methods) from C#. You can search on GitHub and StackOverflow to try to find some.
Here’s an example of using the XlCall methods to read and write from a range on a worksheet:
https://stackoverflow.com/a/3868370/44264
But it is indeed obscure. The reference for the C API is the Excel SDK: https://msdn.microsoft.com/en-us/library/office/bb687883.aspx together with the macro help files I link to from the GitHub page here: https://github.com/Excel-DNA/ExcelDna/wiki/Excel-C-API
If you have used the C API in C++ then using it from C# is very easy. Otherwise it is not easy.
-Govert
--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
exceldna+u...@googlegroups.com.
To post to this group, send email to exce...@googlegroups.com.
Visit this group at https://groups.google.com/group/exceldna.
For more options, visit https://groups.google.com/d/optout.