在Excel中更改图表位置会产生访问冲突

时间:2013-11-03 15:54:40

标签: delphi excel-2010

使用Delphi,我创建一个包含一些数据值的Excel工作表,然后创建一个图表:

WChart := ((ExcelApplication1.Workbooks[1].ActiveSheet) as _Worksheet).Shapes.AddChart(xl3DPie,
                                 EmptyParam, EmptyParam,
                                 EmptyParam, EmptyParam).Chart;

在工作表上正确创建了图表。 现在我想将它移动到这样的新表:

WChart.Location(xlLocationAsNewSheet, EmptyParam);

这会导致访问冲突。

我google了很多,没有发现使用Delphi的例子。所有其他语言(如VBA)都在做与我的代码相同的事情。

任何帮助表示感谢。

编辑:

以下是触发AV的行的汇编代码:

WChart.Location(xlLocationAsNewSheet, EmptyParam);
   lea eax,[ebp-$0000019c]
   call @IntfClear
   push eax
   lea eax,[ebp-$000001ac]
   call EmptyParam
   push dword ptr [ebp-$000001a0]
   push dword ptr [ebp-$000001a4]
   push dword ptr [ebp-$000001a8]
   push dword ptr [ebp-$000001ac]
   push $01
   mov eax,[ebp-$14]                 // [ebp-$14] is WChart value
   push eax
   mov eax,[eax]
   call dword ptr [eax+$00000198]    // Supposed to call Location()
   call @CheckAutoResult

1 个答案:

答案 0 :(得分:2)

我无法弄清楚你遇到的具体问题(我得到了一个不同的AV,但它仍然是AV)。但是,如果您有兴趣,可以采用解决方法。

var
  Book: _Workbook;
  LCID: Integer;
  Chart: _Chart;
begin
  LCID := GetUserDefaultLCID;

  Book := ExcelApplication1.ActiveWorkbook;
  Chart := Book.Charts.Add(EmptyParam, EmptyParam, 
                           EmptyParam, EmptyParam, LCID) as _Chart;

  // Tested, and the second param below can be EmptyParam to leave
  // the new sheet name as "Chart1".
  Chart.Location(xlLocationAsNewSheet, 'ChartSheet');
end;