使用 VBA 在幻灯片中显示随机数

Display random number in slide using VBA

我需要生成一个介于 1 到 30 之间的随机数并将其显示在每张幻灯片上。我在网上找到了以下代码:

Sub UpdateRandomNumber(oSh As Shape)
    Dim X As Long
    'Make the shape’s text a random number
    'X or less
    'Change 12 below to any number you’d like:
    X = 30
 
    oSh.TextFrame.TextRange.Text = CStr(Random(X))
End Sub
 
Function Random(High As Long) As Long
    'Generates a random number less than or equal to
    'the value passed in High
    Randomize
    Random = Int((High * Rnd) + 1)
End Function

Sub RandomNumber()

End Sub

我需要代码以不同的方式做一件事:
提示操作的对象在所有幻灯片上都位于同一位置。生成和显示随机数时,所有幻灯片都应相应更改。
当我离开幻灯片时,应该显示之前生成的数字,而不是之前在此幻灯片上生成的数字。

这将创建一个介于 1 和 30 之间的随机数。将形状名称更改为文件中使用的实际形状:

Sub ShapeNumber()
    Dim X As Long
    Dim ShapeNumber As String
    Dim oSlide As Slide
    Dim oShape As Shape

    X = 30
    Randomize
    ShapeNumber = Int((X * Rnd) + 1)
    For Each oSlide In ActivePresentation.Slides
        For Each oShape In oSlide.Shapes
            If oShape.Name = "Rectangle 3" Then
                oShape.TextFrame.TextRange.Text = ShapeNumber
            End If
        Next oShape
    Next oSlide
End Sub