Search code examples
c#excelcominteropfile-not-found

A end product user can't create an excel interop instance


I'm creating a wpf application for internal deployment.

A user using the software is getting the following error when trying to create an excel interop instance.

Retrieving the COM class factory for component with CLSID (...) failed due to the following error: 80070002 The system cannot find the file specified

The section that is catching the error is the following

try
{
    _excelApplication = new Microsoft.Office.Interop.Excel.Application();
    GetWindowThreadProcessId(_excelApplication.Hwnd, out ExcelAppProcessId);
    _excelApplication.ScreenUpdating = false;
}
catch(Exception e)
{
    //TODO Move message box to parents
    MessageBox.Show($"Termination Error: Could not open Excel Application: {e.Message}");

    Environment.Exit(110);
}

Previously the same user had an issue while trying to open Access (can't remember what the exact error was) and I implemented the following to fix it.

try
{
    //MessageBox.Show($"OS: {EnvironmentFunctions.is64BitOperatingSystem} Process: {EnvironmentFunctions.is64BitProcess}");

    if (EnvironmentFunctions.is64BitOperatingSystem && !EnvironmentFunctions.is64BitProcess)
    {
        string PathValue = "";
        string sAdd = "";
        string strCommonFiles =
            Environment.GetEnvironmentVariable("CommonProgramFiles(x86)");

        sAdd = ";" + strCommonFiles + "\\microsoft shared\\DAO";
        PathValue = Environment.GetEnvironmentVariable("Path");
        PathValue += sAdd;

        Environment.SetEnvironmentVariable("path", PathValue);

    }

    _accessApplication = new Microsoft.Office.Interop.Access.Application();
    GetWindowThreadProcessId(_accessApplication.hWndAccessApp(), out AccessAppProcessId);
}
catch
{
    MessageBox.Show("Termination Error: Could not open Access Application");
    Environment.Exit(110);
}

Would there be a similar solution but for the Excel interop?

Notes about the user: They are one of the few left running windows 7.


Solution

  • Retrieving the COM class factory for component with CLSID (...) failed due to the following error: 80070002 The system cannot find the file specified

    Typically this error is caused by only a few issues, which I will list below.

    1. Recent Windows updates
    2. Partition issue's and or problems
    3. Bitness issue's (determined what Office is installed on the end machine and what the application is compiled against)

    I asked earlier about what you were targeting because you have some code that is checking if the pc is 64bit and you have already ran into some issues. This then lead me to bitness issues with creating instances of Excel.

    My recommendation and solution to your exact issue is because the end machine is 32bit, so compiling for x86/32bit should fix the issue.

    On a final note, you may be able to remove that old code as it wouldn't be needed anymore.