开发者

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

开发者 https://www.devze.com 2023-01-13 18:52 出处:网络
I have a project which opens a simple Excel file and popul开发者_如何学编程ates it. It was working fine until this morning, when it has suddenly started giving me the error above: \'Application\' is a

I have a project which opens a simple Excel file and popul开发者_如何学编程ates 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'.

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

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

I've tried googling this error, and the only response is that I need to declare xl as Microsoft.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.

Any ideas?


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

Imports Excel = Microsoft.Office.Interop.Excel

And then use it as:

Dim xl As Excel.Application

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

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


Removing and re-adding the reference solved this issue.


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):

  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

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

The same method worked to resolve Excel


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.


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.


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.


Run the NuGet package manager update for Microsoft.Office.Interop.Excel. After that, all my problems went away!

0

精彩评论

暂无评论...
验证码 换一张
取 消