excel-dna

Excel-dna Add-in opens Excel in the background after closing Excel


I wrote an Add-In using Excel-DNA. Testing it in different Excel Versions (2016,365 on different PC with Win 10) with Excel-Add-Ins getting the .xll Works fine - but when Excel is closed, a hidden Excel Instance is created/kept? Which one has to be killed by task manager. (Or reopening Excel will not open with add-ins)

When I unload the add-in, Excel closes completely.

A strange thing is, that the Add-In is shown in the Excel Add-Ins also in the com Add-Ins

What could be the reason for that (mis)behaviour and how can I solve this.

Thankful for every advice

P.S. ExcelDna 1.6.0, .Net Framework 4.7.2, vb.net


Solution

  • What happens is that the Excel process is not terminating because there are outstanding COM references. From your Excel-DNA add-in, this can happen easily if you access the Excel COM object model from a thread that is not the main thread (ManagedThreadId==1). From an Excel-DNA add-in you should only ever access the COM object model from the main thread.

    Some examples that cause problems would be:

    In some of these cases you can switch to the main thread in a context where it is safe to call the COM object model with a call to the Excel-DNA helper method ExcelAsyncUtil.QueueAsMacro. This can be called from any thread, and the code will execute as soon as Excel is available.