0

SO I ran into a problem using the WorkbookOpen event when in an ole embedded scenario: its a well documented issue:

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:

noztol
  • 494
  • 6
  • 25

1 Answers1

0

So this answer was really good: https://stackoverflow.com/a/349202/ Don't like to have to specify using for every COM object but other than that the code is pretty clean.

Community
  • 1
  • 1
noztol
  • 494
  • 6
  • 25