SaveAs method throws exception

gbritton

New Member
Joined
Jul 31, 2015
Messages
2
I'm working on a .NET program to interface with Excel. The code looks like this:

Code:
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 104
at testcs.Program.Main(String[] args) in c:\Users\Redirection\brittg2\Documents\Visual Studio 2012\Projects\testcs\Program.cs:line 95
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.

I should mention that the destination directory is on a different computer. The second argument to the function is given as a UNC path.

Any ideas what's going on here?
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

Forum statistics

Threads
1,215,055
Messages
6,122,902
Members
449,097
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top