locked
C# WPF Closing Webbrowser Hosting Excel RRS feed

  • Question

  • I am using the Webbrowser to open an Excel file from within a C# WPF application.

    When I attempt to close Excel I get the error "Excel ran out of resource while attempting to calculate one or more formulas. As a result, these formulas cannot be evaluated. I am using the following code to close Excel, any ideas what is going wrong?

    Microsoft.Office.Interop.Excel.Application excelApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application") as Microsoft.Office.Interop.Excel.Application;
    if (excelWebBrowser != null)
    {
        Microsoft.Office.Interop.Excel.Workbook xlWorkBook = excelWebBrowser.webBrowser.Document as Microsoft.Office.Interop.Excel.Workbook;
        webBrowser.Navigate("about:blank");
        xlWorkBook.Close(false, Missing.Value, Missing.Value);
        Marshal.ReleaseComObject(xlWorkBook);
        xlWorkBook = null;
    
            excelApp.Quit();
            Marshal.ReleaseComObject(excelApp);
            excelApp = null;
    
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }

    I also tried a few variants on this using webBrowser.Dispose() but get the same error.

    Thanks!


    Saturday, March 31, 2018 7:25 PM

Answers

  • Thanks for the thoughts.  I don't think the problem is not using the LoadCompleted event, the point at which I want to close the document is much later, after the user has finished manipulating the spreadsheet.

    After some more experimenting, the error only occurs when I try to close spreadsheets that have #VALUES cells in them, other spreadsheets are fine.  It turned out to be an XLL function that got upset which I have now resolved.

    Regarding whether it is appropriate to use a WebBrowser control, there is no feasible alternative currently imo.  ActiveX embedding is the officially supported method except MS do not provide a working example since 2008 (DSOFRAMER), the SetParent approach gives an unresponsive GUI, and the commercial alternative OFFICEOCX does not seem to work in 64bit and they do not reply to support emails (at least not to mine for the last month).

    My view is that WebBrowser is the only approach that works reliably now, although annoyingly you have to set 

    Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Excel.Sheet.12 (and variants for different file types) BrowserFlags from to value 80000A00 for it to be an embedded window.

    Sunday, April 1, 2018 6:12 AM

All replies

  • It seems totally unnecessary to use a WebBrowser control for that. If you do use a WebBrowser control then when you do a navigate then everything you do to the document after the navigate should be done in a DocumentCompleted event. That might be the problem.

    I don't know if use of Marshal.ReleaseComObject can be a problem. I also don't kow if calling the garbage collector could be a problem. As far as I know none of that is necessary and quite often when we do things like that because we think we know better than the specialists at Microsoft, we learn that we don't know better.



    Sam Hobbs
    SimpleSamples.Info

    Saturday, March 31, 2018 8:35 PM
  • Thanks for the thoughts.  I don't think the problem is not using the LoadCompleted event, the point at which I want to close the document is much later, after the user has finished manipulating the spreadsheet.

    After some more experimenting, the error only occurs when I try to close spreadsheets that have #VALUES cells in them, other spreadsheets are fine.  It turned out to be an XLL function that got upset which I have now resolved.

    Regarding whether it is appropriate to use a WebBrowser control, there is no feasible alternative currently imo.  ActiveX embedding is the officially supported method except MS do not provide a working example since 2008 (DSOFRAMER), the SetParent approach gives an unresponsive GUI, and the commercial alternative OFFICEOCX does not seem to work in 64bit and they do not reply to support emails (at least not to mine for the last month).

    My view is that WebBrowser is the only approach that works reliably now, although annoyingly you have to set 

    Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Excel.Sheet.12 (and variants for different file types) BrowserFlags from to value 80000A00 for it to be an embedded window.

    Sunday, April 1, 2018 6:12 AM