我目前有一个工作代码,它从我的 excel 电子表格中获取每个图表,并创建一个 PowerPoint 演示文稿,将我的所有图表放在同一张幻灯片上。我希望宏在每张幻灯片上放置四 (4) 个图表,但我遇到了麻烦,感谢您提供任何帮助。(注意 - 我没有在 PowerPoint 中调整图表的大小,我会在之后处理这个问题我在每张幻灯片中得到 4 个)我当前的代码如下所示
Private Sub CommandButton17_Click()
'Add a reference to the Microsoft PowerPoint Library by:
'1. Go to Tools in the VBA menu
'2. Click on Reference
'3. Scroll down to Microsoft PowerPoint X.0 Object Library, check the box, and press Okay
'keep button in same location
Set btn = ActiveSheet.Shapes("CommandButton17")
With btn
btLeft = .Left
btTop = .Top
End With
'First we declare the variables we will be using
Dim newPowerPoint As PowerPoint.Application
Dim activeSlide As PowerPoint.Slide
Dim cht As Excel.ChartObject
'Look for existing instance
On Error Resume Next
Set newPowerPoint = GetObject(, "PowerPoint.Application")
On Error GoTo 0
'Let's create a new PowerPoint
If newPowerPoint Is Nothing Then
Set newPowerPoint = New PowerPoint.Application
End If
'Make a presentation in PowerPoint
If newPowerPoint.Presentations.Count = 0 Then
newPowerPoint.Presentations.Add
End If
'Show the PowerPoint
newPowerPoint.Visible = True
' newPowerPoint.ActivePresentation.ApplyTemplate _
' "D:\Documents and Settings\austin.plantz\Desktop\Misc Projects\CSA PP Theme.thmx"
'Loop through each chart in the Excel worksheet and paste them into the PowerPoint
For i = 1 To ActiveSheet.ChartObjects.Count
Set cht = ActiveSheet.ChartObjects(i)
' With ActivePresentation.SlideMaster
' .CustomLayouts.Add (1)
' .CustomLayouts(1).Name = "Title And Content"
' End With
'Add a new slide where we will paste the chart
If i - 1 Mod 4 = 0 Then
newPowerPoint.ActivePresentation.Slides.Add newPowerPoint.ActivePresentation.Slides.Count + 1, ppLayoutTitle
End If
newPowerPoint.ActiveWindow.View.GotoSlide newPowerPoint.ActivePresentation.Slides.Count
Set activeSlide = newPowerPoint.ActivePresentation.Slides(newPowerPoint.ActivePresentation.Slides.Count)
'Copy the chart and paste it into the PowerPoint as a Metafile Picture
cht.Select
ActiveChart.ChartArea.Copy
activeSlide.Shapes.PasteSpecial(DataType:=ppPasteMetafilePicture).Select
'Set the title of the slide the same as the title of the chart
'activeSlide.Shapes(1).TextFrame.TextRange.Text = cht.Chart.ChartTitle.Text
'Adjust the positioning of the Chart on Powerpoint Slide
newPowerPoint.ActiveWindow.Selection.ShapeRange.Left = 165
newPowerPoint.ActiveWindow.Selection.ShapeRange.Top = 150
newPowerPoint.ActiveWindow.Selection.ShapeRange.Width = 400
activeSlide.Shapes(2).Width = 200
activeSlide.Shapes(2).Left = 505
activeSlide.Shapes(1).Top = 25
Next
AppActivate ("Microsoft PowerPoint")
Set activeSlide = Nothing
Set newPowerPoint = Nothing
End Sub