Search code examples
c#referenceexport-to-excel

ambiguous errorr due to reference microsoft.office.interop.excel


I added a reference 'microsoft.office.interop.excel' when i add this reference i got an error that says
'button is ambiguous reference between 'system.windows.forms.button' and 'microsoft.office.interop.excel'

my code contains the following function.

 using microsoft.office.interop.excel

 void SetButtons(Button i_InFrontButton)
 {
  ...
 }

Solution

  • The errors says what is all about:

    you have 2 namespaces available in yuour project

    • microsoft.office.interop.excel
    • system.windows.forms

    both of them contain inside Button class, which is completely different one from another. So compiler looking on written Button, has no idea what should be chosen.

    In this case, you have to specify fully qualified name of the type you intend to use, like:

     void SetButtons(System.Windows.Forms.Button i_InFrontButton)
     {
      ...
     }
    

    or if you would like to have short notion of namespace (alias), you can define it in the beginning of the file like:

     using WF = Sytem.Windows.Forms; 
     ... 
    
     //so your function definition will look like
     void SetButtons(WF.Button i_InFrontButton)
     {
      ...
     }