我使用 follow Sub 从 Excel 文件创建一个 PowerPoint 演示文稿。
Sub Gera_PPT(PFile As String, EFile As String, Plans As Collection)
'officevb.com
'PFile= PowerPoint Template empty powerPoint with Slide Master to presentation
'EFile = Excel File with Charts and Tables
'Plans = A collection with names of sheets to transpose
Dim rg As Range
'objetos usados para o powerpoint
Dim pptA As Object
Dim ppt As Object
Dim sld As Object
Debug.Print "passei 1"
If Not ValidaCaminho(PFile) Then
MsgBox "PowerPoint file not found!", vbInformation
Exit Sub
Else
Set pptA = CreateObject("PowerPoint.Application")
pptA.Visible = msoCTrue
'pptA.WindowState = -1
Set ppt = pptA.Presentations.Open(PFile)
End If
'objetos usados para o Excel
Dim ExA As Excel.Application
Dim wb As Excel.Workbook
Dim sht As Excel.Worksheet
If Not ValidaCaminho(EFile) Then
MsgBox "Excel file not found!!", vbInformation
Exit Sub
Else
Set ExA = New Excel.Application
'ExA.Visible = True
Set wb = Workbooks.Open(EFile, False)
End If
Debug.Print "passei 2"
'For Each sht In wb.Sheets
For i = 1 To Plans.Count
Set sht = wb.Sheets(Plans(i))
Select Case Left(sht.Name, 1)
'Debug.Print "passei 3"
'case is Table
Case "T"
Set sld = ppt.Slides.AddSlide(ppt.Slides.Count + 1, ppt.SlideMaster.CustomLayouts(2))
sld.Select
sld.Shapes.Placeholders(2).Select msoCTrue
Set rg = sht.Range("B4").CurrentRegion
rg.Copy
ppt.Windows(1).View.PasteSpecial ppPasteMetafilePicture
Case "G"
'Case is 1 Chart
If sht.ChartObjects.Count = 1 Then
Set sld = ppt.Slides.AddSlide(ppt.Slides.Count + 1, ppt.SlideMaster.CustomLayouts(2))
sld.Select
sld.Shapes.Placeholders(2).Select msoCTrue
sht.ChartObjects(1).Copy
ppt.Windows(1).View.PasteSpecial ppPasteMetafilePicture
' sld.Shapes.Placeholders.Item(1).TextFrame.TextRange.Text = sht.[A2]
Else
'Case is >1 Chart
Set sld = ppt.Slides.AddSlide(ppt.Slides.Count + 1, ppt.SlideMaster.CustomLayouts(4))
sld.Select
sht.Activate
sld.Shapes.Placeholders(1).TextFrame.TextRange.Text = sht.Range("A2").Value
sld.Shapes.Placeholders(2).Select msoCTrue
sht.ChartObjects(1).Copy
ppt.Windows(1).View.PasteSpecial ppPasteMetafilePicture
sld.Shapes.Placeholders(3).Select msoCTrue
sht.ChartObjects(2).Copy
ppt.Windows(1).View.PasteSpecial ppPasteMetafilePicture
End If
End Select
Next i
'Insert LastSlide
Set sld = ppt.Slides.AddSlide(ppt.Slides.Count + 1, ppt.SlideMaster.CustomLayouts(5))
wb.Close False
ExA.Quit
Strfile = Split(apoio.[PPTFile], "\")
ppt.SaveAs YourFilePath & "\" & Split(Strfile(UBound(Strfile)), ".")(0) & "-" & Format(Date, "ddmmyyyy")
pptA.Quit
MsgBox "Presentation created!", vbInformation
End Sub