The exception is thrown at if condition itself, why does the Range object does not return null if it is not named ?
That's just the way the Excel object model works, you'll get the same result if you try to access Range.Name from VBA code.
Your options would be:
Catch the exception, e.g.
public string GetRangeName(range As Range) { try { var name = range.Name; if (name == null) return null; // Won't happen in the current version of Excel, but who knows for a future version return name.Name; } catch(COMException) { return null; } }
or, iterate through the
Names
collection of the Worksheet and its Workbook, looking for one that references the cell you're interested in.