title: CS0433 Type exists in two assemblies
body: <p><strong>Short version</strong></p>
  <p>At runtime, I'm getting a type mismatch error from <code>Microsoft.Office.Interop.Excel</code>, because Visual Studio thinks this Interop .dll is also part of my assembly and it can't decide which of the two equally named types it's perceiving (<code>XlYesNoGuess</code>) to use as my second parameter of <a href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel.range.removeduplicates?view=excel-pia" rel="nofollow noreferrer"><code>Range.RemoveDuplicates(object, XlYesNoGuess)</code></a>. Please help me to stop Visual Studio from considering <code>Microsoft.Office.Interop.Excel</code> as part of my assembly rather than just a project reference.</p>
  <p><strong>Long version</strong></p>
  <p>The specific error I'm getting in my logs is below:</p>
  <p><code>System.Runtime.InteropServices.COMException (0x80020005): Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))</code></p>
  <p>However, I know from using breakpoints and step through that there's a perceived connection to <a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0433" rel="nofollow noreferrer"><code>CS0433: The type TypeName1 exists in both TypeName2 and TypeName3</code></a> because it tells me that XlYesNoGuess is part of both Interop.Excel and my project.</p>
  <p>When I do this, it points to my program's assembly as the second source of <code>XlYesNoGuess</code>. I tried to confirm this further by writing my assembly's types via <code>Assembly.GetExecutingAssembly.GetTypes()</code> at the point of the error to a log. I see this in the log, which may confirm the problem that the type is also present in my assembly:</p>
  <p><code>AutoLayout, AnsiClass, Class, Public, SealedMicrosoft.Office.Interop.Excel.XlYesNoGuess</code></p>
  <p>After reading several related questions, I tried to the following steps to solve:</p>
  <ul>
  <li>Restart my computer</li>
  <li>Delete my project's bin and obj folders</li>
  <li>Clean my solution</li>
  <li>Go to my project's references, uncheck the following two assemblies, confirm, go back to references, and recheck them:
  <ul>
  <li>Microsoft.Office.Interop.Excel</li>
  <li>Microsoft.Office.Interop.Word</li>
  </ul>
  </li>
  <li>Build my project</li>
  </ul>
  <p>However, this did not solve my problem. What can I do to stop Visual Studio from thinking that I have XlYesNoGuess types in my project and in the Interop.dll, and getting a type mismatch because it passes the one from my project to the following line of code:</p>
  <p><code>researchItems.Range[researchItems.Cells[1, 1], researchItems.Cells[lastRow, 1]].RemoveDuplicates(researchItems.Columns[1], XlYesNoGuess.xlNo);</code></p>
  <hr />
  <p>Here are (what I think are) the relevant parts of the class where the error is occurring. See the line above for where the error occurs:</p>
  <p><strong>ViewModel.cs</strong></p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Collections.ObjectModel;
  using System.ComponentModel;
  using System.Data;
  using System.IO;
  using System.Linq;
  using System.Runtime.CompilerServices;
  using System.Runtime.InteropServices;
  using System.Windows;
  using Mass_Queryer.ViewPlusViewModel.DataGrid_Row_Classes;
  using Microsoft.Office.Interop.Excel;
  
  namespace Mass_Queryer
  {
      public class ViewModel : INotifyPropertyChanged
      {
          public bool GetUserPromptedResearchItems(int queryNumber)
          {
              var (excelInstance, researchItems) = GetResearchItemsTemplateAndContainingInstance();
  
              PrepareResearchItemsTemplate(researchItems, GeneralTools.SanitizeSheetNameCandidate(this.QueryNames[queryNumber] + &quot; Research Items&quot;));
  
              var everythingIsReady = false;
              while (!everythingIsReady)
              {
                  var result = MessageBox.Show(&quot;Please push OK after updating the &quot; + researchItems.Name + &quot; sheet to your liking. Items listed here will be inserted with appropriate formatting between all instances of () in your query.&quot;, &quot;Research Items sheet is ready?&quot;, MessageBoxButton.OKCancel, MessageBoxImage.None, MessageBoxResult.OK, MessageBoxOptions.DefaultDesktopOnly);
                  if (result == MessageBoxResult.Cancel)
                  {
                      CloseSettingsSheetAndQuitExcelInstance(excelInstance, researchItems);
                      return false;
                  }
  
                  try
                  {
                      everythingIsReady = ResearchItemTemplateIsErrorFree(researchItems);
                  }
                  catch (COMException exception)
                  {
                      switch (exception.ErrorCode)
                      {
                          case -2147418111: //can we prevent this by selecting a blank cell after user selects ok or when this error is caught?
                              MessageBox.Show(
                                  string.Join(
                                      Environment.NewLine,
                                      &quot;This program cannot proceed while you are changing a cell's value in Excel.&quot;,
                                      string.Empty,
                                      &quot;Please go to the settings sheet you were just updating and confirm your changes to the cell, then press the OK button on this message.&quot;
                                  ), &quot;Excel Still in Edit Mode&quot;, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.OK, MessageBoxOptions.DefaultDesktopOnly
                              );
  
                              break;
                          case -2147221080:
                              MessageBox.Show(&quot;A settings sheet needed for your selection was closed before the program had a chance to read it. The program will now return to the Landing window.&quot;, &quot;Settings Sheet No Longer Open&quot;, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.OK, MessageBoxOptions.DefaultDesktopOnly);
  
                              return false;
                          default:
                              throw;
                      }
                  }
              }
  
  
              var lastRow = GeneralTools.GetLastRowOfSheetInColumn(researchItems, 1);
  
              if (lastRow == 1)
              {
                  if (string.IsNullOrEmpty(researchItems.Cells[1, 1].Value))
                  {
                      CloseSettingsSheetAndQuitExcelInstance(excelInstance, researchItems);
                      return false;
                  }
              }
  
              if (lastRow &gt; 1)
              {
                  researchItems.Unprotect();
                  researchItems.Range[researchItems.Cells[1, 1], researchItems.Cells[lastRow, 1]].RemoveDuplicates(researchItems.Columns[1], XlYesNoGuess.xlNo);
                  researchItems.Protect();
              }
  
              var researchItemsAsArray = GeneralTools.ConvertRangeTo2DStringArray(researchItems.Range[researchItems.Cells[1, 1], researchItems.Cells[lastRow, 1]]);
  
              this.ResearchItems[queryNumber].Clear();
              for (var i = researchItemsAsArray.GetLowerBound(0); i &lt;= researchItemsAsArray.GetUpperBound(0); i++)
              {
                  this.ResearchItems[queryNumber].Add(researchItemsAsArray[i, 0]);
              }
  
              this.ResearchItemColumnNumber[queryNumber] = Convert.ToInt32(researchItems.Cells[8, 3].Value);
  
              CloseSettingsSheetAndQuitExcelInstance(excelInstance, researchItems);
              return true;
          }
      }
  }
  </code></pre>
  <p>If it matters, my project is:</p>
  <ul>
  <li>Visual Studio Professional 2019, 16.6.2</li>
  <li>.NET Framework 4.7.2</li>
  <li>C# 8.0</li>
  <li>WPF</li>
  </ul>