Solved

Excel Chart manipulation from delphi

Posted on 2014-01-14
13
1,778 Views
Last Modified: 2014-01-23
I have three worksheets in a workbook.  The second and third sheet each have a chart.  I am trying to access one of these charts to change the title and data range.  

I have defined

XLApp: TExcelApplication;
ExcelWS: ExcelWorksheet;
Chart: ExcelChart;

I can access a worksheet with

  ExcelWS := XLApp.WorkSheets.Get_Item(2) as ExcelWorkSheet;

However I am unable to access the Chart.

  Chart := XLApp.Charts[1] as ExcelChart;

  Chart := XLApp.Charts.Get_Item(1) as ExcelChart;

both give me an Invalid index error

Does any one have any idea of the correct syntax to achieve this.

Delphi v6 with Excel2000.pas
Excel 2010
Windows 7 Professional

Many thanks
0
Comment
Question by:rogerfg7
  • 6
  • 5
  • 2
13 Comments
 
LVL 22

Accepted Solution

by:
rspahitz earned 250 total points
ID: 39779798
Not sure, but try using "Shapes" rather than "Chart"
for some reason, Excel uses some strange internal names.
One way to start getting this information is to record a macro in Excel.  From there, much of it should be easy to translate into Delphi (which I haven't worked with in a dozen years)
0
 

Author Comment

by:rogerfg7
ID: 39779969
I need to obtain a Chart reference to be able to do anything.  

The Excel macro suggests that I should use

ActiveSheet.ChartObjects("Chart 1").Activate

This gives a compile error of Not enough actual parameters - add lcid

ActiveSheet.ChartObjects("Chart 1",lcid).Activate

error Undeclared identifier Activate.  Try

  Chart := ExcelWS.ChartObjects('Chart 1',lcid) as ExcelChart;

gives run time error Interface not supported.

It ain't as simple as that!!
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 39780990
If you iterate through the Shapes, I think that you may be able to check the object type and map it to a chart when you find it...but again, I'm not sure how that would be implemented in Delphi.
0
 
LVL 19

Assisted Solution

by:Thommy
Thommy earned 250 total points
ID: 39781704
This is the way you can iterate the charts

    for i:=1 to XLApp.ActiveWorkBook.Sheets[1].ChartObjects.count do begin
      ShowMessage(XLApp.ActiveWorkbook.Sheets[1].ChartObjects(i).name);
      XLApp.ActiveWorkbook.Sheets[1].ChartObjects(i).Activate;
      ...
    end;

Open in new window

You can also access a chart directly by its name:
XLApp.ActiveWorkbook.Sheets[1].ChartObjects('chart 1').Activate;

Open in new window

0
 

Author Comment

by:rogerfg7
ID: 39781931
Combination of above replies
var
  XLShape: Shape;
  i,j: integer;
begin
  for i:= 1 to XLApp.ActiveWorkbook.Sheets.Count do
  begin
    ExcelWS := XLApp.ActiveWorkbook.Sheets[i] as ExcelWorksheet;
    for j:=1 to ExcelWS.Shapes.Count do
    begin
      XLShape := ExcelWS.Shapes.Item(j);
      Memo1.Lines.Add(Format('%d:%d %s',[i,j,XLShape.Name]));
    end;
  end;

Open in new window

Works and I get a list of Chart names with the sheet and shape index.  However, I cannot get an ExcelChart reference from XLShape to do any chart manipulation.
Chart := XLShape as ExcelChart;

Open in new window

gives an Interface not supported.
Chart := ExcelChart(ExcelShape);
Memo1.Lines.Add(Format('%d:%d %s',[i,j,Chart.Name]));

Open in new window

gives an Access violation at Chart.Name.

So near and yet so far
0
 
LVL 19

Expert Comment

by:Thommy
ID: 39782036
Forget the shapes!!!

Get your list of chart names as below:
for i:= 1 to XLApp.ActiveWorkbook.Sheets.Count do
  begin
    ExcelWS := XLApp.ActiveWorkbook.Sheets[i] as ExcelWorksheet;
    for j:=1 to ExcelWS.ChartObjects.count do
    begin
        Memo1.Lines.Add(Format('%d:%d %s',[i,j,ExcelWS.ChartObjects(j).name]));
    end;
  end;

Open in new window

0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

Author Comment

by:rogerfg7
ID: 39782140
The only method available is

ChartObjects(Index: OleVariant; lcid: Integer): IDispatch.

Providing parameters to ChartObjects

ExcelWS.ChartObjects(1,lcid).Count

Undeclared identifier Count
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 39782764
It would be so nice if the Chart objects worked they way they should.  It seems that maybe they do for Thommy.  Could there be an added reference in the project to get that?
Anyway, for Shapes, is there a way in Delphi to cast it to the Chart?
In VB, you can use TypeName() to determine the object type then create a variable of that type

Here's some sample VBA to examine each of the charts on the current sheet.  You may be able to adapt this code to what you need:

    Dim idx As Integer
    Dim shp As Shape
    Dim cht As Chart
    Dim chtArea As ChartArea
    For idx = 0 To ActiveSheet.Shapes.Count
        Set shp = ActiveSheet.Shapes(1)
        Set cht = shp.Chart
        Set chtArea = cht.ChartArea
        ' do something with the ChartArea object...
    Next

Open in new window

0
 

Author Comment

by:rogerfg7
ID: 39785729
Unfortunately Shape does not have a Chart property in Delphi 6 implementation of the automation interface (excel2000.pas)
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 39797842
There's no way to cast it to that?
I've also found that in VBA you can simply use the methods on an item of type Object and it still works.  Not sure if your compiler will let you do that.
0
 

Author Comment

by:rogerfg7
ID: 39803479
Finally cracked it after lots of trial and error

    
var
  ExcelWS : ExcelWorksheet;
  fChart: Variant;
  fRangeStart, fRangeEnd: OLEVariant;  // Variant doesn't work!!

begin
    ExcelWS := XLApp.ActiveWorkbook.Sheets[i] as ExcelWorksheet;
    fChart := ExcelWS.ChartObjects(1,lcid);
    fRangeStart := 'A'+intToStr(fStartLine);  // Format('A%d',[fStartLine]) doesn't work!!!
    fRangeEnd := 'E'+intToStr(fEndLine);
    fChart.Chart.SetSourceData(ExcelWS.Range[fRangeStart,fRangeEnd],xlColumns);

Open in new window


This compiles, runs and does what I want.

Thanks everyone for your assistance, it shouldn't be that hard, but that's Microsoft!!
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 39803639
So it looks like you had to use the generic data type Variant instead of Object...the old VB did it that way but the new one uses Object.
And OLEVariant? That sounds like some obsolete name for an Object!
Glad you got it working.
0
 

Author Comment

by:rogerfg7
ID: 39804599
Eventually got it working with Variant, not sure why it objected first time as it gives the same error code for any OLE error.

I guess using a variant allows the application to do late binding at run time rather than early binding at compile time, but the syntax is a case of try it and see.  I know I never get round to documenting my systems, but you think Microsoft would try to lead by example.

Thanks for your help, now have to figure how to split points.
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

The canonical version of this article is on my web site here: http://iconoun.com/articles/collisions/ A companion presentation is available here: http://iconoun.com/articles/collisions/Unicode_Presentation.pdf
As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

911 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

18 Experts available now in Live!

Get 1:1 Help Now