logo
search
Office Settings & Configuration

How to Format a 10-Digit Phone Number in Word Content Control using VBA

Maira MehtabMaira Mehtab Oct 10, 2026 869 views

Question details

The user needs to validate a 10-digit phone number entered into a plain-text content control in Word and automatically format it as (123) 456-7890 using a VBA macro.

How to Format a 10-Digit Phone Number in Word Content Control using VBA
Product
Word
Device & OS
not provided
Scenario
Creating a structured form or document where user input in a content control must be validated and strictly formatted as a standard 10-digit US phone number.
Observed behavior
The macro fails to execute properly due to variable declaration errors (inline assignments), incorrect object references (using CCtrl instead of ContentControl), and the macros are lost because the document is not saved in a macro-enabled format.
Before you start

Ensure the Developer tab is enabled in your word processor to access Content Controls and the Visual Basic Editor, and be prepared to save your file as a Macro-Enabled Document (.docm) to prevent code loss.

Solution 1Recommended

Correct VBA Syntax and Save as a Macro-Enabled Document

Fix the object references and variable declarations in your VBA macro, and save the document correctly to ensure your automation works and is retained.

VBA does not support inline variable initialization. You must declare variables on one line and assign their values on the next. Additionally, ensure you reference the correct 'ContentControl' object parameter provided by the event, rather than an undefined variable.

1
Open the Visual Basic Editor

Press Alt + F11 to open the VBA Editor and locate the Document_ContentControlOnExit event code in your ThisDocument module.

2
Fix Variable Declarations

Separate your Dim statements from assignments. Write 'Dim phoneNumber As String' on one line, and 'phoneNumber = ContentControl.Range.Text' on the following line.

3
Filter and Validate Input

Write a loop or use Regular Expressions to remove any non-numeric characters from the input string. Verify that the remaining string length is exactly 10 digits.

4
Format and Update Content Control

If exactly 10 digits remain, reformat the string and write it back to the control using 'ContentControl.Range.Text = "(" & Left(cleanPhoneNumber, 3) & ") " & Mid(cleanPhoneNumber, 4, 3) & "-" & Right(cleanPhoneNumber, 4)'.

5
Save as .docm

Go to File > Save As, and change the file format to 'Word Macro-Enabled Document (*.docm)'. Saving as a standard .docx will permanently delete your macro.

Correct VBA Syntax and Save as a Macro-Enabled Document
Correct Parameter Usage: Always use the ContentControl parameter passed directly into the Document_ContentControlOnExit event instead of inventing custom object names like CCtrl.

Create and Manage Macro-Enabled Documents with WPS Writer

WPS Office provides robust tools for document editing, including support for form fields and VBA macros (in supported versions). You can easily open, edit, and save macro-enabled documents with high compatibility.

  1. 1. Download WPS Office: Download and install WPS Office Free from the official website.
  2. 2. Open Your Document: Open your existing .docm file or create a new document in WPS Writer.
  3. 3. Enable the Developer Tab: Navigate to the settings to enable the Developer tab so you can access advanced form controls.
  4. 4. Configure Content Controls: Add plain-text content controls and use the built-in macro editor (in VBA-supported versions) to write your validation scripts.
  5. 5. Save Document Correctly: Save your document in the .docm format from the File menu to ensure your macros and configurations remain intact.
Fully compatible with Microsoft Word .docm and .docx formatsComprehensive text formatting and form control featuresLightweight application with a familiar, easy-to-use interfaceFree to download and use for everyday document processing
microsoft office alternative - wps office

Frequently Asked Questions

Why do I get a syntax error when declaring variables in my Word VBA macro?

VBA does not support inline variable initialization. You cannot declare and assign a value on the same line (e.g., Dim x As String = "text"). You must separate them into two lines: declare with 'Dim x As String' followed by the assignment 'x = "text"'.

Why do my macros disappear after saving and reopening the Word document?

If you save your document in the standard .docx format, all VBA code is automatically stripped out for security reasons. You must save the file specifically as a Word Macro-Enabled Document (.docm) to retain your VBA macros.

How do I extract or update text in a Content Control using VBA?

You can access or modify the text inside a Content Control by referencing its Range.Text property. During the Document_ContentControlOnExit event, use 'ContentControl.Range.Text' to get the current string or assign a newly formatted string back to it.