none
Excel Hangs on Save() RRS feed

  • Question

  • Hi,

    Occasionally the below code just hangs on workbook.save();

    I do not know why it does this. If I rerun the process it works and seems to occur randomly.

    Any ideas why this might be?

    Thanks!

    private string Build(IWorkbookBuilder workbookBuilder)
    {
    Application excelApplication = null;
    Workbook workbook = null;
    try
    {
    	File.WriteAllBytes(temporaryFilePath, workbookBuilder.Template);
    
    	excelApplication = new Microsoft.Office.Interop.Excel.Application();
    	workbook = excelApplication.Workbooks.Open(temporaryFilePath);
    
    	workbookBuilder.Populate(excelApplication);
    
    	Cleanup(excelApplication);
    
    	_logger.Debug("Saving...");
    	workbook.Save();
    	_logger.Debug("Saving Done");
    }
    
    public void Cleanup(Microsoft.Office.Interop.Excel.Application excelApplication)
    {
    	excelApplication.Run("DeleteClassTemplate");
    }

    Monday, November 28, 2016 9:35 AM

Answers

  • Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

    If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.

    As a workaround you may consider using the Open XML SDK if you deal with open documents only, see Welcome to the Open XML SDK 2.5 for Office for more information. Or just any other third-party components designed for the server-side execution.  


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Monday, November 28, 2016 8:11 PM
  • I suspect that probably Excel is displaying a dialog box when .Save() is being called, possibly asking for confirmation in overwriting, or perhaps some other error message. The problem is that the dialog box is being displaying on the desktop corresponding to the identity of the web service, not on the interactive desktop.

    This is a common problem with trying to drive Excel in the background through automation, and is the primary reason why you shouldn't be doing this: it is brittle and unreliable.

    Eugene is correct: you should be coding this through Open XML instead.

    Monday, November 28, 2016 8:23 PM

All replies

  • Hello,

    Where and when do you run the code shown above? Is it a windows service or asp.net application?

    > excelApplication = new Microsoft.Office.Interop.Excel.Application();

    Why do you need to create a new Application instance?

    Note, if you develop a VSTO add-in you need to use the Application property of the add-in class (ThisAddin.cs) which is available everywhere by using the Globals static class. See Global Access to Objects in Office Projects for more information.

    Have you tried using the SaveAs method instead?


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Monday, November 28, 2016 1:19 PM
  • Hello,

    Where and when do you run the code shown above? Is it a windows service or asp.net application?

    > excelApplication = new Microsoft.Office.Interop.Excel.Application();

    Why do you need to create a new Application instance?

    Note, if you develop a VSTO add-in you need to use the Application property of the add-in class (ThisAddin.cs) which is available everywhere by using the Globals static class. See Global Access to Objects in Office Projects for more information.

    Have you tried using the SaveAs method instead?


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Hi,

    It's a web service running on windows. Not sure why that code is used as I did not create it and no documentation. It looks like it was taken from Microsoft example...

    https://support.microsoft.com/en-us/kb/302084


    • Edited by obrienkev Monday, November 28, 2016 5:25 PM
    Monday, November 28, 2016 5:24 PM
  • Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

    If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.

    As a workaround you may consider using the Open XML SDK if you deal with open documents only, see Welcome to the Open XML SDK 2.5 for Office for more information. Or just any other third-party components designed for the server-side execution.  


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Monday, November 28, 2016 8:11 PM
  • I suspect that probably Excel is displaying a dialog box when .Save() is being called, possibly asking for confirmation in overwriting, or perhaps some other error message. The problem is that the dialog box is being displaying on the desktop corresponding to the identity of the web service, not on the interactive desktop.

    This is a common problem with trying to drive Excel in the background through automation, and is the primary reason why you shouldn't be doing this: it is brittle and unreliable.

    Eugene is correct: you should be coding this through Open XML instead.

    Monday, November 28, 2016 8:23 PM
  • Hi obrienkev,

    I think that the suggestion given by the Eugene  Astafiev and Brian Muth is appropriate suggestion.

    so as they suggested please try to use open xml instead of using interop.

    if you think that suggestion given by them can be the answer then please mark their suggestions as an Answer.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 29, 2016 2:22 AM
    Moderator