I came up with the following script:
Sub ChartToPresentation()
Dim ppApp As PowerPoint.Application
Dim ppPres As PowerPoint.Presentation
Dim ppSlide As PowerPoint.Slide
Dim shp As String
Dim newShape As PowerPoint.ShapeRange
Dim cell As Range
Dim rng As Range
Dim RangeName As String
Dim CellName As String
' Make sure a chart is selected
If ActiveChart Is Nothing Then
MsgBox "Please select a chart and try again.", vbExclamation, _
"No Chart Selected"
Else
' Reference existing instance of PowerPoint
Set ppApp = GetObject(, "Powerpoint.Application")
' Reference active presentation
Set ppPres = ppApp.ActivePresentation
' Reference active slide
RangeName = "PPTSlide"
CellName = "B6"
Set cell = Worksheets("VIVA GRAPH").Range(CellName)
Worksheets("VIVA GRAPH").Names.Add Name:=RangeName, RefersTo:=cell
' Copy chart as a picture
ActiveChart.CopyPicture Appearance:=xlScreen, Size:=xlScreen, _
Format:=xlPicture
' Paste chart
Set newShape = ppSlide.Shapes.Paste
With newShape
.IncrementLeft 400
.IncrementTop 250
.ScaleWidth 0.87, msoFalse, msoScaleFromTopLeft
.ScaleHeight 0.87, msoFalse, msoScaleFromTopLeft
End With
' Clean up
Set ppSlide = Nothing
Set ppPres = Nothing
Set ppApp = Nothing
End If
End Sub
I am referring to the sheet "VIVA GRAPH" and made a range (via Name Manager) called "PPTSlide" that is linked to cell B6. However, it gives "Object variable or With block variable not set" at this piece of code: Set newShape = ppSlide.Shapes.Paste.
What am I missing?
Yours sincerely,
Djani