Delphi – Modifier le titre du graphique dans Excel provoque AV – Mise à jour avec un échantillon complet

J'utilise Delphi Seattle pour créer et afficher un graphique dans Excel (2013). Je crée mon tableau à partir d'un tableau pivot. Le tableau affiche tout bien. Il a un titre par défaut, donc je veux changer cela. Cela devrait être un changement de propriété simple, mais je continue à avoir des erreurs AV. Quand je google, le plus proche, je peux find des mentions que je dois sélectionner le titre Graphique et / ou Graphique avant de le modifier. [ACTUALISÉ] Voici un échantillon qui montre le problème.

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; 

Je ne trouve aucun moyen de modifier mon titre. Je ne peux même pas find un moyen de lire le titre existant, et encore less de le modifier.

La documentation du model d'object Microsoft pour ChartTitle indique que c'est la propriété correcte … ( https://msdn.microsoft.com/en-us/library/office/ff840521.aspx )

Informations supplémentaires: J'ai généré ma propre bibliothèque de types, Excel_TLB, et je l'ai dans ma clause USES. Ma clause complète d'UTILISATION est …

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

J'ai pu définir un titre comme ci-dessous. La différence consiste à sélectionner le graphique pour pouvoir se référer à celui-ci via ActiveChart . Notez que j'ai Office 14 qui n'a pas AddChart2 , donc j'ai utilisé 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; 

Notez également que je ne connais pas l'implication du paramètre passé à Select .

Une autre option qui fonctionne est de laisser aller la security de type après un certain point (où cela ne fonctionne plus) et countr sur les exemples VBA trouvés sur la documentation MS (qui ne correspond pas aux signatures avec celle de la bibliothèque de types générés). Cela a permis de travailler sur le graphique directement.

 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;