Click to See Complete Forum and Search --> : Excel - Referencing a named range


FMarino
October 5th, 2009, 10:49 AM
Hi folks, I'm tearing my hair out over this one and any help would be greatly appreciated.

I'm trying to reference a named range in my worksheet, but of the two methods that I've found, neither work.

If I use this:

dataRange = Program.application.Names.Application.Names("name of range").RefersToRange;

I receive the following error:
Non-invocable member 'Microsoft.Office.Interop.Excel._Application.Names' cannot be used like a method.


If I try to index it, as some have suggested, like this:

dataRange = Program.application.Names.Application.Names["name of range"].RefersToRange;

I recieve this error:
Cannot apply indexing with [] to an expression of type 'Microsoft.Office.Interop.Excel.Names'


Can anyone tell me what I'm doing wrong?

FMarino
October 5th, 2009, 11:21 AM
I'm sorry, the line that I posed up there isn't the correct line. I was trying random garbage in my frustration.

Here are the two alternatives that I've tried:

dataRange = c_application.ActiveWorkbook.Names["name of range"].RefersToRange;

dataRange = c_application.ActiveWorkbook.Names("name of range").RefersToRange;

rliq
October 5th, 2009, 06:10 PM
Take a look at the Gembox.Spreadsheet Samples programme. You can download it from their site for free. It gives you some examples of how to do many things in Excel using C#. http://www.gemboxsoftware.com/

Its on this page http://www.gemboxsoftware.com/GBSpreadsheet.htm in the paragraph headed:

Features
(>) Launch the GemBox.Spreadsheet Sample Explorer

Download it and install it, very easy to use and view the code.