Solved

Getting Excel Names using OLE Automation

Posted on 2015-01-30
5
223 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 70

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40580342
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
ID: 40580633
probably a zero-origin list.  Try from 0 to count-1
0
 
LVL 24

Expert Comment

by:jimyX
ID: 40581208
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
ID: 40581237
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 70

Expert Comment

by:Qlemo
ID: 40581268
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

737 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