Hi Anthony,
Indeed, it's all getting quite complicated. Any help you can offer
with creating better documentation and samples would be appreciated. I
believe programmers at all levels should prefer .NET over VBA, and
eventually I hope we will have something that makes this viable for
all tasks. Of course getting to the effortless integration with Excel
still needs work. I like the .dna samples because the are completely
self-contained in a single text file.
If you are stuck getting a particular example or feature to work, I'm
happy to help - this is the right place to ask.
If you post a public sample add-in where you have tried to incorporate
the features and interactions you need, (maybe on GitHub or
somewhere), I'd be happy to help fix it up and get everything working.
=================================================
Back to you more specific queries...
1. COM Server
-------------
I think you are being distracted by the Excel-DNA COM Server feature.
This was introduced in v 0.29, and allows you to:
* create COM libraries inside your Excel-DNA add-in, that can be
instantiated and used directly as COM objects from VBA, and
* create RTD servers that are instantiated by Excel directly, without
having the Excel-DNA add-in loaded, which is needed to support the
'old values' feature of RTD>
Apart from these two specialised cases, you need not bother with the
COM Server stories. Certainly for porting from VSTO, you do not need
this.
Calling regsvr32 to register an Excel-DNA COM Server does not require
admin rights (we write to the user's hive if need be). Registration of
the COM Server can also be done automatically when your add-in is
loaded - then the add-in must be loaded before the VBA code runs.
As you say you can most easily call into your Excel-DNA add-in from
VBA by Application.Run("MyMacro") where MyMacro is a static void
method in your add-in, or a function or whatever.
I'm not sure what this means: "Is it possible to run a local COM
server inside an XLL, like I now do with VSTO?"
If you mean accessing some third-party COM server from your .NET code,
the answer is probably 'Yes'. That part has nothing to do with VSTO
though.
2. Excel APIs
-------------
Excel has a C API and the COM object model. You can fully use both
from within your Excel-DNA add-in. Some calls are not allowed by Excel
in some contexts, for example you can't call the C API from a Ribbon
handler. But from a ribbon handler you can call into a macro using
Application.Run, and then call the C API from inside the macro.
The entry point into the COM object model is via the
ExcelDnaUtil.Application property, which gives you the correct
instance of the Application object, matching the Excel instance your
add-in is running in.
The full C API is exposed in Excel-DNA via the XlCall class, using the
ExcelReference type to represent regions of a sheet. The C API does
not have the full capabilities of the COM object model. Whether it is
faster or not in a particular case needs to be tested, often it is not
faster. The C API is exposed through the XlCall class, using the
ExcelReference type to represent regions of a sheet.
I'd recommend the following:
* use the C API inside user-defined function,
* generally use the COM object model for 'macro' type work, like
applying formatting, creating new sheets, etc.
* sometimes use the C API (via the ExcelReference class) in a macro to
read / write large amounts of data.
3. Ribbon and Custom Task Pane support
--------------------------------------
Excel-DNA has support for Ribbon and Custom Task Panes without
requiring admin rights or registration before running your add-in. The
required COM registration is done at runtime, and into the user's hive
in the registry if need be.
The Custom Tasl Pane support is via a single call to
var ctp =
CustomTaskPaneFactory.CreateCustomTaskPane(typeof(MyUserControl), "My
Super Task Pane");
where MyUserControl is a System.Windows.Forms.UserControl, hosting
your WPF stuff.
The MyUserControl class must be marked [ComVisible(true)] or in an
assembly that is marked [assembly:ComVisible(true)] - the default
Visual Studio projects sets it up as [assembly:ComVisible(false)] in
AssemblyInfo.cs.
The MyUserControl class will be registered and loaded as needed at
runtime - you should _never_ set your Excel-DNA project to 'Register
for COM interop'. You need no pre-registration of you assemblies or
add-ins for the CTP support to work.
I'm not sure what exactly you mean by:
"In any case I need to access the Task Pane (that being the whole
point), which needs? the addin approach to work."
4. ExcelComAddIn helper
-----------------------
You should ignore this initially. Initially this class was made as a
helper for the Ribbon support, but it can also give you a way to get
the COM Add-In events, allow you to deal properly with Excel shutdown
(the C API has some bugs that makes this difficult).
=================================================
Eventually your life will probably be easiest by porting you whole VBA
add-in to
VB.NET.
Patrick O'Beirne has written up some of his experiences doing this:
http://sysmod.wordpress.com/.
I suggest if you have a follow-up questions on any of these aspects,
you ask as a new thread on the group - these catch-all discussions
tend to get lost easily.
Good luck,
Govert