2017-05-04 48 views
1

我想创建一个图表并将其重新定位到我的PowerPoint表格上的正确位置。但收到一个运行时错误。的代码中的相关位是:重新定位图表对象VBA

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

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的方法左失败

一些网站的搜索已经建议我可能有一个宏观的安全问题,但在选择所有宏操作都CUR肯定允许。

如果您需要更多信息,请让我知道。

回答

0

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

你需要移动的myChart父:

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 

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

+0

谢谢!这对我有用 – Onno