RandomGerman
06-05-2015, 09:36 AM
Hi there,
I'm trying to copy some shapes with a macro from a presentation somewhere on the computer. An example could be a collection of maps and flags. So, e.g., on slide one I have the United States. 50 shapes (for each state),50 textboxes (their names) and one picture (the flag). And on slide 2 it is Germany. 16 shapes, 16 textboxes and 1 picture. So you never know, how many shapes you have to copy - the code should be flexible.
Parts of my code are already working. I have added comments and questions into the code, where things do not work the way I want them to.
Any help is appreciated.
Thank you,
RG
Sub CopyTest()
Dim src As Presentation
Dim trg As Slide
Dim shp As Shape
'Open the source presentation - this works fine.
Set Target = Application.Presentations.Open("C:\Users\Chef\Desktop\CopyTest.pptx")
Set src = Presentations("CopyTest.pptx")
'Select all shapes on the slide.
src.Slides(1).Shapes.SelectAll
'This works fine, as long as I try to select all on the first slide.
'When I changed Slides(1) to Slides(2), debugging said, for selecting the slide has to be active.
'How can I choose, which one is active?
'The next aspect, where I'm only close to my goal, is copying.
'I need to copy the selection, but the following is wrong,
'as ShapeRange(1) only copies one shape.
'How can I copy the whole selection?
Set shp = ActiveWindow.Selection.ShapeRange(1)
shp.Copy
'Close the source presentation - works well
With Application.Presentations("CopyTest.pptx")
.Close
End With
Set trg = ActiveWindow.View.Slide
'And finally paste it onto the active slide, my target.
'Seems to work, but the shapes lose their formatting.
'Is there a way to keep it with paste special?
'The copied shapes could be all kinds of objects:
'shapes, textboxes, groups, pictures, charts ...
trg.Shapes.Paste
End Sub
I'm trying to copy some shapes with a macro from a presentation somewhere on the computer. An example could be a collection of maps and flags. So, e.g., on slide one I have the United States. 50 shapes (for each state),50 textboxes (their names) and one picture (the flag). And on slide 2 it is Germany. 16 shapes, 16 textboxes and 1 picture. So you never know, how many shapes you have to copy - the code should be flexible.
Parts of my code are already working. I have added comments and questions into the code, where things do not work the way I want them to.
Any help is appreciated.
Thank you,
RG
Sub CopyTest()
Dim src As Presentation
Dim trg As Slide
Dim shp As Shape
'Open the source presentation - this works fine.
Set Target = Application.Presentations.Open("C:\Users\Chef\Desktop\CopyTest.pptx")
Set src = Presentations("CopyTest.pptx")
'Select all shapes on the slide.
src.Slides(1).Shapes.SelectAll
'This works fine, as long as I try to select all on the first slide.
'When I changed Slides(1) to Slides(2), debugging said, for selecting the slide has to be active.
'How can I choose, which one is active?
'The next aspect, where I'm only close to my goal, is copying.
'I need to copy the selection, but the following is wrong,
'as ShapeRange(1) only copies one shape.
'How can I copy the whole selection?
Set shp = ActiveWindow.Selection.ShapeRange(1)
shp.Copy
'Close the source presentation - works well
With Application.Presentations("CopyTest.pptx")
.Close
End With
Set trg = ActiveWindow.View.Slide
'And finally paste it onto the active slide, my target.
'Seems to work, but the shapes lose their formatting.
'Is there a way to keep it with paste special?
'The copied shapes could be all kinds of objects:
'shapes, textboxes, groups, pictures, charts ...
trg.Shapes.Paste
End Sub