Powerpoint Random Name Selection VBA - 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"

Related

Identifying a word's and sentences

I need to produce code within Visual Basic that identify's a words position. For example, my sentence could write 'This is my Visual Basic Project'. If the user entered the word 'my', the output will open another form displaying 'Your word is in the 3rd position'. Its required to use strings then change it into an array.
My design for this project is fairly simple:
A TextBox for thee user to enter text.
A "Confirm" Button.
And an "Exit" Button.
Also, in the Form containing the output (when confirming that the button has been clicked) there is a Label (giving answer) and an "ok" Button to return you back to the main program. Finally, if their word is not within the sentence, an error message should appear. The program should be caps sensitive.
I am fairly new to programming and would love any help. I would appreciate it if you could return some code for the Buttons, TextBox's and a whole program.
Really please. Many thanks!!
You haven't specified a platform, so this answer is only for Office Development scenarios.
The Microsoft Word Object Model is useful here. You can loop through a Sentence object, looping through its Words collection. Loop through the Words collection until you get a match. So some code to run in Microsoft Word
Sub SetUpSentence()
Dim currentSelection As Word.Selection
Set currentSelection = Application.Selection
Application.Options.Overtype = False
currentSelection.TypeText Text:="This is some text"
End Sub
Sub TestFindWord()
Debug.Print FindWord("some")
End Sub
Function FindWord(ByVal sWord As String) As Long
Dim oWords As Word.Words
Set oWords = ActiveDocument.Words
Dim lIndex As Long
lIndex = 0
Dim oRange As Word.Range
For Each oRange In oWords
lIndex = lIndex + 1
If Trim(oRange.Text) = sWord Then
FindWord = lIndex
Exit Function
End If
Next oRange
End Function

Set Min/Max of Spin Button Form Control with VBA

I have a spin button on my worksheet (not in a userform), and I need to set the minimum and maximum values in VBA. Easy, right? I tried worksheetName.Shapes("shapeName").Min = x but I get Run-time error 438: Object doesn't support this property or method.
I used Excel's macro recorder and changed the min and max of the spin button, and it recorded the following:
ActiveSheet.Shapes("shapeName").Select
With Selection
.Min = x
.Max = y
End With
How is it that if I select the shape, I can then access its properties, but if I reference the shape directly I am unable to access the same properties? This does not make sense to me. Obviously, I would like to avoid selecting the shape and referencing "selection," as this generally is not best practice.
You can use the Shape.ControlFormat property:
Sub test()
Dim shp As Excel.Shape
Set shp = ActiveSheet.Shapes("Spinner 1")
With shp.ControlFormat
.Min = 2
.Max = 33
End With
End Sub
This answer will be helpful, although not particularly intuitive...
Excel-VBA: Getting the values from Form Controls
As I mention in the comments above, it is peculiar. The SpinButton is a member of the worksheet's Shapes collection, but it does not allow you to access those properties as a shape directly (see Doug's answer for how to do this another way which is probably better).
Try:
With ActiveSheet.Spinners("spinbutton1")
.Min = x
.Max = y
End With
Likewise, you can delcare a variable and iterate if you have multiple controls like this:
Dim spinbtn as Spinner 'or As Variant
For each spinbtn in ActiveSheet.Spinners
spinbtn.Min = x
spinbtn.Max = y
Next
Etc.
Simply you can try this:
Private Sub SpinButton1_SpinDown()
TextBox3.Text = val(TextBox3.Text) - 1
If TextBox3.Text < 0 Then TextBox3.Text = 0
End Sub

Generic Powerpoint Developer Controls Value Saving in VBA

I am looking to create a sort of Quiz using Powerpoint and I would like to save the input given by the user.
For example: If I ask the question: What is your favorite color? When they answer the question and click the next button, I would like to save the value of the textbox and append it to an output file.
I already know how to get the value and write the value to a file.
What I am looking to do is a sort of loop.
Here is the pseudo-code:
foreach(Control c in CurrentSlide.Controls)
{
File.Append(c.Value);
}
This way, no matter what controls are on the form, I want to save the value of each and every control.
Is this possible in VBA? If not, do you have any generic solutions for this situation?
Here's a more generic way of checking for each control on the slide, w/o knowing in advance how many there'll be. This assumes that you've kept the default names assigned to control shapes (TextBox1, CheckBox1 and so on). If you want to change those, you'll need to be sure that each TextBox has a name that includes at least one bit of string that's unique to it and change the code accordingly. Siddarth has already supplied code for writing to files and you said you have that under control so I'm just Debug.Printing the values here to keep the example simple.
Sub TestIt()
' Run this to test the shapes on slide 1
ProcessTheSlide ActivePresentation.Slides(1)
End Sub
Sub ProcessTheSlide(oSl As Slide)
Dim oSh As Shape
For Each oSh In oSl.Shapes
' Is it a control?
If oSh.Type = 12 Then ' msoOLEControlObject
On Error Resume Next
With oSh.OLEFormat.Object
If InStr(.Name, "TextBox") > 0 Then
Debug.Print .Text
End If
If InStr(.Name, "CheckBox") > 0 Then
Debug.Print .Value
End If
End With
End If
Next
End Sub
Is this possible in VBA?
Yes it is :)
On your presentation, Place 1 TextBox and two Command Buttons. Your presentation should look like this.
In the VBA Editor paste this code
Option Explicit
'~~> Save data to file
Private Sub CommandButton1_Click()
Dim filesize As Integer
Dim FlName As String
'~~> text File where you want to save the data
FlName = "C:\Sample.Txt"
'~~> Get a free file handle
filesize = FreeFile()
'~~> Open your file
Open FlName For Append As #filesize
'~~> Export Text
Print #filesize, TextBox1.Text
Close #filesize
TextBox1.Text = ""
End Sub
'~~> Exit Show
Private Sub CommandButton2_Click()
SlideShowWindows(1).View.Exit
End Sub
Now when you run it and click on "Next", the data will automatically be saved in the text file.
And this is how your text file will look...
HTH

How do I refer to a controls object, on a worksheet, using a variable name?

I have added a ListBox to a SHEET (not to a "UserForm")
I did this using the mouse.
I clicked the little Hammer and Wrench icon.
This ListBox seems to be easily referenced using code such as this:
ListBox1.Clear
or
ListBox1.AddItem("An option")
However, I have three of these ListBoxes (named, conveniently, ListBox1, ListBox2, and ListBox3) and I want to write a function to populate them with array data, like this:
Call populate_listbox(ListBox2, designAreaArray)
Where the first argument is the listbox name, the 2nd is the data.
But I do not know how to send "ListBox2" correctly, or refer to it correctly within the function.
For example:
Dim controlName as string
controlName = "ListBox1"
doesn't work, even if I define the function as follows:
Sub populate_listbox(LB As ListBox, dataArray As Variant)
Dim i As Integer: i = 0
For i = LBound(dataArray, 2) + 1 To UBound(dataArray, 2) ' Skip header row
LB.AddItem (dataArray(index, i))
Next i
End Sub
Clearly it results in a mis-matched data type error. I've tried defining "controlName" as a ListBox, but that didn't work either...
Though perhaps it is my reference to the listBox that is incorrect. I've seen SO MANY ways to refer to a control object...
MSForms.ListBox.
ME.ListBox
Forms.Controls.
Worksheet.Shapes.
The list goes on an on, and nothing has worked for me.
Try this:
Dim cMyListbox As MSForms.ListBox
Set cMyListbox = Sheet1.ListBox1 '// OR Worksheets("YourSheetName").Listbox1
cMyListbox.AddItem("An option")
Also you can populate a listbox without having to loop through the array, try this:
Dim cMyListbox As MSForms.ListBox
Dim vArray As Variant
Set cMyListbox = Sheet1.ListBox1
vArray = Range("A1:A6").Value
cMyListbox.List = vArray
Change the sub signature to match this:
Sub populate_listbox(LB As MSForms.ListBox, dataArray As Variant)
Now you can pass it like you were trying to originally.
NOTE: This only works if you used the "ActiveX" version of the listbox. I'm assuming you are because you are able to call ListBox1 straight from a module.
PS: The ActiveX controls are members off of the parent sheet object. So if you have the listbox1 on sheet1, you can also call it like Sheet1.ListBox1 so you don't get confused if you end up with multiple sheets with multiple listboxes. Also, you may want to change the name just to make it easier on yourself.
Dim controlName As OLEObject
Set controlName = Sheet1.OLEObjects("ListBox1")
Call populate_listbox(controlName, designAreaArray)
Sub populate_listbox(LB As OLEObject, dataArray As Variant)
Dim i As Integer: i = 0
For i = LBound(dataArray, 2) + 1 To UBound(dataArray, 2) ' Skip header row
LB.Object.AddItem (dataArray(Index, i))
Next i
End Sub
To access the state of a checkbox Active-X control on Sheet1:
Dim checkBox1 As Object
Set checkBox1 = Sheet1.OLEObjects("CheckBox1").Object
MsgBox checkBox1.Value

Making a macro to generate a custom show in Powerpoint

I want to make a macro for PowerPoint, to generate a custom show, containing all the slides from my PowerPoint but in random order.
How would I do this?
I want to be able to run it and create different custom shows each time.
It's been 3 years since I used PowerPoint, and the only experience I have with VB was a little bit of VB6 in 2004.
Check out the info here.
Sample:
Sub sort_rand()
Dim i As Integer
Dim myvalue As Integer
Dim islides As Integer
islides = ActivePresentation.Slides.Count
For i = 1 To ActivePresentation.Slides.Count
myvalue = Int((i * Rnd) + 1)
ActiveWindow.ViewType = ppViewSlideSorter
ActivePresentation.Slides(myvalue).Select
ActiveWindow.Selection.Cut
ActivePresentation.Slides(islides - 1).Select
ActiveWindow.View.Paste
Next
End Sub