Home > other >  How to make a random image generator that creates 2 images VBA
How to make a random image generator that creates 2 images VBA

Time:05-09

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

CodePudding user response:

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
  • Related