Automatically Size a Text Box with User Input

The following example demonstrates the effects of the AutoSize property with a single-line TextBox and a multiline TextBox. The user can enter text into either of the TextBox controls and turn AutoSize on or off independently of the contents of the TextBox. This code sample also uses the Text property.

To use this example, copy this sample code to the Script Editor of a form. To run the code you need to open the form so the Open event will activate. Make sure that the form contains:

  • Two TextBox controls named TextBox1 and TextBox2.

  • A ToggleButton named ToggleButton1.

Dim ToggleButton1 
Dim TextBox1 
Dim TextBox2 
 
Sub Item_Open() 
 Set ToggleButton1 = Item.GetInspector.ModifiedFormPages("P.2").ToggleButton1 
 Set TextBox1 = Item.GetInspector.ModifiedFormPages("P.2").TextBox1 
 Set TextBox2 = Item.GetInspector.ModifiedFormPages("P.2").TextBox2 
 
 TextBox1.Text = "Single-line TextBox. Type your text here." 
 
 TextBox2.MultiLine = True 
 TextBox2.Text = "Multi-line TextBox. Type your text here. Use SHIFT+ENTER to start a new line." 
 
 ToggleButton1.Value = True 
 ToggleButton1.Caption = "AutoSize On" 
 TextBox1.AutoSize = True 
 TextBox2.AutoSize = True 
End Sub 
 
Sub ToggleButton1_Click() 
 If ToggleButton1.Value = True Then 
 ToggleButton1.Caption = "AutoSize On" 
 TextBox1.AutoSize = True 
 TextBox2.AutoSize = True 
 Else 
 ToggleButton1.Caption = "AutoSize Off" 
 TextBox1.AutoSize = False 
 TextBox2.AutoSize = False 
 End If 
End Sub

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.