שתף באמצעות


...is ambiguous in the namespace 'Microsoft.Office.Interop.Excel'

Question

Wednesday, July 2, 2014 2:28 PM

Hi,

I am trying to convert a project with Framework 2.0 to Frameowrk 4.0, and I come into those errors of '...is ambiguous in the namespace 'Microsoft.Office.Interop.Excel''. This is referring to Application, Workbook, Worksheet. I tried to delete the reference and re-add, but still the problems are not fixed.

And the code works fine under Framework 2.0.

If I copy the problematic code to a new project and add the same reference, it works perfectly fine. So I guess there must be other part of the project causing this problem but I have no hint.

Does anyone know how to do about it?

Thanks!

Problems fixed. I need to delete the older files in bin and obj folder. 

All replies (11)

Wednesday, July 2, 2014 3:18 PM

Hello,

If you remove the reference for Microsoft.Office.Interop.Excel then add the reference for Microsoft.Office.Interop.Excel back in where is the error at i.e. in a specific piece of code etc. ??? If needed post your code.

Example code for perspective. If this was your code do you get an error in the IDE editor on an import statement or when declaring the Application object etc. 

Option Strict On
Imports Excel = Microsoft.Office.Interop.Excel
Imports Microsoft.Office
Imports System.Runtime.InteropServices
''' <summary>
''' Hard coded example of creating a named range. Of course
''' the cell address for the range can easily be dynamic.
''' </summary>
''' <remarks></remarks>
Module CreateNamedRange

    Private FileName As String =
        IO.Path.Combine(Application.StartupPath, "NameChangeDemo.xlsx")

    Public Sub CreateNamed()

        If Not IO.File.Exists(FileName) Then
            MessageBox.Show("'" & FileName & "' not found, aborting")
            Exit Sub
        End If

        Dim SheetName As String = "Sheet1"
        Dim Proceed As Boolean = False
        Dim xlApp As Excel.Application = Nothing
        Dim xlWorkBooks As Excel.Workbooks = Nothing
        Dim xlWorkBook As Excel.Workbook = Nothing
        Dim xlWorkSheet As Excel.Worksheet = Nothing
        Dim xlWorkSheets As Excel.Sheets = Nothing
        Dim xlRange1 As Excel.Range = Nothing

        xlApp = New Excel.Application
        xlApp.DisplayAlerts = False
        xlWorkBooks = xlApp.Workbooks
        xlWorkBook = xlWorkBooks.Open(FileName)

        xlApp.Visible = False

        xlWorkSheets = xlWorkBook.Sheets
        For x As Integer = 1 To xlWorkSheets.Count
            xlWorkSheet = CType(xlWorkSheets(x), Excel.Worksheet)

            If xlWorkSheet.Name = SheetName Then
                Proceed = True
                Exit For
            End If

            Runtime.InteropServices.Marshal.FinalReleaseComObject(xlWorkSheet)
            xlWorkSheet = Nothing
        Next

        If Proceed Then
            xlRange1 = xlWorkSheet.Range("A1:B6")
            xlRange1.Name = "Kevin1"
        End If

        xlWorkSheet.SaveAs(FileName)

        xlWorkBook.Close()
        xlApp.UserControl = True
        xlApp.Quit()

        ReleaseComObject(xlRange1)
        ReleaseComObject(xlWorkSheets)
        ReleaseComObject(xlWorkSheet)
        ReleaseComObject(xlWorkBook)
        ReleaseComObject(xlWorkBooks)
        ReleaseComObject(xlApp)
    End Sub
    Private Sub ReleaseComObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        End Try
    End Sub
End Module

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.


Wednesday, July 2, 2014 3:39 PM

@Kevin,

Maybe you know it and it could not come into your mind. But the name for this is full qualifying. 

:-)

Success
Cor


Wednesday, July 2, 2014 4:34 PM

Imports System.Data.OleDbImports Microsoft.Office.InteropModule    Dim ExAppln As New Excel.Application//error    Dim ExWorkBook As Excel.Workbook// error    Dim ExSheet As Excel.Worksheet//error    Dim ExSheet1 As Excel.Worksheet//error    Dim ExSheet2 As Excel.Worksheet//error......Module End

Hi Kevin,

Above is the code. There are error indication with lines under Excel.Application, Excel.Workbook and Excel.Worksheet.

Thanks!


Wednesday, July 2, 2014 4:40 PM

I don't know which it is but use (and the same with the other namespace. This is full qualifying. 

dim whatever as Microsoft.Office.Interop.Excel.TheOneYouWant

Success
Cor


Wednesday, July 2, 2014 4:58 PM

Hello,

To go from this

We need to use

Imports System.Data.OleDb
Imports Microsoft.Office.Interop

Module Module1
    Dim ExAppln As New Excel.Application
    Dim ExWorkBook As Excel.Workbook
    Dim ExSheet As Excel.Worksheet
    Dim ExSheet1 As Excel.Worksheet
    Dim ExSheet2 As Excel.Worksheet

End Module

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.


Wednesday, July 2, 2014 5:21 PM

No, the problem is still there.


Wednesday, July 2, 2014 5:24 PM

No, the problem is still there.

Can you post the code else all I can do is guess at what you should try and that is a waste of both of our time.

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.


Wednesday, July 2, 2014 7:34 PM

I really want to. But it is a company project and I didn't write those. The one who wrote has left, so I have to handle all the shitting stuff.......


Wednesday, July 2, 2014 9:01 PM

Hello,

I can understand that your company does not want for source code to be put on the web. With that said there is nothing I can do for you. Good luck with this issue.

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.


Thursday, July 3, 2014 1:29 PM

Thank you.


Thursday, July 3, 2014 1:59 PM

I'm curious, did you ever try what I suggested, I see nothing about it?

Success
Cor