Run-Time Error when Adding Chart During PowerPoint

2019-08-10 23:18发布

问题:

I have used vba in PowerPoint to add a chart to a slide. However, this only seems to work when I am in normal/slide view/edit mode. When I try to do this during a slideshow however, I get a runtime error saying: Method 'AddChart' of object 'Shapes' failed.

The issue is that I'm using user input during the slideshow to filter what kinds of data I query and then I create new slides based on that data. Using standalone macros in slide view/edit mode wont help.

Here's the relevant portion of my code... any suggestions would be greatly appreciated.

Set oSlide = GlobalFunctions.AddCustomSlide_MDA("Blank")
oSlide.Shapes(1).TextFrame.TextRange = "Change Summary - Time Frames"

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 = oSlide.Shapes.AddChart.Chart
Set gChartData = myChart.ChartData

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

' Add the data to the workbook.
gWorkSheet.ListObjects("Table1").Resize eWorkSheet.Range("A1:B13")
gWorkSheet.Range("Table1[[#Headers],[Series 1]]").value = "Changes"
gWorkSheet.Range("A2").value = "Past Date"
gWorkSheet.Range("B2").value = iPrevious
gWorkSheet.Range("A3").value = "This Week"
gWorkSheet.Range("B3").value = iThisWeek
gWorkSheet.Range("A4").value = "Next Week"
gWorkSheet.Range("B4").value = i1Week
gWorkSheet.Range("A5").value = "2 Weeks"
gWorkSheet.Range("B5").value = i2Weeks
gWorkSheet.Range("A6").value = "3 Weeks"
gWorkSheet.Range("B6").value = i3Weeks
gWorkSheet.Range("A7").value = "4 Weeks"
gWorkSheet.Range("B7").value = i4Weeks
gWorkSheet.Range("A8").value = "5 Weeks"
gWorkSheet.Range("B8").value = i5Weeks
gWorkSheet.Range("A9").value = "6 Weeks"
gWorkSheet.Range("B9").value = i6Weeks
gWorkSheet.Range("A10").value = "7 Weeks"
gWorkSheet.Range("B10").value = i7Weeks
gWorkSheet.Range("A11").value = "8 Weeks"
gWorkSheet.Range("B11").value = i8Weeks
gWorkSheet.Range("A12").value = "9+ Weeks"
gWorkSheet.Range("B12").value = i9Weeks
gWorkSheet.Range("A13").value = "No Dates"
gWorkSheet.Range("B13").value = iNoWeeks

' Apply styles to the chart.
With myChart
    .ChartStyle = 4
    .ApplyLayout 4
    .ClearToMatchStyle
End With

' Add the axis title.
With myChart.Axes(xlValue)
    .HasTitle = True
    .AxisTitle.Text = "Units"
End With

'myChart.ApplyDataLabels

' Clean up the references.
Set gWorkSheet = Nothing
gWorkBook.Application.Quit
Set gWorkBook = Nothing
Set gChartData = Nothing
Set myChart = Nothing

Thanks