Solved

Excel Chart manipulation from delphi

Posted on 2014-01-14
13
1,746 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:rogerfg7
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Leon
Software Metering within our group of companies has always been an afterthought until auditing of software and licensing became a pain point. Orchestrator and SCCM metering gave us the answer and it was an exciting process.
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

744 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

15 Experts available now in Live!

Get 1:1 Help Now