Short version
At runtime, I'm getting a type mismatch error from Microsoft.Office.Interop.Excel
, 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 (XlYesNoGuess
) to use as my second parameter of Range.RemoveDuplicates(object, XlYesNoGuess)
. Please help me to stop Visual Studio from considering Microsoft.Office.Interop.Excel
as part of my assembly rather than just a project reference.
Long version
The specific error I'm getting in my logs is below:
System.Runtime.InteropServices.COMException (0x80020005): Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))
However, I know from using breakpoints and step through that there's a perceived connection to CS0433: The type TypeName1 exists in both TypeName2 and TypeName3
because it tells me that XlYesNoGuess is part of both Interop.Excel and my project.
When I do this, it points to my program's assembly as the second source of XlYesNoGuess
. I tried to confirm this further by writing my assembly's types via Assembly.GetExecutingAssembly.GetTypes()
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:
AutoLayout, AnsiClass, Class, Public, SealedMicrosoft.Office.Interop.Excel.XlYesNoGuess
After reading several related questions, I tried to the following steps to solve:
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:
researchItems.Range[researchItems.Cells[1, 1], researchItems.Cells[lastRow, 1]].RemoveDuplicates(researchItems.Columns[1], XlYesNoGuess.xlNo);
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:
ViewModel.cs
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] + " Research Items"));
var everythingIsReady = false;
while (!everythingIsReady)
{
var result = MessageBox.Show("Please push OK after updating the " + researchItems.Name + " sheet to your liking. Items listed here will be inserted with appropriate formatting between all instances of () in your query.", "Research Items sheet is ready?", 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,
"This program cannot proceed while you are changing a cell's value in Excel.",
string.Empty,
"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."
), "Excel Still in Edit Mode", MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.OK, MessageBoxOptions.DefaultDesktopOnly
);
break;
case -2147221080:
MessageBox.Show("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.", "Settings Sheet No Longer Open", 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 > 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 <= 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;
}
}
}
If it matters, my project is:
User contributions licensed under CC BY-SA 3.0