SO I ran into a problem using the WorkbookOpen event when in an ole embedded scenario: its a well documented issue:
- https://blogs.msdn.microsoft.com/mshneer/2008/10/18/com-interop-handling-events-has-side-effects/
- https://blogs.msdn.microsoft.com/rcook/2008/10/30/iconnectionpoint-and-net-or-how-i-learned-to-stop-worrying-and-love-managed-event-sinks-part-1/
reading this blog post https://blogs.msdn.microsoft.com/vsofficedeveloper/2008/04/11/excel-ole-embedding-errors-if-you-have-managed-add-in-sinking-application-events-in-excel-2/
I not only need an IConnectionSink but also call Marshal.ReleaseComObject on each workbook object passed as a parameter.
public class ExcelAppEventSink : Excel.AppEvents
{
... other functions ...
public void WorkbookOpen(Excel.Workbook Wb)
{
<-- operate on workbook -->
Marshal.ReleaseComObject(Wb);
}
... other functions ...
}
But another blog post from microsoft posted 2 years later claims Marshal.ReleaseComObject is dangerous. https://blogs.msdn.microsoft.com/visualstudio/2010/03/01/marshal-releasecomobject-considered-dangerous/
since I just need to release the RCWs would it be safer in 2016 to call CleanupUnusedObjectsInCurrentContext at the end of the function call instead?
extra reading material: