I am using NamedRanges to store “Views” of my Excel Sheet.
So when a user picks a View it will grab the NamedRange for that View (a collection of rows) it will then Hide those rows. However, it appears that when the actual Range in the NamedRange gets too big I am getting a COM Exception whenever I try to refer to the RefersToRange Property
‘(ViewRange).RefersToRange’ threw an exception of type
‘System.Runtime.InteropServices.COMException’
The really interesting part is that I can select the NamedRange in Excel and it will highlight the entire range just fine, also other properties like .RefersTo and .RefersToR1C1 return just fine.
I create the NamedRange by first creating a Range with all the Rows, and then just naming it.
here is an example of what is returned by RefersToR1C1 when I am getting the COM error
“=Sheet1!R13:R23,Sheet1!R26:R39,Sheet1!R41,Sheet1!R43:R46,Sheet1!R48:R49,Sheet1!R51:R72,Sheet1!R76:R78,Sheet1!R83:R84,Sheet1!R137:R147,Sheet1!R150:R163,Sheet1!R165,Sheet1!R167:R170,Sheet1!R172:R173,Sheet1!R175:R196,Sheet1!R200:R202,Sheet1!R207:R208,Sheet1!R261:R271,Sheet1!R274:R287,Sheet1!R289,Sheet1!R291:R294,Sheet1!R296:R297,Sheet1!R299:R320,Sheet1!R324:R326,Sheet1!R331:R332,Sheet1!R385:R395,Sheet1!R398:R411,Sheet1!R413,Sheet1!R415:R418,Sheet1!R420:R421,Sheet1!R423:R444,Sheet1!R448:R450,Sheet1!R455:R456”
Here is the code where I am having the problem
Excel.Range rngAll = _Blocks.DataRange;
rngAll.EntireRow.Hidden = false;
Excel.Name ViewRange = Globals.ThisWorkbook.Names.Item(viewName, System.Type.Missing);
string addy = ViewRange.RefersToR1C1 as string; //this line works fine
ViewRange.RefersToRange.EntireRow.Hidden = true; //this line throws the COM Exception
Any help would be appreciated, or if you have a better way of accomplishing my “Views”, or is there a way even to use the Formula Address above to instantiate a range that I could then Hide?
UPDATE!!!
Ok So this doesn’t answer the original question, which is why I edited instead of answered. If there are reputable “Stack Overflowers” here please let me know if I should answer instead.
I did figure out a way to access the Range in question, which does solve my immediate problem. I ended up using the following line of code
Globals.Sheet1.InnerObject.get_Range(viewName, System.Type.Missing).EntireRow.Hidden = true;
Thanks,
Pete
Still don’t know why it fails but the working solution is to access the Name Range via the Worksheet’s get_Range() method.