PDA

View Full Version : inserting multiple images in a slide



harisyam
10-10-2014, 12:38 AM
Hello All
I've written a vba script to insert 4 images in slides from a folder. Now the problem is it's working fine with .jpg, but not working for .png., I'm wondering why this is happening.
The error which i get after running the code is "object required".
I've posted the code below.
I also want the images to be appearing in the same order as they were in the folder. Please help me with that

thank you
Code:


Sub CreatePictureSlides()
Dim presentation
Dim layout
Dim slide
Dim FSO ' code by me to fill images with names in the slides
Dim folder
Dim file
Dim folderName
Dim i As Integer
' Set this to point at the folder you wish to import JPGs from
' Note: make sure this ends with a backslash \
folderName = "C:\Users\hamanda\Desktop\images\"
i = 1
' Delete all slides and setup variables
Set presentation = Application.ActivePresentation
If presentation.Slides.Count > 0 Then
presentation.Slides.Range.Delete
End If

Set layout = Application.ActivePresentation.SlideMaster.CustomLayouts(1)
Set FSO = CreateObject("Scripting.FileSystemObject")
' Retrieve the folder's file listing and process each file
Set folder = FSO.GetFolder(folderName)
For Each file In folder.Files
' Filter to only process PNG images
If LCase(Mid(file.Name, Len(file.Name) - 3, 4)) = ".png" Then
If i Mod 4 = 1 Then
Set slide = presentation.Slides.AddSlide(presentation.Slides.Count + 1, layout)
While slide.Shapes.Count > 0
slide.Shapes(1).Delete
Wend
Set img = slide.Shapes.AddPicture(folderName + file.Name, False, True, 200, 200)
With img
.Left = 0
.Top = 0
.Height = 300
.Width = 300
End With
ElseIf i Mod 4 = 2 Then
Set img = slide.Shapes.AddPicture(folderName + file.Name, False, True, 200, 200)
With img
.Left = 301
.Top = 0
.Height = 300
.Width = 300
End With
ElseIf i Mod 4 = 3 Then
Set img = slide.Shapes.AddPicture(folderName + file.Name, False, True, 200, 200)
With img
.Left = 0
.Top = 301
.Height = 250
.Width = 250
End With
Else
Set img = slide.Shapes.AddPicture(folderName + file.Name, False, True, 200, 200)
With img
.Left = 300
.Top = 301
.Height = 250
.Width = 250
End With
End If

End If
i = i + 1
Next
End Sub