Consulting

Results 1 to 9 of 9

Thread: Copy slides to new presentation in a specific order

  1. #1

    Unhappy Copy slides to new presentation in a specific order

    Dear forum,

    I am very very new to VBA and have just spent all day creating the macro I need. . . and then accidentally told powerpoint to close the wrong window, and I lost all my code. Lesson learned.

    I was wondering if anyone would be willing to help me recreate my work, as I keep getting everything wrong. What I had was something that would:



    Dim Template As Presentation
    Dim Target As Presentation


    Set Template = Presentations.Open("S:\Documents\Documents\Syllable\Syllable template.pptx")
    Set Target = Presentations.Open("S:\Documents\Documents\Syllable\blank.pptx")


    Dim s As Integer


    s = 20


    Template.Windows(1).Activate
    ActivePresentation.Slides(s).Copy
    Target.Windows(1).Activate
    ActivePresentation.Slides.Paste

    So it would take whatever Template slide number I said and paste it at the end of the Target presentation.

    That's it, only I had that 34 different times, because I need all 34 slides, but in a specified new order. And then I changed the number S was in between each time I told it to copy and paste S.

    Clearly there has to be an easier way to do this. If I have to recreate my work, I might as well try to make it better the second time! Is there a way you can create the copy and pasting business as a function, then say for each value of s, run the function?

    If anyone could help, I would greatly appreciate learning.
    Last edited by spillthebean; 03-14-2014 at 08:17 AM.

  2. #2
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,094
    Location
    Is the intention to just change the order of slides in a presentation??

    If so maybe you could use the move command
    Example
    ActivePresentations.Slides(6).MoveTo 1
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  3. #3
    Hi John, yes I only need to change the order of the slides. I started out yesterday using the move command, but when I ran the script all the slides were still in the wrong order - not where I specified them to go. Perhaps I was using the command incorrectly, but the reason that I wanted to just keep changing the value of S and carrying out a function on the new value was so that I could have something like:

    s=20
    s=8
    s=5

    etc all in a line. I have to change the order several times, and it is very time consuming to keep typing the number of the slides in the brackets every time I needed to change the order for a new presentation.

  4. #4
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,094
    Location
    What do you want the final order to be?

    eg

    8,4,5,3,2,1,6,7
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  5. #5
    Yes, that's right, I know what position each slide needs to be and the new order is like in your example

  6. #6
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,094
    Location
    Try this (obviously add your order instead of 5,3,2,1,4)

    Sub changorder()
    Dim rayOrder() As String
    Dim neworder() As Long
    Dim L As Long
    Dim lngTot As Long
    On Error Resume Next
    lngTot = ActivePresentation.Slides.Count
    rayOrder = Split("5,3,2,1,4", ",")
    ReDim neworder(0 To UBound(rayOrder))
    For L = 0 To UBound(rayOrder)
    neworder(L) = ActivePresentation.Slides(CLng(rayOrder(L))).SlideID
    Next L
    For L = 0 To UBound(rayOrder)
    ActivePresentation.Slides.FindBySlideID(neworder(L)).MoveTo lngTot
    Next L
    End Sub
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  7. #7
    Thank you John, this looks way beyond anything I could have written! Could you possibly clarify how I need to add my order? So, the order change I need to run is:

    22,35,18,6,24,28,30,27,9....

    So slide 22 needs to become slide 1, slide 35 needs to become slide 2, slide 18 needs to become slide 3 etc.

    Could you please show me where I need to put the commas and the speech marks?

  8. #8
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,094
    Location
    Just put the current slide numbers in the order you need. Include all slides

    So something like:

    Sub changeorder()
        Dim rayOrder() As String
        Dim neworder() As Long
        Dim L As Long
        Dim lngTot As Long
        On Error Resume Next
        lngTot = ActivePresentation.Slides.Count
        rayOrder = Split("22,35,18,6,24.....", ",")
        ReDim neworder(0 To UBound(rayOrder))
        For L = 0 To UBound(rayOrder)
            neworder(L) = ActivePresentation.Slides(CLng(rayOrder(L))).SlideID
        Next L
        For L = 0 To UBound(rayOrder)
            ActivePresentation.Slides.FindBySlideID(neworder(L)).MoveTo lngTot
        Next L
    End Sub
    The other comma means split up at each comma. You couls also split at e.g. a "/" which might be asier to understand

    Sub changeorder()
        Dim rayOrder() As String
        Dim neworder() As Long
        Dim L As Long
        Dim lngTot As Long
        On Error Resume Next
        lngTot = ActivePresentation.Slides.Count
        rayOrder = Split("5/3/2/1/4", "/")
        ReDim neworder(0 To UBound(rayOrder))
        For L = 0 To UBound(rayOrder)
            neworder(L) = ActivePresentation.Slides(CLng(rayOrder(L))).SlideID
        Next L
        For L = 0 To UBound(rayOrder)
            ActivePresentation.Slides.FindBySlideID(neworder(L)).MoveTo lngTot
        Next L
    End Sub
    Notice you DO need the comma between the text to split and the split character
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  9. #9
    Dear John,

    Thank you very much for your time and expertise. This works perfectly and I have definitely learned a lot here. Thank you for explaining this to me.

Tags for this Thread

Posting Permissions

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