none
SaveAs method throws exception RRS feed

  • Question

  • I'm working on a .NET program to interface with Excel. The code looks like this:

    private static void ConvertToXls(string xlsxFn, string xlsFn)
    {
                var Excel = new Microsoft.Office.Interop.Excel.Application();
                var wkb = Excel.Workbooks.Open(xlsxFn, false, true);
                wkb.SaveAs(xlsFn, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel9795);
                wkb.Close();
                Excel.Quit();
    }

    As you can see, it is simply used to convert an .xlsx file to a .xls file (Excel 97-2003 format).

    When I run this code it throws exception:

    System.Runtime.InteropServices.COMException was unhandled
      HResult=-2146827284
      Message=Exception from HRESULT: 0x800A03EC
      Source=testcs
      ErrorCode=-2146827284
      StackTrace:
           at Microsoft.Office.Interop.Excel._Workbook.SaveAs(Object Filename, Object FileFormat, Object Password, Object WriteResPassword, Object ReadOnlyRecommended, Object CreateBackup, XlSaveAsAccessMode AccessMode, Object ConflictResolution, Object AddToMru, Object TextCodepage, Object TextVisualLayout, Object Local)
           at testcs.Program.ConvertToXls(String xlsxFn, String xlsFn) in c:\Users\Redirection\brittg2\Documents\Visual Studio 2012\Projects\testcs\Program.cs:line 111
           at testcs.Program.Main(String[] args) in c:\Users\Redirection\brittg2\Documents\Visual Studio 2012\Projects\testcs\Program.cs:line 100
           at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException: 

    While troubleshooting, I ran the steps in my code interactively in Excel. They worked as expected and the converted file appeared in the destination directory.

    Any ideas what's going on here?


    • Edited by G Britton Wednesday, August 5, 2015 1:42 PM edit
    Wednesday, August 5, 2015 1:41 PM

Answers

  • Hi G Britton,

    I made a test with your code, and I got the same error with you. But I got the error even when I debug step by step. XlFileFormat.xlExcel9795 is Excel version 95 and 97, and based on the link below, it has been deprecated.

    # Deprecated features for Excel 2007
    https://blogs.office.com/2006/08/24/deprecated-features-for-excel-2007/

    To achieve your requirement, you could use XlFileFormat.xlExcel8, a simple code as below:

    wkb.SaveAs(xlsFn, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel8);

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Marked as answer by G Britton Thursday, August 6, 2015 1:24 PM
    Thursday, August 6, 2015 2:20 AM

All replies

  • Hello G,

    Do you run the code on the main thread? The fact is that Office applications use the single-threaded apartment model and you should use the main thread for calling property and methods of Office applications.

    Wednesday, August 5, 2015 2:57 PM
  • Yup. running on the main thread
    Wednesday, August 5, 2015 2:58 PM
  • Hi G Britton,

    I made a test with your code, and I got the same error with you. But I got the error even when I debug step by step. XlFileFormat.xlExcel9795 is Excel version 95 and 97, and based on the link below, it has been deprecated.

    # Deprecated features for Excel 2007
    https://blogs.office.com/2006/08/24/deprecated-features-for-excel-2007/

    To achieve your requirement, you could use XlFileFormat.xlExcel8, a simple code as below:

    wkb.SaveAs(xlsFn, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel8);

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Marked as answer by G Britton Thursday, August 6, 2015 1:24 PM
    Thursday, August 6, 2015 2:20 AM
  • Thanks Edward, you are correct about the deprecation.  However, note that the enumeration still contains a member that will cause an exception if used.  I filed a Connect item about this.  Really, xlExcel9795 should be removed from the enumeration.

    Connect item:

    Excel application crash using SaveAs Method with Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel9795

    Thursday, August 6, 2015 1:23 PM
  • Hi G Britton,

    Thanks for your feedback.

    If you have any other developing issues about Excel, please feel free to post in this forum.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, August 7, 2015 1:41 AM