Excel .NET COM - 自动化错误。该系统找不到指定的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/881750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:43:23  来源:igfitidea点击:

Excel .NET COM - Automation error. The system cannot find the file specified

.netexcelcomautomation

提问by ingt

I have a .NET 2.0 COM object that's used by VBA in Excel. It works fine on my dev machine, but when trying to use it on a clean VM workstation I get this error:

我有一个由 Excel 中的 VBA 使用的 .NET 2.0 COM 对象。它在我的开发机器上运行良好,但是当尝试在干净的 VM 工作站上使用它时,我收到此错误:

Automation error. The system cannot find the file specified.

自动化错误。该系统找不到指定的文件。

The dll is registered with "regasm /tlb /codebase mycom.dll" and not put in the GAC. I don't have administration rights on the VM box

该 dll 是用“regasm /tlb /codebase mycom.dll”注册的,而不是放在 GAC 中。我没有对 VM 框的管理权限

Any ideas?

有任何想法吗?

回答by sharptooth

You need to either invoke regasm with the full path to the assembly as the codebaseparameter value or put the assembly into some location which is always on the path for searching libraries. Otherwise it will not be found when the client tries to instantiate the COM object.

您需要使用程序集的完整路径作为codebase参数值调用 regasm,或者将程序集放在某个位置,该位置始终位于搜索库的路径上。否则当客户端尝试实例化 COM 对象时将找不到它。

回答by Kent Kruckeberg

On windows 7, 64 bit and a .NET 4.0 framework dll (32 bit) that I want to be usable as a COM object for a Microsoft Excel 2010 VBA application, here is what worked for me.

在 Windows 7、64 位和 .NET 4.0 框架 dll(32 位)上,我希望将其用作 Microsoft Excel 2010 VBA 应用程序的 COM 对象,这对我有用。

  1. Copy the dll to c:\windows\syswow64
  2. In a cmd shell, run

    C:\Windows\Microsoft.NET\Framework\v4.0.<whatever you have>\regasm.exe c:\windows\syswow64\<name of dll> /codebase /tlb:c:\windows\syswow64\<name of dll minus '.dll'>.tlb
    
  1. 将dll复制到c:\windows\syswow64
  2. 在 cmd shell 中,运行

    C:\Windows\Microsoft.NET\Framework\v4.0.<whatever you have>\regasm.exe c:\windows\syswow64\<name of dll> /codebase /tlb:c:\windows\syswow64\<name of dll minus '.dll'>.tlb
    

You can skip the last part (/tlb:. . .) if you don't want or need intellisense on the machine you are registering the assembly on.

如果您不想或不需要在您注册程序集的机器上使用智能感知,您可以跳过最后一部分 (/tlb:...)。

The key hangup I had is that on XP I never had to use the /codebase parameter before but that was the key thing needed before this worked.

我遇到的关键问题是,在 XP 上我以前从未使用过 /codebase 参数,但这是在此之前需要的关键。

回答by clamum

I received this "Automation error. The system cannot find the file specified" error after I had created a .NET .dll (v4.0) with the intention of using it in a VB6 application (decorated my class with "ClassInterface" and "ComVisible" attributes, methods with "ComVisible").

在我创建了一个 .NET .dll (v4.0) 并打算在 VB6 应用程序中使用它之后,我收到了这个“自动化错误。系统找不到指定的文件”错误(用“ClassInterface”和“ ComVisible”属性,带有“ComVisible”的方法)。

I ran "regasm.exe -tlb C:\PathTo\MyDll.dll" but received the above error after adding the .tlb file as a reference in my VB6 application and running/debugging it. Only after adding the "-codebase" parameter to the regasm.exe call and re-adding the .tlb reference did the error get resolved.

我运行了“regasm.exe -tlb C:\PathTo\MyDll.dll”,但在我的 VB6 应用程序中添加 .tlb 文件作为参考并运行/调试它后收到上述错误。只有在将“-codebase”参数添加到 regasm.exe 调用并重新添加 .tlb 引用后,错误才得到解决。

Just thought I'd share my experience.

只是想我会分享我的经验。

回答by John M

I also receive a automation error. My reference (in MS Access) was to a TLB file. The corresponding DLL file was missing from the folder that held the TLB file and this caused the 'automation error' message to appear. Adding the DLL back in fixed.

我也收到一个自动化错误。我的参考(在 MS Access 中)是一个 TLB 文件。保存 TLB 文件的文件夹中缺少相应的 DLL 文件,这导致出现“自动化错误”消息。将 DLL 添加回固定状态。

回答by paulRoman

I was getting the same error (could not consume the .NET object from legacy VB6 cod on a second dev machine, after it was working on a first machine where I originally wrote it). The .NET DLL compiled and registered just fine - I tried all sorts of combinations - with and without using the "Register for COM Interop" build setting in VS; manually registering via regasm.exe and trying this both with and without the /codebase parameter; tried both enabling and suppressing the COM Visible assembly-level attribute (when suppressing, I set the attribute on the class I need to consume from COM). But nothing worked, I kept getting the same error.

我遇到了同样的错误(在我最初编写它的第一台机器上工作之后,无法在第二台开发机器上使用来自旧版 VB6 cod 的 .NET 对象)。.NET DLL 编译和注册得很好 - 我尝试了各种组合 - 在 VS 中使用和不使用“注册 COM 互操作”构建设置;通过 regasm.exe 手动注册并尝试使用和不使用 /codebase 参数;尝试启用和取消 COM Visible 程序集级属性(取消时,我在需要从 COM 使用的类上设置该属性)。但没有任何效果,我不断收到同样的错误。

Turns out I had upgraded the DLL output to .NET 4.5 on the second machine, whereas it was originally building a .NET 2.0 assembly. My project had a few references targeting 3rd party Interop DLLs that were running .NET 2.0. When I either updated these references and rebuilt the DLL -or- set my project back to run on .NET 2.0 - my problem was solved. When using /codebase (which VS does automatically) I found that I did not need to put my DLL in the application directory or in \syswow64. Also the MSDN docs state you must use a SN (strong name) for your assembly when using /codebase, but I found you don't have to; you just get a warning from the regasm.exe command line tool.

结果我已经在第二台机器上将 DLL 输出升级到 .NET 4.5,而它最初是构建一个 .NET 2.0 程序集。我的项目有一些针对运行 .NET 2.0 的 3rd 方互操作 DLL 的引用。当我更新这些引用并重建 DLL 或将我的项目重新设置为在 .NET 2.0 上运行时,我的问题就解决了。使用 /codebase(VS 自动执行)时,我发现不需要将 DLL 放在应用程序目录或 \syswow64 中。MSDN 文档还指出,在使用 /codebase 时,您必须为程序集使用 SN(强名称),但我发现您不必这样做;您只会从 regasm.exe 命令行工具收到警告。

The point is, from a COM Interop standpoint, be careful about the .NET runtime version of your dependencies with respect to the .NET Framework you are targeting.

关键是,从 COM Interop 的角度来看,要注意相对于目标 .NET Framework 的依赖项的 .NET 运行时版本。