Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Excel Chart manipulation from delphi

Posted on 2014-01-14
13
Medium Priority
?
2,121 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 750 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
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 
LVL 19

Assisted Solution

by:Thommy
Thommy earned 750 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

715 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