Solved

Getting Excel Names using OLE Automation

Posted on 2015-01-30
5
227 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

729 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