Solved

Getting Excel Names using OLE Automation

Posted on 2015-01-30
5
212 Views
Last Modified: 2015-01-31
I am working on a Delphi project (in XE3) that links to Excel spreadsheets (Excel 2013/365) using OLE Automation.
I am successfully connecting and reading/writing information to cells in a variety of workbooks and worksheets.

I want to be able to read in the list of defined Names that are present in an Excel workbook, and have written the following function to read them in.
I can successfully get the NUMBER of defined names, but when I try and access the Nth element I get an OLE exception 'Member not found'.
I can successfully ADD more name definitions into the list.

So basically .Names.Count exists, but .Names[n] does not
Does anyone know what the correct syntax is to access the Nth defined name?

Thanks

Jon
 
function TXLrun.ExtractNames(AWorkbook : TXLworkbook)  : boolean;
var
  Nobj      : Variant;
  Robj      : Variant;
  Ncount    : integer;
  n         : integer;
begin
  Result := false;
  try
    DebugLog('Extracting named ranges');

    { create an object pointing at source worksheet }
    Ncount := AWorkbook.WBobj.Names.Count; 
   // WORKS TO HERE
   // Ncount is set successfully to the number of defined names that exist in the XLS
    for n := 1 to Ncount do
      begin
        AWorkbook.WBobj.Names[n]; //EXCEPTION HERE ... 'Member not found'
        DebugLog('Name '+Robj.Name);
      end;

    for n := 1 to 2 do
      AWorkbook.WBobj.Names.Add('Name'+IntToStr(n),'A1'); // THIS LINE WORKS THOUGH ...

    DebugLog('Name extraction completed: '+
             IntToStr(Ncount)+' Named Ranges');

    Result := true;
  except on E:Exception do
    LogException('TXLrun.ExtractNames',E.Message);
  end;
end;

Open in new window

0
Comment
Question by:jon_rs
5 Comments
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
With Office or COM Automation you cannot rely (or use) the default action/property in most cases. So with collections you need to use the Item method.
  AWorkbook.WBobj.Names.Item(n);
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
probably a zero-origin list.  Try from 0 to count-1
0
 
LVL 24

Expert Comment

by:jimyX
Comment Utility
Hi Jon,
What are you assigning "AWorkbook.WBobj.Names[n]" to?
    for n := 1 to Ncount do
      begin
        AWorkbook.WBobj.Names[n]; //EXCEPTION HERE ... 'Member not found'
        DebugLog('Name '+Robj.Name);
      end;

Open in new window


Are you able to show Values or Names in that loop?
AWorkbook.WBobj.Names[n].Value;
AWorkbook.WBobj.Names[n].Name;

Open in new window

0
 

Author Comment

by:jon_rs
Comment Utility
jimyX:
Apologies in hacking the code around trying to find a solution the version I posted had an error in the very line that the exception occured:
Code should have read:
Robj := AWorkbook.WBobj.Names[n];

Open in new window


aikimark:
It's definitely a 1 origin list ...

Qlemo:
Thanks ... that's the solution:
The exact syntax is:
Robj := AWorkbook.WBobj.Names.Item(n);

Open in new window


Using [] throws a 'Method Items not support by Automation object' Exception
Interesting (given your comment about relying or using default action/property) then that

WBobj.Worksheets[n] IS allowed and DOES work

and

WBobj.Worksheets.Item(n) throws an exception.

Jon
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
AWorkbook is a TXLworkbook, and might be implemented different in Delphi than in other languages, so particular modifications to the "default" behaviour of COM objects might apply. Using Worksheets[n] is non-standard, and leads in e.g. PowerShell (exposing the raw COM object) to the same issue as with Names.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now