How do I get the text from a user input in a userform to populate a text box on another slide?
My code for the UserForm1 submit button:
Private Sub CommandButton1_Click()
UserName$ = TextBox1.Value
Unload UserForm1
ActivePresentation.SlideShowWindow.View.Next
End Sub
I believe that the value typed by the user in the GUI is stored in the variable UserName$
.
I tried using label boxes and text boxes.
There is so much to unpack in your question. Microsoft has spent the least amount of effort in exposing PowerPoints Object model through VBA. While using VBA in Access, Excel, Outlook and Word, you can create some amazing solutions; PowerPoint, not so much.
First as ashleedawg suggested, you should add Option Explicit to every Module, Class, and Form Code. You can set this as the default by clicking on Tools (on the menu), selecting Options, and then checking Require Variable Declaration.
Now create a Module, by clicking Insert from the menu, then select Module. This will create a new module. In the module, enter Public UserName As String
. This will now enable the variable UserName to be accessible anywhere throughout the solution. It's a Global variable if you will. I usually call a module that contains my gobals modGobals
.
Option Explicit
Public UserName As String
Now for your form code. When you create a text box on a slide, the default name begins with TextBox followed by a space and a number. You're going to want to rename the text box. To do this, go to the slide view. On the Ribbon, make sure the File section is showing. At the far right, you'll see an area called Editing with Find, Replace, Select. Click on Select. From the drop down menu that appears, click on "Selection Pane..." The selection pane will appear, and you'll see all the objects on the slide. When you click on different slide objects, they will be highlighted in the selection pane.
Now that you've renamed your textbox to something like tbUserName, you can update your code as shown below.
Option Explicit
Private pvSlide As Slide
Private pvShp As Shape
Private Sub CommandButton1_Click()
UserName = TextBox1.Value
'Assume that slide index 2 contains the text box you want to update
Set pvSlide = ActivePresentation.Slides(2)
'set the shape you're going to update
Set pvShp = pvSlide.Shapes("tbUserName")
'This is the fun part -- traversing the object tree to get to the
'right property that so we can update the text box's value
pvShp.TextFrame2.TextRange.Text = UserName
Unload UserForm1
'I'm not sure why you're executing this method. So I commented it out
'ActivePresentation.SlideShowWindow.View.Next
End Sub
When you run the user form, enter text into the textbox and press the command button, the text you enter will show up in the text box on slide 2.
Good luck and happy coding