'How to make a random image generator that creates 2 images VBA

I'm doing a final project in action researh.I want to create a game that can improve students' speaking skills.

I have a PowerPoint slide with different images. I need to create VBA code in PowerPoint that pressing a button or clicking an object will randomly output two images. But we don't know VBA very well and have this so far from finding things online. It creates one image but I want to know how to make it display two at a time.

Sub RandomImage()

  Randomize

  RanNum% = Int(57 * Rnd) + 1

  Path$ = ActivePresentation.Path

  FullFileName$ = Path$ + "/" + CStr(RanNum%) + ".png"
  ActivePresentation.Slides(1).Shapes.AddPicture(FileName:=FullFileName$, LinkToFile:=msoTrue, SaveWithDocument:=msoTrue, Left:=100, Top:=100, Width:=500).Select

End Sub


Solution 1:[1]

You can use a for-loop.

You will have to think about a logic for the left-Parameter so that both pictures occure next to each other - please see my suggestion in the code

Sub RandomImage()

Dim i As Long
Dim posLeft As Long
  
For i = 1 To 2

    Randomize
  
    RanNum% = Int(57 * Rnd) + 1
  
    Path$ = ActivePresentation.Path
  
    FullFileName$ = Path$ + "/" + CStr(RanNum%) + ".png"
  
    posLeft = 100 + ((i-1) * 510)   '-->>> you will have to adjust this to your needs
    
    ActivePresentation.Slides(1).Shapes.AddPicture(FileName:=FullFileName$, LinkToFile:=msoTrue, SaveWithDocument:=msoTrue, _
        Left:=posLeft, Top:=100, Width:=500).Select
Next

End Sub

Solution 2:[2]

All of you guys, I really thank you. I'cant remove ".SELECT " at the end of the line. So I added Insert | Action and choose Run Macro: RandomImage but it only runs one image at a time. So I added the subs on this site (docs.microsoft.com/en-us/office/vba/api/…) . but randomly only get one picture at a time as before. I don't know where it's wrong. I am new to VBA and trying to learn. Thank you.

Sub Shape_Click()
    Set myShape = ActivePresentation.Slides(1).Shapes(1)
    myShape.ActionSettings(ppMouseClick).Action = ppActionLastSlide
    myShape.ActionSettings(ppMouseOver).SoundEffect.Name = "applause"
End Sub

Sub RandomImage()
     Dim i As Long
     Dim posLeft As Long
     For i = 1 To 2
     Randomize
     RanNum% = Int(57 * Rnd) + 1
     Path$ = ActivePresentation.Path
     FullFileName$ = Path$ + "/" + CStr(RanNum%) + ".png"
     posLeft = 100 + ((i - 1) * 510)
     ActivePresentation.Slides(1).Shapes.AddPicture(FileName:=FullFileName$, 
     LinkToFile:=msoTrue, SaveWithDocument:=msoTrue, Left:=posLeft, Top:=100, 
     Width:=500).Select
Next
End Sub

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ike
Solution 2 Siriwan Polpangkwa