I made it work, but I need now to copy text from a range in Excel to powerpoint, when code goes to this points it gets me an error 424 (Line bold)
Can you help me please
Sub Open_PowerPoint_Presentation()
'Opens a PowerPoint Document from Excel
Dim objPPT As Object
Dim PPPres As PowerPoint.Presentation
Dim PPApp As PowerPoint.Application
Dim Name As Variant
Set objPPT = CreateObject("PowerPoint.Application")
objPPT.Visible = True
objPPT.Presentations.Open "C:\Users\jose_jimenez2\Documents\test\test.ppt"
Set PPApp = GetObject(, "Powerpoint.Application")
Set PPPres = PPApp.ActivePresentation
copy_chart "sheet1", 2 ' Name of the sheet to copy graph and slide number the graph is to be pasted in
Sheets("Sheet1").Select
Range("P1").Select
Name = Cells(1, 16)
If Name = 1 Then
With PPPres
.SaveAs "C:\Users\jose_jimenez2\Documents\test\test1\test1.ppt"
End With
Else
If Name = 2 Then
With PPPres
.SaveAs "C:\Users\jose_jimenez2\Documents\test\test2\test2.ppt"
End With
Else
If Name = 3 Then
With PPPres
.SaveAs "C:\Users\jose_jimenez2\Documents\test\test3\test3.ppt"
End With
Else
End If
End If
End If
' Set PPSlide = Nothing
'Set PPPres = Nothing
'Set PPApp = Nothing
End Sub
Public Function copy_chart(sheet, slide)
' Uses Early Binding to the PowerPoint Object Model
' Set a VBE reference to Microsoft PowerPoint Object Library
Dim PPApp As PowerPoint.Application
Dim PPPres As PowerPoint.Presentation
Dim PPSlide As PowerPoint.slide
Dim oPPShape As Object
Dim oPPSlide As Object
' Reference existing instance of PowerPoint
Set PPApp = GetObject(, "Powerpoint.Application")
' Reference active presentation
Set PPPres = PPApp.ActivePresentation
PPApp.ActiveWindow.ViewType = ppViewSlide
'Copy "Chart 1" on "Sheet1" to Slide # 2
' Copy "Chart 1" on "Sheet1" as a picture
ActiveWorkbook.Sheets("Sheet1").ChartObjects("Chart 1").CopyPicture
' Paste chart to Slide # 2
With PPPres.Slides(2).Shapes.Paste
' Align pasted chart
'.Align msoAlignCenters, True
'.Align msoAlignMiddles, True
.LockAspectRatio = msoFalse
.Width = 534.9034263959
.Height = 165.5603448276
.Left = 0
.Top = 34.2786890756
Set oPPShape = oPPSlide.Shapes(1)
'~~> Write to the shape
oPPShape.TextFrame.TextRange.Text = _
TActiveWorkbook.Sheets("Sheet1").Range("C2:C5").Value
End With
'Copy "Chart 2" to from "Sheet1" to Slide #
ActiveWorkbook.Sheets("Sheet1").ChartObjects("Chart 2").CopyPicture
' Paste chart to Slide # 4
With PPPres.Slides(4).Shapes.Paste
' Align pasted chart
' .Align msoAlignCenters, True
'.Align msoAlignMiddles, True
.LockAspectRatio = msoFalse
.Width = 534.9034263959
.Height = 165.5603448276
.Left = 0
.Top = 34.2786890756
And With
'Copy "Chart 3" to from "Sheet1" to Slide # 5
ActiveWorkbook.Sheets("Sheet1").ChartObjects("Chart 3").CopyPicture
' Paste chart to Slide # 4
With PPPres.Slides(5).Shapes.Paste
' Align pasted chart
'.Align msoAlignCenters, True
'.Align msoAlignMiddles, True
.LockAspectRatio = msoFalse
.Width = 534.9034263959
.Height = 165.5603448276
.Left = 0
.Top = 34.2786890756
End With
'Copy "Chart 4" to from "Sheet1" to Slide # 6
ActiveWorkbook.Sheets("Sheet1").ChartObjects("Chart 4").CopyPicture
' Paste chart to Slide # 4
With PPPres.Slides(6).Shapes.Paste
' Align pasted chart
'.Align msoAlignCenters, True
'.Align msoAlignMiddles, True
.LockAspectRatio = msoFalse
.Width = 534.9034263959
.Height = 165.5603448276
.Left = 0
.Top = 34.2786890756
End With
'Copy "Chart 5" to from "Sheet1" to Slide # 7
ActiveWorkbook.Sheets("Sheet1").ChartObjects("Chart 5").CopyPicture
' Paste chart to Slide # 4
With PPPres.Slides(7).Shapes.Paste
' Align pasted chart
'.Align msoAlignCenters, True
'.Align msoAlignMiddles, True
.LockAspectRatio = msoFalse
.Width = 534.9034263959
.Height = 165.5603448276
.Left = 0
.Top = 34.2786890756
End With
'Copy "Chart 6" to from "Sheet1" to Slide # 8
ActiveWorkbook.Sheets("Sheet1").ChartObjects("Chart 6").CopyPicture
' Paste chart to Slide # 4
With PPPres.Slides(8).Shapes.Paste
' Align pasted chart
'.Align msoAlignCenters, True
'.Align msoAlignMiddles, True
.LockAspectRatio = msoFalse
.Width = 534.9034263959
.Height = 165.5603448276
.Left = 0
.Top = 34.2786890756
End With
'Copy "Chart 7" to from "Sheet1" to Slide # 9
ActiveWorkbook.Sheets("Sheet1").ChartObjects("Chart 7").CopyPicture
' Paste chart to Slide # 4
With PPPres.Slides(9).Shapes.Paste
' Align pasted chart
'.Align msoAlignCenters, True
'.Align msoAlignMiddles, True
.LockAspectRatio = msoFalse
.Width = 534.9034263959
.Height = 165.5603448276
.Left = 0
.Top = 34.2786890756
End With
'Copy "Chart 8" to from "Sheet1" to Slide # 10
ActiveWorkbook.Sheets("Sheet1").ChartObjects("Chart 8").CopyPicture
' Paste chart to Slide # 4
With PPPres.Slides(10).Shapes.Paste
' Align pasted chart
'.Align msoAlignCenters, True
'.Align msoAlignMiddles, True
.LockAspectRatio = msoFalse
.Width = 534.9034263959
.Height = 165.5603448276
.Left = 0
.Top = 34.2786890756
End With
'Copy "Chart 9" to from "Sheet1" to Slide # 12
ActiveWorkbook.Sheets("Sheet1").ChartObjects("Chart 9").CopyPicture
' Paste chart to Slide # 4
With PPPres.Slides(12).Shapes.Paste
' Align pasted chart
'.Align msoAlignCenters, True
'.Align msoAlignMiddles, True
.LockAspectRatio = msoFalse
.Width = 534.9034263959
.Height = 165.5603448276
.Left = 0
.Top = 34.2786890756
End With
'Copy "Chart 10" to from "Sheet1" to Slide # 13
ActiveWorkbook.Sheets("Sheet1").ChartObjects("Chart 10").CopyPicture
' Paste chart to Slide # 4
With PPPres.Slides(13).Shapes.Paste
' Align pasted chart
'.Align msoAlignCenters, True
'.Align msoAlignMiddles, True
.LockAspectRatio = msoFalse
.Width = 534.9034263959
.Height = 165.5603448276
.Left = 0
.Top = 34.2786890756
End With
'Copy "Chart 11" to from "Sheet1" to Slide # 14
ActiveWorkbook.Sheets("Sheet1").ChartObjects("Chart 11").CopyPicture
' Paste chart to Slide # 4
With PPPres.Slides(14).Shapes.Paste
' Align pasted chart
'.Align msoAlignCenters, True
'.Align msoAlignMiddles, True
.LockAspectRatio = msoFalse
.Width = 534.9034263959
.Height = 165.5603448276
.Left = 0
.Top = 34.2786890756
End With
' Clean up
Set PPSlide = Nothing
Set PPPres = Nothing
Set PPApp = Nothing
End Function