1

The Situation: I am running a macro to import graphics from a folder to PowerPoint, 4 per slide in a custom format, using a macro I found here.

The Issue: The order the pictures are imported is not done by the name of the file. How could I import these photos in that order? My files are named as Chart 1, Chart 2, etc.

The Code:

Sub InsertQuadFormat()

  Dim presentation
  Dim layout
  Dim slide
  Dim FSO
  Dim folder
  Dim file
  Dim folderName
  Dim i As Integer

  'Change the folder as per your needs
  folderName = "C\"
  i = 1

  Set presentation = Application.ActivePresentation
  If presentation.Slides.Count > 0 Then
      presentation.Slides.Range.Delete
  End If

  Set layout = Application.ActivePresentation.SlideMaster.CustomLayouts(3)
  Set FSO = CreateObject("Scripting.FileSystemObject")
  Set folder = FSO.GetFolder(folderName)

  ' loop though each image in the folder

  For Each file In folder.Files

      If LCase(Mid(file.Name, Len(file.Name) - 3, 4)) = ".png" Then

          If i Mod 4 = 1 Then
              ' For 1,5,9 .... images
              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 = 15
                  .Top = 70
                  .Height = 460
                  .Width = 460
              End With

          ElseIf i Mod 4 = 2 Then
              ' For 2,6,10 .... images

              Set img = slide.Shapes.AddPicture(folderName + file.Name, False, True, 200, 200)
              With img
                  .Left = 484
                  .Top = 70
                  .Height = 460
                  .Width = 460
              End With

          ElseIf i Mod 4 = 3 Then
              ' For 3,7,11 .... images

              Set img = slide.Shapes.AddPicture(folderName + file.Name, False, True, 200, 200)
              With img
                  .Left = 15
                  .Top = 296
                  .Height = 460
                  .Width = 460
              End With
          Else
              ' For 4,8,12 .... images

              Set img = slide.Shapes.AddPicture(folderName + file.Name, False, True, 200, 200)
              With img
                  .Left = 484
                  .Top = 296
                  .Height = 460
                  .Width = 460
              End With
          End If
      End If
      i = i + 1
  Next

End Sub
shA.t
  • 16,580
  • 5
  • 54
  • 111

1 Answers1

0

Don't do the work inside the "For Each file In folder.Files" loop. Instead, use that loop to fill an array with the names of files in the folder, sort the array, then loop through the array to add the images.

Steve Rindsberg
  • 14,442
  • 1
  • 29
  • 34
  • I have never worked with arrays before, but I can see that an array would allow the files to be pulled in the order I want, but how would I use that loop to put the files into an array? And would I use the same loop to pull out of the array? – Hunter Bonham Jul 01 '15 at 13:11
  • Very broadly, you'd put files into the array in the loop where you're now picking up and processing each file. Then another loop from LBound(arrayname) to UBound(arrayname) would let you process the files one at a time; you'd put your existing code within that loop. You'll want to read up a bit on Dynamic vs Static arrays; Dynamic is generally a better choice for this, but if you can be certain that you'll never have more than X files to process, you can Dim your array out to X elements and later ignore any elements that contain blanks. Less flexible, but simpler to get working. – Steve Rindsberg Jul 01 '15 at 14:42