I would like to create a Named Range in Excel from a NetOffice, but I cannot figure out how to invoke the method. Whatever the input, the underlying Excel Object throws a COMException with a generic HRESULT 0x800A03EC Error.
I have no hint about what is wrong (syntax? Excel version? NetOffice version?).Is there a known bug on this method?
Thanks for your help!
private void CreateNameRange(Workbook wb, Range range)
{
try
{
Name existingName = (Name)wb.Names.FirstOrDefault(); //works fine if a name already exists in Excel
wb.Names.Add(); //throws
wb.Names.Add("name1"); //throws
wb.Names.Add("name1", range); //throws
wb.Names.Add("name1", range.Address); //throws
wb.Names.Add("name1", "F7"); //throws
wb.Names.Add("myName", "=Sheet1!$F$7:$I$13"); //throws
wb.Names.Add("myName", "'=Sheet1!$F$7:$I$13'"); //throws
if (_wb.ActiveSheet is Worksheet ws) {
ws.Names.Add("myname", "=Sheet1!$F$7:$I$13"); //throws as well
}
}
catch (COMException e)
{
//always ends up here with Inner Exception : COMException (HRESULT 0x800A03EC)
}
}
Actually, the root cause of this issue was not the method itself, but when the method was called.
I was trying to create Named Ranges from within a UDF Function but Excel considers this operation as unauthorized, along with other worksheet/data manipulation when calculation is being made, and that's why the COM Exception was raised.
Bottom line : Named Ranges cannot be created when a workbook calculation is in progress.