我在第二张幻灯片中有一个PowerPoint文件和一个图表。在Excel文件中编写宏时,无法设置图表的高度和宽度。下面是我正在尝试的代码。请注意,我只需要从Excel宏修改高度和宽度。
Sub controlPPT()
Dim PPT As Object
Set PPT = CreateObject("PowerPoint.Application")
With Application.FileDialog(1)
.AllowMultiSelect = False
.Show
.Filters.Clear
.Filters.Add "PPT files", "*.pptx"
.FilterIndex = 1
If .SelectedItems.Count > 0 Then
Set slideTwo = PPT.ActivePresentation.Slides(2)
slideTwo.Shapes(1).Chart.PlotArea.Height = 120
slideTwo.Shapes(1).Chart.PlotArea.Width = 200
slideTwo.Shapes(1).Chart.PlotArea.Left = 0
slideTwo.Shapes(1).Chart.PlotArea.Top = 0
End If
End With
End Sub
主要问题是Application.FileDialog
无法打开所选文件,您应该显式打开它:
Sub controlPPT()
Dim pptApp As Object
Dim pres As Object
Dim slideTwo As Object
Set pptApp = CreateObject("PowerPoint.Application")
With Application.FileDialog(1)
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add "PPT files", "*.pptx"
.FilterIndex = 1
.Show
If .SelectedItems.Count > 0 Then
Set pres = pptApp.Presentations.Open(.SelectedItems(1))
Set slideTwo = pres.Slides(2)
slideTwo.Select
With slideTwo.Shapes(1).Chart.PlotArea
.Height = 120
.Width = 200
.Left = 0
.Top = 0
End With
End If
End With
'save/close presentation
pres.Save
pres.Close
'clean up
Set pres = Nothing
pptApp.Quit
Set pptApp = Nothing
End Sub
也为可靠性我会改变slideTwo.Shapes(1)
对slideTwo.Shapes("Chart1")
其中“Chart1”应与实际图表名称取代。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句