Solved

Excel Chart manipulation from delphi

Posted on 2014-01-14
13
1,978 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
[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
  • 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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
 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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.

734 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