VBA & Macro Problems

How to Automatically Select All Text in a Word VBA UserForm Text Box

Bushra ParveenBushra Parveen Nov 13, 2026 869 views

Question details

The user needs a Word VBA UserForm text box to automatically select existing text when it receives focus and apply default values or final formatting when focus leaves, with formatting triggered by Tab and click-away actions.

How to Automatically Select All Text in a Word VBA UserForm Text Box
Product
WPS Writer VBA UserForm
Device & OS
not provided
Scenario
Creating or editing a Word VBA UserForm where a text box should select all text on focus and format or validate the value on exit.
Observed behavior
Zero-padding and N/A default values do not apply until the user clicks again; pressing Tab does not trigger the formatting after leaving the text box.
Before you start

Open the VBA editor in WPS Writer, display the UserForm in design view, and confirm the text box control name (for example txtPromptText) before editing its event procedures.

Solution 1Recommended

Select all text with the Enter event in the UserForm text box

Use this when the text box already contains a value and you want a click or Tab into the field to highlight the entire existing value for fast replacement.

The Enter event runs before the text box gets focus in a Word VBA UserForm. Setting SelStart to 0 and SelLength to the full text length selects the existing value without changing the stored Value/Text.

1
Open the UserForm code module

In WPS Writer, press Alt+F11 to open the VBA editor, find your UserForm in the Project Explorer, and double-click the text box control (for example txtPromptText) to open its code module.

2
Add the Enter event handler

Insert the following event procedure above or below your existing code: Private Sub txtPromptText_Enter() With Me.txtPromptText .SelStart = 0 .SelLength = Len(.Text) End With End Sub

3
Save and test the focus behavior

Save the VBA project, run the UserForm, and click or tab into the text box. The existing text should be fully selected and replaced as soon as you type.

Select all text with the Enter event in the UserForm text box
Text box values are selected on focus: The Enter event solution leaves the underlying Value unchanged until the user types, avoiding accidental data loss.
WPS Writer VBA support

Automate Word VBA UserForm Text Boxes in WPS Writer

Use WPS Writer's VBA editor to add Enter and Exit events that select all text on focus and apply default values or zero-padding when the user leaves the field. WPS Writer supports Word-compatible VBA UserForms, so you can implement the same automation without changing applications.

  1. 1. Download and install WPS Office: Get WPS Office from the official download page and choose the version for your Windows or Mac device.
  2. 2. Open the macro-enabled Word document: Launch WPS Writer and open the .docm or .dotm file that contains the UserForm.
  3. 3. Open the VBA editor: Press Alt+F11 to open the VBA editor and locate the UserForm to edit its text box events.
  4. 4. Add Enter and Exit procedures: Insert text box Enter and Exit event code to select all text on focus and apply default values or padding when the field is exited.
  5. 5. Run the UserForm and test: Run the macro or UserForm, click or tab into the field, type a value, and press Tab to verify formatting.
Runs Word VBA UserForm Enter and Exit events for text box selection and formattingSupports .docm and .dotm macro-enabled files for Microsoft Word compatibilityFree, lightweight Office suite with a familiar VBA IDE and UserForm controlsSeamless migration from Microsoft Word without retrainingApplies default values and zero-padding reliably with the Exit event
microsoft office alternative - wps office

Frequently Asked Questions

Why doesn't the zero-padding happen until I click again in a Word VBA UserForm text box?

Usually the formatting is placed in an event that only runs after focus leaves the field. Put zero-padding in the text box Exit event, and make sure another tab-stop control is enabled so pressing Tab moves focus and triggers the event.

How do I make a UserForm text box select its existing value when I tab into it?

Add an Enter event for the text box and set SelStart = 0 and SelLength = Len(TextBox.Value). Open the VBA editor with Alt+F11, select the text box in the UserForm code module, and insert the event procedure.

Should I format the text box value in the Change event?

No. The Change event fires on every keystroke and makes it difficult to type partial values such as two-digit numbers. Use the Exit event for final formatting and the Enter event for focus selection, then run the UserForm to test Tab and click-away behavior.

How do I set a default value like N/A when a UserForm text box is left blank?

In the text box Exit event, check if the Value is an empty string and assign "N/A": If Me.TextBoxName.Value = "" Then Me.TextBoxName.Value = "N/A". This runs when the focus moves to another control.

Does WPS Writer support Word VBA UserForm events?

Yes. WPS Writer includes a VBA editor and supports Word-compatible UserForm controls and events such as Enter, GotFocus, and Exit, so you can use the same event procedures in macro-enabled documents.