Search code examples
.netexcelms-wordvstoole

Embed Excel as OLE Object in MS Word showing Error


I have created a VSTO for MS Excel and MS Word as well. I am trying to embed OLE Object of Excel in my word document. It works for me for New Document. Now I saved this document somewhere on my hard drive (for eg C:\ drive). Now I am trying to open that word document from File -> Open . Now when I double click on table that is Excel OLE object , it shows me Message like this... enter image description here I think there is no issue in my VSTO. I have disabled both my VSTO and follow the same procedure. But failed again. Has anyone tried doing this before successfully. Please assist me with sample code.


Solution

  • Surround all your Application_Workbook code with try-catch-finally and release the workbook object. eg.

    void Application_WorkbookOpen(Excel.Workbook Wb) { 
        try {
            // Do Stuff
        } catch (Exception) {
            // Probably Eat it
        } finally {
            Marshal.ReleaseComObject(Wb);
        }
     }
    

    Here is a reference ...

    https://theofficecontext.com/2012/10/09/vsto-and-comole/