Search code examples
vbapowerpointcopy-paste

Copy/pasting slide from template PPT to current location in open PPT


I'm trying to put together a macro so that, when I run it, it copy/pastes a slide from a template presentation to after the current slide within the active presentation. I'm 90% of the way there, but can't seem to figure out how to make the paste portion function as desired. All I can find is how to paste it at a specified location (e.g., slide 4), or at the end of the presentation.

This is the MS resource I've referred to so far: https://learn.microsoft.com/en-us/office/vba/api/powerpoint.slides.paste

This is the current version of my code:

  Sub PastefromTemplate()
  Dim tgt, i%

  'open the target presentation
  'use path with the file if it is in a different location
  Set objPresentation = Presentations.Open("source path")

  'copy slide 1 from source presentation
  'change the item number in order to target a different slide
  objPresentation.Slides.Item(1).Copy
 
  'paste the slide in target, currently set to slide 3
  tgt = Array(3)
  Presentations.Item(1).Slides.Paste tgt i

  objPresentation.Close
  End Sub

Thanks for your help!


Solution

  • Here is the solution, was finally able to figure it out:

    Sub Slide9()
    Dim src As Presentation
    Dim trg As Slide
    Dim shp As Shape
    Dim target As Presentation
    
    'Copies slide (change # in Item(#)) from the source presentation
    Set objPresentation = Presentations.Open("source file path")
    objPresentation.Slides.Item(9).Copy
     
    'Closes the source presentation
    objPresentation.Close
    
    'Go back to active presentation and paste, then go to pasted slide
    With ActiveWindow.View
    .Paste
    .GotoSlide (ActiveWindow.View.Slide.SlideIndex)
    End With
            
    End Sub