Consulting

Results 1 to 6 of 6

Thread: Code to switch between to open presentations

  1. #1
    VBAX Newbie
    Joined
    Jun 2007
    Posts
    3
    Location

    Question Code to switch between to open presentations

    I need to copy a set of slides from one ppt file to another thru vba.

    I can't figure out how to activate the other ppt file that is not activated so I can then copy the next slide.

    Oh I am newbie to vba needless to say.

    It would go something like this in pseudocode

    Dim pptapp As PowerPoint.Application
    Dim pptpres As PowerPoint.Presentation
    Dim pptslide As PowerPoint.Slide
    Dim pptpres2 As PowerPoint.Presentation
    Dim pptslide2 As PowerPoint.Slide
    'open presentation 1 slides end up here.
    'open presentation 2
    'Loop through slides on presentation 2
    'select slide
    'switch to presentation 1 and paste slide
    'switch back to presentation 2
    'Loop
    'ActivePresentation.Slides.Add(Index:=13, Layout:=ppLayoutText).Select
    ' ActiveWindow.Selection.Copy
    ' ActiveWindow.View.Paste
    ' ActiveWindow.Selection.Copy
    ' ActiveWindow.View.Paste

    I will figure it out if I could just find the way to activate an open presentation other than the on that is activated.

    So e.g. if pptpres is activated I could copy the active slide and then activate pptpres2 and paste it until all done.

    I thought the code would be pptpres.Activate(name of presentation) but no luck.

    Also any could books on ppt vba out there. I can't find a one.

    Thanks much Leon.
    Last edited by Aussiebear; 04-28-2023 at 01:22 AM. Reason: Adjusted the code tags

  2. #2
    VBAX Expert TrippyTom's Avatar
    Joined
    Jul 2005
    Location
    New York, NY (USA)
    Posts
    556
    Location
    Part of the problem is PowerPoint itself. The VBA Object model for PowerPoint is TERRIBLE! It's the worst out of the whole suite of applications and Microsoft should be whipped repeatedly with a wet noodle for making it so user UNfriendly!

    Anyway, I had a similar need in the past, and what I ended up doing was creating the slide layout through code. I tried your way first, but it was proving to be just too unreliable.

    So here's a link to the topic I posted my sample code in:

    http://www.vbaexpress.com/forum/showthread.php?t=12800
    Office 2010, Windows 7
    goal: to learn the most efficient way

  3. #3
    VBAX Newbie
    Joined
    Jun 2007
    Posts
    3
    Location
    Thanks very much for your response TrippyTom.

    I was trying to keep the question simple but realize now I need to go into more detail.

    I am limited to office 2003 and xp.

    Also, those 3 other presentations with 10 slides are made of images of charts pasted from excel into one ppt presentatoin. I manipulated ppt from excel.

    I am not the best programmer and this method is the only way I could figure out how to do it and I am about 5 days into creating that part.

    So I very much just want to put the slides from one file into the other file and order them. I didn't mention that part yet but am thinking I can figure out how to order them by the slide name.

    I appreciate your method but am afraid I would then have to recreate getting the graphs from excel into ppt. ...5 days worth of time. I am not very good at navigating the slides to land the right graph on the right slide also.

    So much of the ppt code I see always uses the activepresentation method of id'ing. At this point I really just want to know how to activate a presentation and then activate the other open presentation...back and forth. Still very grateful for your time. Thank, Leon

  4. #4
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Leon

    I've not coded much with Powerpoint VBA but when I looked at the code in Tom's link I see that he doesn't actually appear to be activating anything.

    In Excel, which I normally code in, it's generally not needed to activate/select anything eg worksheets to work with them.

    Perhaps that's the same in PowerPoint?

    I've just done a small test.

    Sub test()
    Dim prSrc As Presentation 
    Dim prDst As Presentation
    Set prSrc = Presentations(1) ' set a reference to the first presentation
        Set prDst = Presentations(2) ' set a reference to the second presentation
    prSrc.Slides(1).Shapes(1).Copy ' copy the first shape from the first slide in the first presentation
        prDst.Slides(1).Shapes.Paste ' paste into the first slide in the second presentation
    End Sub
    This copies a shape from one presentation to another without activating anything.

    Obviously you'll need something more complicated.
    Last edited by Aussiebear; 04-28-2023 at 01:23 AM. Reason: Adjusted the code tags

  5. #5
    VBAX Newbie
    Joined
    Jun 2007
    Posts
    3
    Location

    Got it

    Thanks Norie,
    that was the piece I needed. I ended up with

    Sub CopySlides()
    Dim pptapp As PowerPoint.Application
    Dim pptpres As PowerPoint.Presentation
    Dim pptslide As PowerPoint.Slide
    Dim pptpres2 As PowerPoint.Presentation
    Dim pptslide2 As PowerPoint.Slide
    Set pptapp = New PowerPoint.Application
    pptapp.Visible = Office.msoTrue
    Set pptpres = pptapp.Presentations.Open("C:\Documents and Settings\llittle\My Documents\AMarketOps\DashboardMarketing\PPTShell_mdf.ppt")
    Set pptpres2 = pptapp.Presentations.Open("C:\Documents and Settings\llittle\My Documents\AMarketOps\DashboardMarketing\PPTShell_paste.ppt")
    Dim x As Integer
    x = 1
    For Each pptslide In pptpres.Slides
        pptpres.Slides(x).Copy
        pptpres2.Slides.Paste
        x = x + 1
    Next
    Last edited by Aussiebear; 04-28-2023 at 01:23 AM. Reason: Added code tags

  6. #6
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Glad that worked.

    It really was a kinda stab in the dark since I've never really coded with PowerPoint.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •