Search code examples
c#exceloffice-interopexcel-interopexcel-automation

Getting a COM class factory Path not found exception when the path clearly exists


I'm trying to use Microsoft.Office.Interop.Excel library to convert a csv to an xlsx. This is the function I am using

public static string ConvertToXlsx(string _sFilePath)
{
string _newFilePath = _sFilePath.Replace("csv", "xlsx");
                Application app = new Application();
                Workbook wb;
                wb = app.Workbooks.Open(_sFilePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                if (File.Exists(_newFilePath))
                    return "";
                wb.SaveAs(_newFilePath, XlFileFormat.xlOpenXMLWorkbook, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                wb.Close();
                app.Quit();
                return _newFilePath;
}

the way I get the path in the function parameter is by using the OpenFileDialog class

OpenFileDialogue openDialog = new OpenFileDialog();
DialogResult response = openDialog.ShowDialog();
if (response == DialogResult.Cancel)
      return;
string convertedFileName = convertToXlsx(openDialog.FileName);

The error throws at app.Workbooks.Open() seemingly when it tries to access the file path that was provided by OpenFileDialog

This is the error I'm receiving with full call stack included

System.IO.DirectoryNotFoundException: Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070003 The system cannot find the path specified. (Exception from HRESULT: 0x80070003).

   at System.Runtime.Remoting.RemotingServices.AllocateUninitializedObject(RuntimeType objectType)

   at System.Runtime.Remoting.Activation.ActivationServices.CreateInstance(RuntimeType serverType)

   at System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(RuntimeType serverType, Object[] props, Boolean bNewObj)

   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)

   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)

   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)

   at System.Activator.CreateInstance(Type type, Boolean nonPublic)

   at System.Activator.CreateInstance(Type type)

   at ConvertToXlsx(String _sFilePath)

The file path in question comes straight from the file explorer so it must exist. The path is C:\temp\example.csv.

Interestingly, this doesn't even happen for all users. For many it works exactly as expected so what could be going on here?


Solution

  • Try to install Excel (MS Office) on the system or repair it. It seems the problem is not with a file to open, but with Excel itself. Remember, you must have Excel installed on the system where you try to automate it.

    Make sure that such file exists on the disk and accessible for all users. I have noticed the following error message:

    80070003 The system cannot find the path specified. (Exception from HRESULT: 0x80070003).

    It is not clear what file (path) is chosen in the dialog window. Note, you need to choose a local file. Excel may not access network shares, in that case you need to copy the file locally and only then use the Workbooks.Open method passing a local file path.

    You may also find the following similar threads helpful: