最终产品用户无法创建 excel 互操作实例

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

我正在为内部部署创建一个 wpf 应用程序。

使用该软件的用户在尝试创建 excel 互操作实例时遇到以下错误。

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

捕获错误的部分如下

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);
}

之前同一位用户在尝试打开 Access 时遇到问题(不记得确切的错误是什么),我执行了以下操作来修复它。

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);
}

如果没有 Excel 互操作,是否会有类似的解决方案?

关于用户的注释:他们是为数不多的人之一 运行 windows 7.

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

通常这个错误是由少数几个问题引起的,我将在下面列出。

  1. 最近 Windows 更新
  2. 分区问题和/或问题
  3. 位数问题(确定在终端计算机上安装了什么 Office 以及针对什么编译应用程序)

我之前问过您的目标是什么,因为您有一些代码正在检查电脑是否为 64 位,并且您已经 运行 解决了一些问题。然后,这导致我在创建 Excel.

实例时遇到位数问题

我的建议和解决方案是因为终端机器是32位的,所以编译x86/32bit应该解决问题。

最后一点,您可以删除旧代码,因为不再需要它了。