【发布时间】:2021-06-04 09:19:53
【问题描述】:
我想创建一个聚集柱形图并将其添加到 PowerPoint 幻灯片中。我不想在 Excel 中创建该图表,然后使用 PowerPoint.Slide.Shapes.PasteSpecial 方法将其复制过来。我可以毫无问题地创建图表,并在有限的范围内与它进行交互(例如,我可以禁用图例,请参阅代码)。但是,我不知道如何编辑图表所引用的数据。
Private Sub AddGraph()
' Create a new slide
Dim PowerPointApplication As PowerPoint.Application
Set PowerPointApplication = New PowerPoint.Application
Dim Presentation As PowerPoint.Presentation
Set Presentation = PowerPointApplication.Presentations(1)
Dim Slide As PowerPoint.Slide
Set Slide = Presentation.Slides.Add(Presentation.Slides.Count + 1, ppLayoutText)
' Remove the existing shape 2
Slide.Shapes(2).Delete
' Add the chart
Slide.Shapes.AddChart _
Type:=xlColumnClustered
With Slide.Shapes(2).Chart
.Legend.Delete
' Try to edit some of the default data of the cluster column chart
' This causes a Subscript out of Range error
WorkBooks("Chart in Microsoft PowerPoint").Cells(2,1) = 2021
End With
End Sub
当 PowerPoint 创建图表时,它似乎还创建了一个临时(?)Excel 工作簿来存储数据。我尝试访问该工作簿以编辑默认数据,例如 WorkBooks("Chart in Microsoft PowerPoint").Cells(2,1) = 2021,但这会抛出下标超出范围错误。
有没有办法编辑在 PowerPoint 中创建图表时创建的 Excel 工作簿?我是否在一条没有结果的道路上走得太远了;有没有更简单的方法来完成这项工作(不包括在 Excel 工作簿中创建所有内容,然后将该图表复制到 PowerPoint 演示文稿中)?
【问题讨论】:
标签: vba charts powerpoint