重新定位图表对象VBA

时间:2017-05-04 08:47:40

标签: vba runtime-error powerpoint-vba

我正在尝试创建一个图表,然后将其重新定位到我的PowerPoint工作表上的正确位置。但是收到运行时错误。相关的代码位是:

(我的图表数据不在代码中)

Dim myChart As chart
Dim gChartData As ChartData
Dim gWorkBook As Excel.Workbook
Dim gWorkSheet As Excel.Worksheet

'Create the chart and set a reference to the chart data.
Set myChart = ActivePresentation.Slides(2).Shapes.AddChart.chart
Set gChartData = myChart.ChartData

'Set the Workbook and Worksheet references.
Set gWorkBook = gChartData.Workbook
Set gWorkSheet = gWorkBook.Worksheets(1)

    With myChart
        .ChartType = xlColumnStacked
        .ChartStyle = 30
        .ApplyLayout 4
        .ClearToMatchStyle
    End With

    With myChart
        .PlotArea.Left = 290
        .PlotArea.Top = 90
    End With

我收到以下错误代码:

运行时

错误:-2147467259(80004005):

对象PlotArea失败的方法

有些搜索已经建议我可能有宏安全问题,但在选项中目前允许所有宏操作。

如果您需要进一步的信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

您的代码正在将图表移动到已添加到的形状内,而不是移动容器形状本身。

您需要移动Sub Test() Dim myChart As Chart Set myChart = ActivePresentation.Slides(2).Shapes.AddChart.Chart With myChart .ChartType = xlColumnStacked .ChartStyle = 30 .ApplyLayout 4 .ClearToMatchStyle 'Move shape containing chart on PPT. With .Parent .Left = 290 .Top = 90 End With 'Move chart plot area within chart (I think). .PlotArea.Left = 290 .PlotArea.Top = 90 End With End Sub 的父级:

[HttpPost]
public ActionResult Index(Object obj)
{
  //Your code goes here...
}

我没有得到你的错误,但那可能是试图将PlotArea移到形状之外?