Delphi - 在Excel中更改图表标题导致AV - 更新完整的示例 [英] Delphi - Changing Chart Title in Excel causes AV - Updated with complete sample

查看:320
本文介绍了Delphi - 在Excel中更改图表标题导致AV - 更新完整的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Delphi Seattle在Excel中构建和显示图表(2013)。我从透视表中构建我的图表。图表显示一切正常。它有一个默认标题,所以我想改变它。这应该是一个简单的属性更改,但我不断得到AV错误。当我google,最接近的事情,我可以找到提到我需要选择图表和/或图表标题,我改变之前。 [UPDATED] 这是一个显示问题的工作示例。

I am using Delphi Seattle to build and display a Chart in Excel (2013). I build my chart from a pivot table. The chart displays everything fine. It has a default title, so I want to change that. This should be a simple property change, but I keep getting AV errors. When I google, the closest thing I can find mentions that I need to select the Chart and/or Chart title before I change it. [UPDATED] Here is a working sample that shows the problem.

procedure TForm1.Button1Click(Sender: TObject);
var
  oExcel : ExcelApplication;
  RawDataSheet :_Worksheet;
  myChart: Shape;
begin
    oExcel := CreateOleObject('Excel.Application') as ExcelApplication;
    oExcel.Visible[LOCALE_USER_DEFAULT] := True;

   // Add a New Workbook, with a single sheet
   oExcel.Workbooks.Add(EmptyParam, LOCALE_USER_DEFAULT);
   // Get the handle to the active Sheet, and insert some dummy data
   RawDataSheet :=  oExcel.ActiveSheet as _Worksheet;
   RawDataSheet.Range['A1', 'B10'].value2 := 10;

    // Now add my chart
    myChart := RawDataSheet.Shapes.AddChart2(208, xlColumnClustered, 200, 10,  300, 300, True);

    // Try to access the Chart Title...  This always AVs here.
    myChart.Chart.HasTitle[LOCALE_USER_DEFAULT] := True;

    // Set Title Text.  If Comemnt out above line, this AVs as well
    myChart.Chart.ChartTitle[LOCALE_USER_DEFAULT].Caption := 'New Chart Title';

end;

我找不到任何方法来更改我的标题。我甚至找不到任何方法来读取现有的标题,更不用说改变它了。

I cannot find any way to change my Title. I can't even find any way to READ the existing title, let alone change it.

ChartTitle的Microsoft对象模型文档说这是正确的属性。 。( https://msdn.microsoft.com/en-us/ library / office / ff840521.aspx

The Microsoft Object Model documentation for ChartTitle says that this is the correct property...(https://msdn.microsoft.com/en-us/library/office/ff840521.aspx)

其他信息:我已经生成了自己的类型库Excel_TLB,我在USES子句中有。我完整的USES子句是...

Additional Info: I did generate my own type library, Excel_TLB, and I have that in my USES clause. My complete USES clause is...

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, ComObj, Excel_TLB;


推荐答案

我可以设置一个标题如下。区别是选择图表,以便能够通过 ActiveChart 引用它。注意,我有一个没有 AddChart2 的办公室14,所以我使用 AddChart

I was able to set a title as below. The difference is to select the chart to be able to refer to it through ActiveChart. Note that I have office 14 which doesn't have AddChart2, so I used AddChart.

uses
  comobj, excel_tlb;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  oExcel : ExcelApplication;
  RawDataSheet :_Worksheet;
  myChart: Shape;
begin
    oExcel := CreateOleObject('Excel.Application') as ExcelApplication;
    oExcel.Visible[LOCALE_USER_DEFAULT] := True;

   // Add a New Workbook, with a single sheet
   oExcel.Workbooks.Add(EmptyParam, LOCALE_USER_DEFAULT);
   // Get the handle to the active Sheet, and insert some dummy data
   RawDataSheet :=  oExcel.ActiveSheet as _Worksheet;
   RawDataSheet.Range['A1', 'B10'].value2 := 10;

    // Now add my chart
//    myChart := RawDataSheet.Shapes.AddChart2(208, xlColumnClustered, 200, 10,  300, 300, True);
    myChart := RawDataSheet.Shapes.AddChart(xlColumnClustered, 200, 10,  300, 300);
    myChart.Select(False);

    oExcel.ActiveChart.HasTitle[LOCALE_USER_DEFAULT] := True;
    oExcel.ActiveChart.ChartTitle[LOCALE_USER_DEFAULT].Caption := 'New Chart Title';
end;

另外请注意,我不知道传递给选择。

Also note, I don't know the implication of the parameter that's passed to Select.


另一种选择是在某些时候放弃类型安全并且依赖于MS文档中找到的VBA示例(其与生成的类型库的签名不匹配)。这允许直接在图表上工作。


Another option that works is to let go of type safety after some point (where it doesn't work anymore) and rely on VBA examples found on MS documentation (which does not match signatures with that of generated type library). This allowed to work on the chart directly.

procedure TForm1.Button1Click(Sender: TObject);
var
  oExcel : ExcelApplication;
  RawDataSheet :_Worksheet;
  myChart: Shape;
  V: OleVariant;
begin
    oExcel := CreateOleObject('Excel.Application') as ExcelApplication;
    oExcel.Visible[LOCALE_USER_DEFAULT] := True;

   // Add a New Workbook, with a single sheet
   oExcel.Workbooks.Add(EmptyParam, LOCALE_USER_DEFAULT);
   // Get the handle to the active Sheet, and insert some dummy data
   RawDataSheet :=  oExcel.ActiveSheet as _Worksheet;
   RawDataSheet.Range['A1', 'B10'].value2 := 10;

    // Now add my chart
//    myChart := RawDataSheet.Shapes.AddChart2(208, xlColumnClustered, 200, 10,  300, 300, True);
    myChart := RawDataSheet.Shapes.AddChart(xlColumnClustered, 200, 10,  300, 300);

    V := myChart.Chart;
    V.HasTitle := True;
    V.ChartTitle.Caption := 'Chart Title';
end;

这篇关于Delphi - 在Excel中更改图表标题导致AV - 更新完整的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆