第二张幻灯片中有一个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")
,其中"图表1"应替换为实际的图表名称。