To get named range of a cell, heres my code. Works ok if the clicked cell is on first sheet. But the moment I select a cell from some other sheet, it just throws the Exception from HRESULT: 0x800A03EC Error
Saw similar question on stack but dint get the conclusion from those posts:
Here is my code:
Microsoft.Office.Interop.Excel.Workbook _workbook = ThisAddIn.Application.ActiveWorkbook;
Microsoft.Office.Interop.Excel.Range Target = (Microsoft.Office.Interop.Excel.Range)Globals.ThisAddIn.Application.ActiveCell;
foreach (Microsoft.Office.Interop.Excel.Name name in _workbook.Names)
{
Microsoft.Office.Interop.Excel.Range intersectRange = _workbook.Application.Intersect(Target, name.RefersToRange);
if (intersectRange != null)
{
rangeName = name.Name;
break;
}
}
You can’t use Intersect without throwing an error if the ranges are on different sheets. Add a check to see whether each range’s
Parentproperty refer to the same sheet, before you try theIntersect()