I have a bunch of Excel.Name objects under Workbook.Names collection which contain invalid references. In other words, when I evaluate the Excel.Name.RefersTo property, I get a value back beginning with "=#REF...". I tried to run the following code but it seems to have no effect in removing the names:
var ranges = myWorkBook.Names;
for (int i = 1; i <= ranges.Count; i++)
{
var currentName = ranges.Item(i, Type.Missing, Type.Missing);
var refersTo = currentName.RefersTo.ToString();
if (refersTo.Contains("REF!"))
{
currentName.Delete();
}
}
Can anyone suggest what I'm doing wrong here? Or maybe I'm missing a step?