Take screenshot and paste to Powerpoint slide

johnsonk

Board Regular
Joined
Feb 4, 2019
Messages
172
Hi, I am trying to take a screenshot of my userform and paste it onto a powerpoint file slide, I need it delete what is on the slide and replace with the new screenshot, I have managed to get it to take a screenshot but that's about it the rest I can not figure out, can anyone help.

This is what I have at the moment.

VBA Code:
Dim MyPPT As Object
Set MyPPT = CreateObject("Powerpoint.application")
MyPPT.Visible = True
MyPPT.Presentations.Open "P:\MyFile.pptm"
Application.SendKeys "(%{1068})"
DoEvents


Regards
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Since you haven't provided all of the relevant information, I have made the following assumptions...

1) The screenshot to be deleted and replaced is located on the first slide of the presentation.

2) The screenshot to be replaced is the last shape in the ZOrder for the slide.

If you need to specify a different slide and/or shape, you'll need to amend the code accordingly. Post back if you need help in making any changes.

VBA Code:
Private Sub CommandButton1_Click()

    Application.SendKeys "(%{1068})", True
  
    DoEvents
  
    Dim MyPPT As Object
    Set MyPPT = CreateObject("Powerpoint.Application")
  
    MyPPT.Visible = True
  
    Dim MyPres As Object
    Set MyPres = MyPPT.Presentations.Open("P:\MyFile.pptm")
  
    Dim leftPos As Single
    leftPos = 10 'default left position in case a shape doesn't already exist
  
    Dim topPos As Single
    topPos = 10 'default top position in case a shape doesn't already exist
  
    With MyPres.slides(1).Shapes 'first slide
        If .Count > 0 Then 'check whether there's at least one shape
            With .Item(.Count) 'last shape in zorder
                leftPos = .Left 'remember left position
                topPos = .Top 'remember top position
                .Delete 'delete shape
            End With
        End If
        .Paste 'paste screenshot
        With .Item(.Count) 'last shape in zorder
            .Left = leftPos 'set left position
            .Top = topPos 'set top position
        End With
    End With
      
End Sub

Hope this helps!
 
Last edited:
Upvote 0
Solution
Domenic, That works perfect, is there a way to do the same but to an open powerpoint file so when the powerpoint file is refreshed it will update.

Regards
 
Upvote 0
To refer to a specific presentation...

VBA Code:
    Set MyPres = MyPPT.Presentations("Presentation1.pptm")

To refer to the active presentation...

VBA Code:
    Set MyPres = MyPPT.ActivePresentation
 
Upvote 0
You're very welcome, I'm glad I could help.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,214,914
Messages
6,122,211
Members
449,074
Latest member
cancansova

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