我使用VBA在Excel中创建了一些图表。现在我想把它发送到我的PP模板,并在同一幻灯片中排列4张图表,然后跳到下一张幻灯片,再添加4张图表。所有图表都需要调整大小和重新排列。我成功地导出了前4个图表,但当我想要安排它们并适应大小时,我就会遇到问题。我有有限的VBA经验,没有使用VBA与MS PP的经验。
到目前为止我的代码是:
Dim PPT As Object
Dim chr
Set PPT = CreateObject("Powerpoint.Application")
PPT.Visible = True
PPT.Presentations.Open Filename:="C:\VBA Projects\XXX\XXX.ppt"
' Set PPT = Nothing
PPT.ActiveWindow.View.GotoSlide 4
For Each chr In Sheets("Output").ChartObjects
chr.Select
ActiveChart.CopyPicture Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture
PPT.ActiveWindow.View.Paste
Next chr
End Sub
我如何在图表中进行选择并单独操作它们?
谢谢
发布于 2021-08-11 13:26:15
将图表粘贴到幻灯片中后,可以使用以下代码引用当前粘贴的图表的属性并设置其属性。
With PPT.ActiveWindow.View.Slide
With .Shapes(.Shapes.Count)
'set properties for shape
'
'
End With
End With
顺便说一句,我建议你代替.
ActiveChart.CopyPicture Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture
使用
chr.Chart.CopyPicture Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture
否则,如果包含图表的工作表不是活动工作表,则会出现错误。
编辑
下面的代码将循环遍历工作表“输出”中的每个ChartObject对象,然后将每个对象复制到PowerPoint演示文稿中,以便每张幻灯片包含4个图表,从第4张幻灯片开始。根据需要更改属性设置。
Const START_LEFT_POS As Long = 25
Const START_TOP_POS As Long = 60
Const GAP As Long = 30 'gap between charts
Dim LeftPos As Long
LeftPos = START_LEFT_POS
Dim TopPos As Long
TopPos = START_TOP_POS
Dim NextSlideIndex As Long
NextSlideIndex = 4
PPT.ActiveWindow.View.GotoSlide NextSlideIndex
With Sheets("Output")
Dim ChrtIndex As Long
For ChrtIndex = 1 To .ChartObjects.Count
.ChartObjects(ChrtIndex).Chart.CopyPicture Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture
PPT.ActiveWindow.View.Paste
With PPT.ActiveWindow.View.slide
With .Shapes(.Shapes.Count)
.Left = LeftPos
.Top = TopPos
.Width = 200
.Height = 200
If ChrtIndex Mod 2 = 1 Then
LeftPos = LeftPos + .Width + GAP
Else
LeftPos = START_LEFT_POS
TopPos = TopPos + .Height + GAP
End If
End With
End With
If ChrtIndex Mod 4 = 0 Then
LeftPos = START_LEFT_POS
TopPos = START_TOP_POS
NextSlideIndex = NextSlideIndex + 1
PPT.ActiveWindow.View.GotoSlide NextSlideIndex
End If
Next ChrtIndex
End With
https://stackoverflow.com/questions/68742175
复制相似问题