?
Solved

Getting Excel Names using OLE Automation

Posted on 2015-01-30
5
Medium Priority
?
243 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 71

Accepted Solution

by:
Qlemo earned 2000 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 46

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 71

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

839 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