PDA

View Full Version : Macro for inserting multiple images



Sunseeker
07-11-2012, 09:29 AM
Hi

I'm using Powerpoint 2007 and I want to create a macro that will use the Metro design with a 2 content format. I have hundreds of photos and for each page I want it to import the image on the left hand box. When I do this individually, the image is sized 12.57 cm x 8.38 cm which is fine.

However, I'd also like the macro to automatically format each image and add a solid white line border to each.

Finally, for the title on each slide I would like it to automatically insert Screenshot 1, Screenshot 2 etc for each slide.

I really appreciate your help

Many thanks

John Wilson
07-11-2012, 12:03 PM
Are the images all in one folder?
Are they all jpegs?

Sunseeker
07-11-2012, 12:08 PM
Yes they are all in one folder but they are .png images.

Thanks

John Wilson
07-11-2012, 12:31 PM
You could try this maybe:

It assumes you have a presentation open with one blank slide in the Metro design and that slide is two content layout.

Might need a few tweaks as it's top of head code.

Obviously change the folder path

Sub insertPics()


Dim strFolder As String ' Full path to folder
Dim strName As String
Dim oPres As Presentation
Dim osld As Slide
Dim ocust As CustomLayout
Dim x As Long
' Edit this:
strFolder = "C:\Users\John\Desktop\Pics\" 'note the last \

Set oPres = ActivePresentation
Set osld = oPres.Slides(oPres.Slides.Count)
Set ocust = osld.CustomLayout

strName = Dir$(strFolder & "*.PNG")
While strName <> ""
x = x + 1
With osld.Shapes.AddPicture(strFolder & strName, msoFalse, msoTrue, -1, -1, -1, -1)
.Line.Visible = True
.Line.ForeColor.RGB = vbWhite
End With
osld.Shapes.Title.TextFrame.TextRange = "Screenshot " & x
strName = Dir()
If strName <> "" Then
Set osld = oPres.Slides.AddSlide(oPres.Slides.Count + 1, ocust)
ActiveWindow.View.GotoSlide osld.SlideIndex
End If
Wend

End Sub

Sunseeker
07-12-2012, 02:32 AM
That pretty much works. Many thanks :thumb

Just one thing, when you run the macro, it inserts the image in the top left hand corner of the screen. Is there any way you could fix it, so that the image is lined up on the left hand side, next to the "Click to add text" box on the right hand side.

When I insert the image manually by clicking the "Insert picture from file" icon, it lines up perfectly. If it could do this, that would be great.

Many thanks for your help.

John Wilson
07-12-2012, 03:53 AM
Ah 2007 act differently to 2010 (where the code works)

Try this (make sure "Shapes(3)" is the left hand shape.

Sub insertPics()


Dim strFolder As String ' Full path to folder
Dim strName As String
Dim oPres As Presentation
Dim osld As Slide
Dim ocust As CustomLayout
Dim x As Long
' Edit this:
strFolder = "C:\Users\John\Desktop\Pics\" 'note the last \

Set oPres = ActivePresentation
Set osld = oPres.Slides(oPres.Slides.Count)
Set ocust = osld.CustomLayout

strName = Dir$(strFolder & "*.PNG")
While strName <> ""
x = x + 1
With osld.Shapes.AddPicture(strFolder & strName, msoFalse, msoTrue, -1, -1, -1, -1)
.Cut
End With
With osld.Shapes(3)
.Select
End With
ActiveWindow.View.Paste
With osld.Shapes(3).Line
.Visible = True
.ForeColor.RGB = vbWhite
End With
osld.Shapes.Title.TextFrame.TextRange = "Screenshot " & x
strName = Dir()
If strName <> "" Then
Set osld = oPres.Slides.AddSlide(oPres.Slides.Count + 1, ocust)
ActiveWindow.View.GotoSlide osld.SlideIndex
End If
Wend

End Sub

Sunseeker
07-12-2012, 05:33 AM
Works perfectly!

Many thanks once again
:beerchug:

John Wilson
07-12-2012, 06:44 AM
There you go!

2010 automatically inserts the picture into the first available suitable placeholder. (To be honest I hate that)

2007 doesn't do this so you need to cut the picture and force it to paste into the placeholder.

fhimage
06-20-2013, 09:10 PM
i have tried the tutorial ,coo stuff