简体   繁体   中英

Powerpoint Random Name Selection VBA

I have the following VBA code in Powerpoint 2010 to pick a name from a list at random:

Dim hat As New Collection

Sub fill_the_hat()
Dim items() As String
Dim x As Long
items = Split("Test\Names\John\Bob\Chris\Mike\Robert\Adam", "\")
For x = 0 To UBound(items)
hat.Add(items(x))
Next x
End Sub

Sub pick_one()
Dim x As Long
Randomize
x = Int(Rnd * hat.Count) + 1
MsgBox hat(x)
hat.Remove (x)
End Sub

I need to adapt this to output to a text box rather than a MsgBox but this doesn't seem as obvious as I thought it would be?

Any help would be greatly appreciated,

Many Thanks, Josh

You will have to adress your UserForm and TextBox directly to change the contents. An example would be:

UserForm1.TextBox1.Text = x

This has to happen before the UserForm1.Show call or alternatively you have to refresh the form via UserForm1.Repaint

If you are just displaying the name there is no need at all to use an ActivX textbox. Just use a normal shape or textbox

ActivePresenation.Slides(1).Shapes ("nameofshape").Textframe.TextRange=hat(x)

Also when you use my code from the net it is more normal to say "I got this code from John Wilson's article here" rather that "I have this code"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM