473,378 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How do I find all named ranges in VB.NET?

I am using VB.NET to read Excel workbooks which have various named
ranges, some of which may not exist in any given workbook. I am
trying to get a list of all the range names -- otherwise I need to
use a Try block to avoid throwing an exception when I try to read
data from ranges which aren't there, and this slows things down
considerably.

I am able to find the *number* of named ranges in a particular
workbook using this expression:

xlApp.ActiveWorkbook.Names.Count

(where xlApp is my open Excel application handle, typed as
Microsoft.Office.Interop.Excel.Application). And knowing the names
I can retrieve the ranges themselves using:

xlApp.ActiveWorkbook.Names.Item("MyRangeName").Ref ersToRange

But try as I might I can't seem to figure out how to get a list of
the actual names! I tried giving the Item property an integer
index, but that throws an exception. Can anyone give me a clue?

(I'd also like to get a list of styles in the workbook, and I
suspect this is the same problem, since I can get the number of
styles defined using xlApp.ActiveWorkbook.Styles.Count).
--
John Brock
jb****@panix.com

Nov 21 '05 #1
5 8753
John Brock wrote...
....
But try as I might I can't seem to figure out how to get a list of
the actual names! I tried giving the Item property an integer
index, but that throws an exception. Can anyone give me a clue?


VB.NET provides an object type for Excel defined names, doesn't it? If
that type were Microsoft.Office.Interop.Excel.Name, then try
Dim n As Microsoft.Office.Interop.Excel.Name

For Each n In xlApp.ActiveWorkbook.Names
MsgBox Prompt:=n.RefersToRange.Address, Title:=n.Name
Next n
If you're coding in Visual Studio, doesn't it provide an object
browser? If so, you should be able to find all the Excel object types
in it.

Nov 21 '05 #2
In article <11*********************@g44g2000cwa.googlegroups. com>,
Harlan Grove <hr*****@aol.com> wrote:
John Brock wrote...
...
But try as I might I can't seem to figure out how to get a list of
the actual names! I tried giving the Item property an integer
index, but that throws an exception. Can anyone give me a clue?
VB.NET provides an object type for Excel defined names, doesn't it? If
that type were Microsoft.Office.Interop.Excel.Name, then try
Dim n As Microsoft.Office.Interop.Excel.Name

For Each n In xlApp.ActiveWorkbook.Names
MsgBox Prompt:=n.RefersToRange.Address, Title:=n.Name
Next n
If you're coding in Visual Studio, doesn't it provide an object
browser? If so, you should be able to find all the Excel object types
in it.


Visual Studio shows me plenty of object types, I just can't get
anything to work.

I tried your suggestion, but it still doesn't work. I didn't want
to throw up messages boxes, so I did it this way:

Dim n As Microsoft.Office.Interop.Excel.Name

For Each n In xlApp.ActiveWorkbook.Names
Console.Debug("Name is: " & n.Name)
Next

I get a COMException exception, with the additional information
"Member not found".

I wonder if this could be related to the fact that I couldn't even
define an enumerator for the Names property:

Dim e as IEnumerator = xlApp.ActiveWorkbook.Names.GetEnumerator

This statement also throws an the same exception, and yet GetEnumerator
is one of the methods that Visual Studio suggests for Names.
--
John Brock
jb****@panix.com

Nov 21 '05 #3
Does Console have a Debug member? VS Intellisense indicated otherwise.

The following works fine. Two things worth noting. Usually, I have
Option Strict On. That disallows late binding and requires all the
type casts in the code. Also, VS Intellisense indicated that the Names
collection has no default property.

Dim i As Integer
With ThisApplication.ActiveWorkbook
For i = 1 To .Names.Count
CType(CType(.ActiveSheet, Excel.Worksheet). _
Cells(i, 1), Excel.Range).Value = _
.Names.Item(i).Name
Next i
End With

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article <df**********@reader1.panix.com>, jb****@panix.com says...
In article <11*********************@g44g2000cwa.googlegroups. com>,
Harlan Grove <hr*****@aol.com> wrote:
John Brock wrote...
...
But try as I might I can't seem to figure out how to get a list of
the actual names! I tried giving the Item property an integer
index, but that throws an exception. Can anyone give me a clue?

VB.NET provides an object type for Excel defined names, doesn't it? If
that type were Microsoft.Office.Interop.Excel.Name, then try
Dim n As Microsoft.Office.Interop.Excel.Name

For Each n In xlApp.ActiveWorkbook.Names
MsgBox Prompt:=n.RefersToRange.Address, Title:=n.Name
Next n
If you're coding in Visual Studio, doesn't it provide an object
browser? If so, you should be able to find all the Excel object types
in it.


Visual Studio shows me plenty of object types, I just can't get
anything to work.

I tried your suggestion, but it still doesn't work. I didn't want
to throw up messages boxes, so I did it this way:

Dim n As Microsoft.Office.Interop.Excel.Name

For Each n In xlApp.ActiveWorkbook.Names
Console.Debug("Name is: " & n.Name)
Next

I get a COMException exception, with the additional information
"Member not found".

I wonder if this could be related to the fact that I couldn't even
define an enumerator for the Names property:

Dim e as IEnumerator = xlApp.ActiveWorkbook.Names.GetEnumerator

This statement also throws an the same exception, and yet GetEnumerator
is one of the methods that Visual Studio suggests for Names.

Nov 21 '05 #4
In article <MP************************@msnews.microsoft.com >,
Tushar Mehta <tm****************@tushar-mehta.SeeOhEm> wrote:
Does Console have a Debug member? VS Intellisense indicated otherwise.
Um..., I meant Debug.Writeline. :-/
The following works fine. Two things worth noting. Usually, I have
Option Strict On. That disallows late binding and requires all the
type casts in the code. Also, VS Intellisense indicated that the Names
collection has no default property.

Dim i As Integer
With ThisApplication.ActiveWorkbook
For i = 1 To .Names.Count
CType(CType(.ActiveSheet, Excel.Worksheet). _
Cells(i, 1), Excel.Range).Value = _
.Names.Item(i).Name
Next i
End With
Damn! I was doing it correctly from the beginning, except I was
using 0-based indexing, and misunderstood the error message. Ouch!
I'm new to VB.NET, and there is a lot I like about it, but while
I am sure there are valid historical reasons for mixed indexing it
is still a huge misfeature. Personally I actually prefer 1-based
indexing -- which I think makes me a minority -- but please, one
or the other!!!
In article <df**********@reader1.panix.com>, jb****@panix.com says...
In article <11*********************@g44g2000cwa.googlegroups. com>,
Harlan Grove <hr*****@aol.com> wrote:
>John Brock wrote...
>...
>>But try as I might I can't seem to figure out how to get a list of
>>the actual names! I tried giving the Item property an integer
>>index, but that throws an exception. Can anyone give me a clue?

>VB.NET provides an object type for Excel defined names, doesn't it? If
>that type were Microsoft.Office.Interop.Excel.Name, then try
>
>
>Dim n As Microsoft.Office.Interop.Excel.Name
>
>For Each n In xlApp.ActiveWorkbook.Names
> MsgBox Prompt:=n.RefersToRange.Address, Title:=n.Name
>Next n
>
>
>If you're coding in Visual Studio, doesn't it provide an object
>browser? If so, you should be able to find all the Excel object types
>in it.


Visual Studio shows me plenty of object types, I just can't get
anything to work.

I tried your suggestion, but it still doesn't work. I didn't want
to throw up messages boxes, so I did it this way:

Dim n As Microsoft.Office.Interop.Excel.Name

For Each n In xlApp.ActiveWorkbook.Names
Console.Debug("Name is: " & n.Name)
Next

I get a COMException exception, with the additional information
"Member not found".

I wonder if this could be related to the fact that I couldn't even
define an enumerator for the Names property:

Dim e as IEnumerator = xlApp.ActiveWorkbook.Names.GetEnumerator

This statement also throws an the same exception, and yet GetEnumerator
is one of the methods that Visual Studio suggests for Names.

--
John Brock
jb****@panix.com

Nov 21 '05 #5
In article <df**********@reader1.panix.com>, jb****@panix.com says...
In article <MP************************@msnews.microsoft.com >,
Tushar Mehta <tm****************@tushar-mehta.SeeOhEm> wrote:

Damn! I was doing it correctly from the beginning, except I was
using 0-based indexing, and misunderstood the error message. Ouch!
I'm new to VB.NET, and there is a lot I like about it, but while
I am sure there are valid historical reasons for mixed indexing it
is still a huge misfeature. Personally I actually prefer 1-based
indexing -- which I think makes me a minority -- but please, one
or the other!!!
LOL! Yes, it can get confusing, but that inconsistency has been around
for a while. Even if we restrict ourselves to Office, the userform
components (such as ListBox) have been zero-relative whereas
application collections have always been 1-relative.

The same issues affect .Net. It would have been nice if MS had carried
forward the VB tradition of any lower bound for an array instead of
forcing it to be zero. However, this requirement applies only to .Net
*arrays.* The Names collection, on the other hand, is a collection and
also a part of the XL object model.

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article <df**********@reader1.panix.com>, jb****@panix.com says... In article <MP************************@msnews.microsoft.com >,
Tushar Mehta <tm****************@tushar-mehta.SeeOhEm> wrote:
Does Console have a Debug member? VS Intellisense indicated otherwise.


Um..., I meant Debug.Writeline. :-/
The following works fine. Two things worth noting. Usually, I have
Option Strict On. That disallows late binding and requires all the
type casts in the code. Also, VS Intellisense indicated that the Names
collection has no default property.

Dim i As Integer
With ThisApplication.ActiveWorkbook
For i = 1 To .Names.Count
CType(CType(.ActiveSheet, Excel.Worksheet). _
Cells(i, 1), Excel.Range).Value = _
.Names.Item(i).Name
Next i
End With


Damn! I was doing it correctly from the beginning, except I was
using 0-based indexing, and misunderstood the error message. Ouch!
I'm new to VB.NET, and there is a lot I like about it, but while
I am sure there are valid historical reasons for mixed indexing it
is still a huge misfeature. Personally I actually prefer 1-based
indexing -- which I think makes me a minority -- but please, one
or the other!!!
In article <df**********@reader1.panix.com>, jb****@panix.com says...
In article <11*********************@g44g2000cwa.googlegroups. com>,
Harlan Grove <hr*****@aol.com> wrote:
>John Brock wrote...
>...
>>But try as I might I can't seem to figure out how to get a list of
>>the actual names! I tried giving the Item property an integer
>>index, but that throws an exception. Can anyone give me a clue?

>VB.NET provides an object type for Excel defined names, doesn't it? If
>that type were Microsoft.Office.Interop.Excel.Name, then try
>
>
>Dim n As Microsoft.Office.Interop.Excel.Name
>
>For Each n In xlApp.ActiveWorkbook.Names
> MsgBox Prompt:=n.RefersToRange.Address, Title:=n.Name
>Next n
>
>
>If you're coding in Visual Studio, doesn't it provide an object
>browser? If so, you should be able to find all the Excel object types
>in it.

Visual Studio shows me plenty of object types, I just can't get
anything to work.

I tried your suggestion, but it still doesn't work. I didn't want
to throw up messages boxes, so I did it this way:

Dim n As Microsoft.Office.Interop.Excel.Name

For Each n In xlApp.ActiveWorkbook.Names
Console.Debug("Name is: " & n.Name)
Next

I get a COMException exception, with the additional information
"Member not found".

I wonder if this could be related to the fact that I couldn't even
define an enumerator for the Names property:

Dim e as IEnumerator = xlApp.ActiveWorkbook.Names.GetEnumerator

This statement also throws an the same exception, and yet GetEnumerator
is one of the methods that Visual Studio suggests for Names.


Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: dbarchitech | last post by:
hi, i'm building a query to find narrow ranges of zip codes within broader ranges in a table (for tax purposes). for example: LOW_ZIP HI_ZIP 23400 23499 need to find 23401 ...
18
by: Peter Hardy | last post by:
Hi Guys, Can I use named parameters in C# Methods. i.e doFoo(fname="do", lname="Foo"); Cheers, Pete
1
by: jayouldo | last post by:
Hi guys, in excel we're creating named ranges from the string values in cells. Some of these cells have invalid characters for range names in them, but the ranges HAVE been given these names. Now...
2
by: Alexandru | last post by:
I recently faced the following problem: I have an increasing sequence a1 <= a2 <= ... <= an <= ... The sequence is generated by a function f such that ai = f(i). Now I want to search for a certain...
3
by: DBC User | last post by:
Hi all, I would like to know is there a way to find out if any named pipes are running in a box. How can I go about doing that? Or is there a way to find out if a particular named pipe already...
0
by: rogerford | last post by:
1) I have a Namedrange which is named Company in sheet 2. Sheet 1 cell A2 has a list created via data validation referencing to this sheet 2 Column A values. 2) If i go to Name Manager (by doing a...
3
by: Eric Abrahamsen | last post by:
Is it possible to use the re module to find runs of characters within a certain Unicode range? I'm writing a Markdown extension to go over text and wrap blocks of consecutive Chinese characters...
2
by: bcr123 | last post by:
I am making mistake and I can’t understand what I am doing wrong. Dynamic named ranges is what I was busy with in Excel 2003. This is practical problem: In worksheet `A` I have data in...
11
by: Laurel Eppstein | last post by:
I have no hair left with this issue! I'm simply trying to import data from named ranges in Excel 2003, into tables of the same name in Access 2003. I have found that unless I have the spreadsheet...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.