I'm working on a .NET program to interface with Excel. The code looks like this:
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:
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?
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?