Share via

Random image from folder

Anonymous
2011-10-14T07:45:51+00:00

Hi,

I would like to insert random pictures into my powerpoint.

So the left side of my slides will be showing static content (words) and the right side will be a randomly selected picture from a folder on my computer.

I found this threadthat says it can be done using macros. Can someone please teach how it can be done?

Thanks!

Microsoft 365 and Office | PowerPoint | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Andreas Killer 144.1K Reputation points Volunteer Moderator
2011-10-14T15:19:26+00:00

This thread talk about to use macros to apply some settings for the picture, not to load a random picture.

Make a new presentation and add an image control to the first slide and save it in the same directory with your pictures.

With Office 2007 we must establish some code first to locate the pictures. Download this file, open the VBA editor, press CTRL-M and import it. This creates a class module FileSearch in your project.

http://dl.dropbox.com/u/35239054/FileSearch.cls

Then add a normal module to your project and paste in the code below. Every time you call this macro the pictures within the image control changes. Note: You must close the presentation before you add or remove pictures in the directory, otherwise the code may cause errors.

Andreas.

Option Explicit

Sub LoadRandomPicture()

  Dim S As Shape

  Static FS As FileSearch

  'Visit all Shapes

  For Each S In ActivePresentation.Slides(1).Shapes

    'Is it an OLEobject?

    If S.Type = msoOLEControlObject Then

      'Is it our image?

      If TypeOf S.OLEFormat.Object Is MSForms.Image Then

        'Yes, search for the files if not already done

        If FS Is Nothing Then

          'Initialize FileSearch

          Set FS = New FileSearch

          With FS

            'Search for loadable pictures

            .FileName = Array("*.jpg", "*.bmp")

            'In this path

            .LookIn = "C:\pictures"

            'And it's subfolders

            .SearchSubFolders = True

            'Do it

            If .Execute = 0 Then

              MsgBox "No pictures found in """ & .LookIn & """, abort."

              Exit Sub

            End If

          End With

        End If

        'Select a random picture

        S.OLEFormat.Object.Picture = LoadPicture(FS.FoundFiles((Rnd * FS.FoundFiles.Count) + 1))

      End If

    End If

  Next

End Sub

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Steve Rindsberg 99,166 Reputation points MVP Volunteer Moderator
    2011-10-14T15:17:33+00:00

    You might want to ask the same question in the PowerPoint section of Answers.  Some add'l info to provide when you do that:

    Do you want the pictures to be randomly selected each time you run the presentation or do you simply want a way to create a presentation with random pic, but that will stay the same from then on?

    Are all the pictures the same size/proportion or are you willing to modify them so that they are?

    Was this answer helpful?

    0 comments No comments