如何按 Excel VBA 修改 Powerpoint 图表 ChartData

How to modify Powerpoint Chart ChartData by Excel VBA

我正在尝试使用 Excel-VBA 修改 Powerpoint 中的 ChartData(值)。 ChartData 未链接到其他 Excel 工作簿。

检查: - PowerPoint 已打开 - 所需的演示文稿已打开 - 图表在幻灯片 4

行为: - 图表标题更改 - 带有 ChartData 的工作簿打开 - 运行时错误“424”:需要对象 - 调试标记以下行:

Chart.ChartData.Workbook.Worksheets("Tabelle1").Range("B2:B5").Value = 50

希望有人能帮助我...:-)

Sub ModifyChartData()

Dim pptApp As Object
Dim pptPres As Presentation

Set pptApp = GetObject(, "PowerPoint.Application")
Set pptPres = pptApp.ActivePresentation


pptPres.Slides(4).Shapes("Diagramm1").Chart.ChartTitle.Text = "Sales Overview"
pptPres.Slides(4).Shapes("Diagramm1").Chart.ChartData.Activate
Chart.ChartData.Workbook.Worksheets("Tabelle1").Range("B2:B5").Value = 50
Workbooks.Close

End Sub

示例:

代码:

Set pptApp = GetObject(, "PowerPoint.Application")
Set pptPres = pptApp.ActivePresentation
pptPres.Slides(1).Shapes(1).Chart.ChartTitle.Text = "Sales Overview"
pptPres.Slides(1).Shapes(1).Chart.ChartData.Activate
pptPres.Slides(1).Shapes(1).Chart.ChartData.Workbook.Worksheets("Blad1").Range("B2:B5").Value = 50

结果: