简体   繁体   中英

Place text from user input textbox into textbox on a different slide

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

As i understand you just want to modify previous code. For that just below $username = textBox1.value write down:

yourtextBoxname.text = textBox1.text

PowerPoint VBA

I have a similar question and would really appreciate any help.

I am trying to take the value in a textbox (Textbox1 Slide 4) that the user will input and output it into an equation and display it in a textbox on another slide (Textbox1 Slide5).

So the users input can range from 1 to 100. I need to convert it into a decimal and into the following equation:

(Users Input - 1)^2

If the user types 40 into Textbox1 Slide4, I would like the textbox on slide 5 show 0.36

(0.40 - 1)^2 = 0.36

Any help would be so greatly appreciated. Thank you!

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