Share via

Powerpoint VBA Random Image Insert

Anonymous
2018-12-23T16:06:32+00:00

Hi all, 

I was wondering if anyone has any experience in using a VBA code/macro to insert a random image from a designated folder upon running the macro. I'm hoping to get something where every time I run the macro, it will select and insert one of a set of images from a folder on my desktop, and randomize with replacement each time. 

I've found some samples that get close, but none of them have quite gotten me there.

Here's a sample code I found that I think is somewhat close to my goals.

If anyone has any ideas, I would really appreciate the help!

Thank you!

Sample Code:

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

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

7 answers

Sort by: Most helpful
  1. John Korchok 232.8K Reputation points Volunteer Moderator
    2018-12-24T20:10:57+00:00

    Here's the best I could come up with. Unfortunately, Apple's sandboxing requirements make this a pain. Here's a macro-enabled presentation with the macro and action button already set up: InsertRandomImage.pptm

    All the photos are in the same folder as the presentation and are number 1.jpg, 2.jpg, etc. I use 12 images for testing, so the macro uses the number 12.

    Sub InsertRandomImage()

      Randomize

      RanNum% = Int(12 * Rnd) + 1

      Path$ = ActivePresentation.Path

      FullFileName$ = Path$ + "/" + CStr(RanNum%) + ".jpg"

      ActivePresentation.Slides(1).Shapes.AddPicture(FileName:=FullFileName$, LinkToFile:=msoTrue, SaveWithDocument:=msoTrue, Left:=300, Top:=150, Width:=200).Select

    End Sub

    Change the 12  in Int(12* Rnd) to the actual number of photos.

    Here's the macOS pain in the butt part: 

    1. When you open the presentation, PowerPoint asks if you want to Enable Macros (You do).
    2. Then when you click on the macro button, PowerPoint asks Do you want to run the macro InsertRandomImage. Click on Run Macro.
    3. Then PowerPoint displays a Grant File Access box with the file name. You have to click Select...
    4. Then yet another dialog opens showing the list of files with the selected file highlighted. You have to click on Grant Access.
    5. Finally the photo appears on the slide.

    Sadly, there is no way around these dialogs.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2018-12-23T19:38:37+00:00

    I see - that's very helpful, thank you!

    The goal is for a university class that uses the socratic method and already has a desktop folder with a picture of each students face to be able to run the macro with a click on each slide, and have it randomly insert one of the student pictures onto the slide. It's a fun gimmick that makes the socratic method a bit less terrifying. Functionally, it's just a way to get a true random draw from a folder of images and have it inserted without needing to exit the presentation.

    If this is too complicated to be worth the minimal payoff - understood!

    Thank you again!

    Was this answer helpful?

    0 comments No comments
  3. John Korchok 232.8K Reputation points Volunteer Moderator
    2018-12-23T19:33:36+00:00

    Your code is never going to work on a Mac. This is because the script tries to attach a picture to an ActiveX control, but ActiveX controls are not supported on Mac.

    It's much easier to develop code in PowerPoint for Windows, then test and debug it on a Mac. The VBA editor in Windows has a helpful AutoComplete utility. When I try to declare Dim FS as, then I see a dropdown list of acceptable object types, and FileSearch is not one of them. Application.FileSearch was added in Office 97, but removed in Office 2007.

    You'll also have a problem with .FileName = Array("*.jpg", "*.bmp"), since the macOS doesn't include wildcard characters. Likewise, .LookIn = "C:\pictures" is not going to work on a Mac, path names are constructed differently.

    What problem are you trying to solve with this macro? Perhaps there's a different way to do it.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2018-12-23T18:58:19+00:00

    I get a "compile error: invalid use of New keyword" at Set FS = New FileSearch when I use this exact code

    Was this answer helpful?

    0 comments No comments
  5. John Korchok 232.8K Reputation points Volunteer Moderator
    2018-12-23T18:40:32+00:00

    Please let us know the details of what problems you're having with the existing code. Error messages? Images not random enough?

    Was this answer helpful?

    0 comments No comments