PDA

View Full Version : [SOLVED:] Copy slides to new presentation in a specific order



spillthebean
03-14-2014, 07:48 AM
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.

John Wilson
03-14-2014, 09:06 AM
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

spillthebean
03-14-2014, 09:27 AM
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.

John Wilson
03-14-2014, 10:14 AM
What do you want the final order to be?

eg

8,4,5,3,2,1,6,7

spillthebean
03-14-2014, 10:33 AM
Yes, that's right, I know what position each slide needs to be and the new order is like in your example

John Wilson
03-14-2014, 10:36 AM
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

spillthebean
03-14-2014, 10:50 AM
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?

John Wilson
03-14-2014, 11:52 AM
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

spillthebean
03-17-2014, 08:40 AM
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.