Refer to "value/range" to put chart in specific PPT slide (VBA)

Djani

Board Regular
Joined
Aug 26, 2015
Messages
61
Hi all,

I have a properly working VBA script that copy-pastes the chart into a slide, but I want it to refer to a specific cell so VBA 'knows' in what slide the chart has to be put. This is the code:

Code:
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


' 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
    Set PPSlide = PPPres.Slides(3)
               
    ' 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

However, instead of putting a 'hard value' in it, I want it to use a "range" called "PPTSlide" (from Name Manager). Is it possible to change the script where I change:

Code:
    ' Reference active slide
    Set PPSlide = PPPres.Slides(3)


In something like this?

Code:
Set PPTSlide = ActiveSheet.Range("PPTSlide").Value

I tried to paste this piece of code into the new one, but it said "Object required".

Yours sincerely,

Djani
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Try...

Code:
Set PPSlide = PPPres.Slides(ActiveSheet.Range("PPTSlide").Value)

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top