.net 我在命名空间“microsoft.office.interop.excel”中收到错误“应用程序”不明确

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3615008/
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 14:40:29  来源:igfitidea点击:

I'm getting the error "Application" is ambiguous in the namespace 'microsoft.office.interop.excel'

.netvb.netvisual-studiovisual-studio-2008interop

提问by calico-cat

I have a project which opens a simple Excel file and populates it. It was working fine until this morning, when it has suddenly started giving me the error above: 'Application' is ambiguous in the namespace 'Microsoft.Office.Interop.Excel'.

我有一个项目,它打开一个简单的 Excel 文件并填充它。直到今天早上它都工作正常,突然开始给我上面的错误:'Application' is ambiguous in the namespace 'Microsoft.Office.Interop.Excel'

I haven't changed any project references, or anything within the file itself. The references include Microsoft.Office.Interop.Excel. The imports statement is there: imports Microsoft.Office.Interop

我没有更改任何项目引用,或文件本身中的任何内容。参考资料包括 Microsoft.Office.Interop.Excel。进口声明在那里:imports Microsoft.Office.Interop

The object declaration is also complete: Dim xl As Microsoft.Office.Interop.Excel.Applicationwhich is the line that's giving me the error!

对象声明也是完整的:Dim xl As Microsoft.Office.Interop.Excel.Application这是给我错误的行!

I've tried googling this error, and the only response is that I need to declare xlas Microsoft.Office.Interop.Excel.Application.

我试过用谷歌搜索这个错误,唯一的反应是我需要声明xlMicrosoft.Office.Interop.Excel.Application.

The fact that I hadn't changed anything within the project nor the code tells me this is a corruption in Visual Studio 2008. However, cleaning and rebuilding the project, rebooting Windows, and restarting VS has no effect.

我没有更改项目中的任何内容,也没有更改代码这一事实告诉我这是 Visual Studio 2008 中的损坏。但是,清理和重建项目、重新启动 Windows 和重新启动 VS 都没有效果。

Any ideas?

有任何想法吗?

采纳答案by calico-cat

Removing and re-adding the reference solved this issue.

删除并重新添加引用解决了这个问题。

回答by Hans Olsson

I don't think you should have the line as Imports Microsoft.Office.Interop. Either use

我不认为你应该把这条线作为Imports Microsoft.Office.Interop. 要么使用

Imports Excel = Microsoft.Office.Interop.Excel

And then use it as:

然后将其用作:

Dim xl As Excel.Application

Or remove the Importsall together and use the full name everywhere, as:

或者Imports一起删除所有内容并在任何地方使用全名,如:

Dim xl As Microsoft.Office.Interop.Excel.Application

回答by J.Harrison

Experienced the same problem and was able to identify the issue and resolve (note: my issue was with Outlook but same error occurred when using Excel):

遇到了同样的问题,并能够确定问题并解决(注意:我的问题是 Outlook,但在使用 Excel 时发生了同样的错误):

  1. Go to Project >> Add References >> Assemblies >> (search Outlook) and UNCHECK ALL Microsoft.Office.Interop.Outlook that are checked (I had version 14 checked).
  2. Go to Project >> Add References >> COM >> (search Outlook) >> CHECK Microsoft Outlook xx Object Library
  1. 转到项目>>添加引用>>程序集>>(搜索Outlook)并取消选中所有已选中的Microsoft.Office.Interop.Outlook(我已选中版本14)。
  2. 转到项目>>添加引用>>COM>>(搜索Outlook)>>检查Microsoft Outlook xx对象库

After doing this the code "Dim olObject as Outlook.Application" no longer had the 'Application' is ambiguous in the namespace 'Microsoft.Office.Interop.Outlook.

执行此操作后,代码“Dim olObject as Outlook.Application”不再有“Application”在命名空间“Microsoft.Office.Interop.Outlook”中不明确。

The same method worked to resolve Excel

同样的方法可以解决 Excel

回答by Nilo M. Padre

Create a new project and copy all those lines of commands from your old project and then go ADD REFERENCE and select anew the MS interop.

创建一个新项目并从旧项目中复制所有这些命令行,然后转到 ADD REFERENCE 并重新选择 MS 互操作。

回答by Tom K

I noted that the problems occur when you have two similar references, each in different locations. Thus the program is unable to narrow down to the one you are implying within the code.

我注意到当您有两个相似的参考文献时会出现问题,每个参考文献位于不同的位置。因此该程序无法缩小到您在代码中暗示的那个。

Simply go to the properties page and remove one of them, and you should be fine.

只需转到属性页面并删除其中之一,您应该没问题。

回答by sk8forether

I was having a similar issue picking up a project someone else had began (and was working for them), and it turns out that Visual Studio 2010 will consider the Microsoft Excel 15.0 Object Library to be a different reference than the Microsoft.Office.Interop.Excel.dll specifically. In my project, I had to remove the Microsoft.Office.Interop.Excel.dll and keep the Excel 15.0 Object Library for it to work.

我在选择其他人开始(并为他们工作)的项目时遇到了类似的问题,结果是 Visual Studio 2010 将 Microsoft Excel 15.0 对象库视为与 Microsoft.Office.Interop 不同的引用特别是.Excel.dll。在我的项目中,我必须删除 Microsoft.Office.Interop.Excel.dll 并保留 Excel 15.0 对象库才能使其工作。