none
SaveAs method of Workbook class failed RRS feed

  • Question

  • Hello,
     I am getting error 1004 - "SaveAs method of Workbook class failed" - from Workbook.SaveAs method. The error occurs regularly under specific conditions described below.

     My application (MyApp) creates an instance of Excel.Application ActiveX object and after adding a workbook to the Excel.Application object and filling in its sheet saves the workbook by calling the SaveAs method. MyApp process gets created by a scheduler application (SchedApp) which can run as a Windows service or a standard application. 

     If MyApp is spawned by SchedApp running as a Windows service, the call to the Workbook.SaveAs method fails returning 1004 - "SaveAs method of Workbook class failed".
     If MyApp is spawned by SchedApp running as a standard application the call to Workbook.SaveAs method succeeds and produces .xsls file.

     Following is the list of tests I have made so far to find out the cause of the SaveAs failure:

     1) SchedApp as a service runs under Local System account (NT AUTHORITY\SYSTEM). I changed the account under which the service runs to an account which belongs to \Administrators group. The new account was the same account under which SchedApp runs as a standard application when call to the  SaveAs method succeeds. SaveAs method failed anyway.

     2) Excel.Application and other Excel ActiveX objects are hosted by out-of-process COM server (EXCEL.EXE). This fact made me believe the cause of the SaveAs error is in privileges granted to the EXCEL.EXE out-of-process server which are too low to enable the Workbook instance to write into the output path. In order to verify I created my own out-of-process COM server whose test object creates a file named identically to the .xsls file and in the same path as the path of .xsls file SaveAs is supposed to create.
     I updated MyApp's code to create an instance of the test object and attempt to create the output file. The test object successfully created and wrote arbitrary text to the .xsls file even if MyApp was spawned by SchedApp running as a service.

     3) I changed the output path where SaveAs is supposed to create .xsls file to the temporary file folder specific for the account under which MyApp is running. SaveAs method failed anyway.

     4) I changed element in EXCELL.EXE manifest file (excel.exe.manifest) from

        <requestedExecutionLevel level="asInvoker" uiAccess="false">
     to 

       <requestedExecutionLevel level="requireAdministrator" uiAccess="false">.

     SaveAs method failed anyway.

     5) In order to rule out possibility of the error being data dependent I removed  the part of code in MyApp which fills in the Excel sheet so the output .xsls file would constitute an empty sheet. SaveAs method failed anyway.

     6) I tested the aforementioned points (1-5) on Windows 2003 Server R2, Windows Vista, Windows 7, Windows 2012 R2. Save for Windows 2003 Server R2, the SaveAs method failed on all OSs - i.e. failed on Windows Vista, Windows 7, Windows 2012 R2. The only exception was on Windows 2003 Server R2 where the SaveAs method succeeded no matter whether SchedApp was running as a service or a standard application.

     The bottom line seems to be the Workbook.SaveAs method fails whenever MyApp process gets created by SchedApp running as a service (with the exception of Windows 2003 Server R2 mentioned in point 6). 

     Would you know why the SaveAs method fails when SchedApp is running as a service and hot to make it work?

     Thank you,
     Radovan

                    
    Tuesday, October 11, 2016 3:51 PM

Answers