Hi Stefan,
It sounds like a complicated mix you have. Simplifying the
interactions, e.g. moving more stuff into the .NET side, or not
accessing Excel from the .NET COM exposed components, might be the
easiest way to stabilise things.
Excel-DNA does very little intervention when you use the COM
automation interface. The main Excel-DNA specific issue is how the COM
objects, particularly the root Application object, gets marshalled
between different apartments. E.g. there is a big difference whether
you use the COM model from the main thread in an Excel-DNA add-in only
- which always seems to clean up properly even if there are
outstanding references, vs. using the COM interface from another
thread, where you have to clean up each and every COM interface before
the Excel process will close properly.
Anyway, I have some ideas you can explore:
The ways ExcelDnaUtil.Application gets that Application object might
mean that it gets marshalled into the .NET apartment from the main
Excel thread. I suspect if you are calling Excel-DNA from VBA, then
calling ExcelDnaUtil.Application, you are effectively getting a
marshalled version of the COM interface. Avoiding that might help.
Correlating threads between the Excel-DNA add-in's AutoOpen and the
VBA calls might shine some light.
You can experiment with getting the Application object in different
ways:
1. Get and keep it in your AutoOpen.
2. Retrieve it every time you need it.
3. Add an ExcelRibbon-derived class or ExcelComAddIn-derived class
(with the workaround discussed here:
http://groups.google.com/group/exceldna/browse_thread/thread/143bd77dec8e6579
or the latest version from the CodePlex source). This will keep the
Application object passed to the COM connect event, and pass this back
every time you call ExcelDnaUtil.Application from the main thread.
Now, your crash sounds a lot like the problems that arise when
embedding into other Office apps. This was discussed here:
http://groups.google.com/group/exceldna/browse_thread/thread/7374ccad1fae0436
with some resolution here:
http://groups.google.com/group/exceldna/browse_thread/thread/5b5e5948f46d402a.
I think the conclusion is that using NetOffice (http://
netoffice.codeplex.com) gives you a way to manage the outstanding COM
references better.
But I still vote for just simplifying stuff a bit.
I look forward to hearing what you discover.
Regards,
Govert