VBA & Macro Problems

How to Create a Word VBA Style Selection Macro with a ComboBox

Natalie TaylorNatalie Taylor Oct 10, 2026 872 views

Question details

The user needs to build a custom UserForm in VBA that uses a ComboBox to list available character styles for selection, making the chosen style available to other macros.

How to Create a Word VBA Style Selection Macro with a ComboBox
Product
Microsoft Word
Device & OS
not provided
Scenario
Creating a custom macro interface to let users select a specific character style from a drop-down list rather than typing its name manually, thereby avoiding spelling mistakes.
Observed behavior
The goal is to have a functional UserForm that populates its ComboBox exclusively with document character styles, passing the selected value into a public variable for subsequent macro operations.
Before you start

Ensure that the Developer tab is enabled on your ribbon so you can access the Visual Basic for Applications (VBA) editor to create your UserForm.

Solution 1Recommended

Build a VBA UserForm with a ComboBox

Create a custom dialog box that dynamically loads the document's character styles into a dropdown menu and stores the selection.

Using a UserForm ensures that users only select from existing styles, eliminating typos. By declaring a public variable in a standard module, the selected style name remains in memory and can be accessed by any other macro running in the document.

1
Set up the Public Variable

Press Alt + F11 to open the VBA Editor. Go to Insert > Module to create a standard module. At the very top, type 'Public SelectedStyle As String' to store the chosen style name globally.

2
Create the UserForm

Click Insert > UserForm. Use the Toolbox to drag a ComboBox (named ComboBox1) and a CommandButton (named CommandButton1) onto the newly created form.

3
Add Initialization Code to Populate Styles

Double-click the UserForm background to open its code module. Use the 'UserForm_Initialize' event to loop through 'ActiveDocument.Styles'. Add an 'If' statement to check if 'Style.Type = wdStyleTypeCharacter', and if true, use 'ComboBox1.AddItem Style.NameLocal' to add it to the list.

4
Store Selection on Button Click

Double-click the CommandButton. In its Click event, add the code 'SelectedStyle = ComboBox1.Value' followed by 'Unload Me'. This saves the user's choice to the public variable and closes the form.

Build a VBA UserForm with a ComboBox
Save as Macro-Enabled: Remember to save your document as a Macro-Enabled Document (*.docm) so your VBA code and UserForm are preserved for future use.

Automate Formatting Tasks with WPS Office

WPS Office provides excellent built-in support for macros and VBA editing, allowing you to easily build automated UserForms and style selectors directly within WPS Writer.

  1. 1. Enable Developer Tools: Open WPS Writer and navigate to the Developer tab on the top ribbon.
  2. 2. Launch the VBA Editor: Click the 'VB Editor' or 'Macros' button to open the integrated development environment.
  3. 3. Insert Your Code: Insert your UserForm and Modules, then paste your style-selection VBA code just as you would normally.
  4. 4. Run Your Macro: Save your document as a macro-enabled format and run your custom ComboBox script instantly.
Seamlessly run standard VBA scripts and macros in WPS Writer.Highly compatible with Microsoft Word DOCX and DOCM formats.Lightweight architecture ensures the macro editor launches instantly.Clean, intuitive interface for managing developer tools.
microsoft office alternative - wps office

Frequently Asked Questions

How do I filter only character styles in my VBA ComboBox?

When looping through the ActiveDocument.Styles collection, wrap your ComboBox1.AddItem command inside an If statement that checks `If Style.Type = wdStyleTypeCharacter Then`. This prevents paragraph or table styles from appearing in your list.

Can I show the actual font formatting in the VBA ComboBox?

By default, a standard VBA ComboBox only displays plain text. Showing actual visual formatting (like bold or custom fonts) requires complex API calls or custom-drawn controls, which is generally not recommended for simple macro tasks.

Why should I use a public variable to store the style name?

A public variable declared in a Standard Module remains stored in memory even after the UserForm is unloaded. This allows subsequent macros or procedures to access the user's selected style seamlessly without keeping the form active.

What happens if a macro tries to use a style that doesn't exist?

If your macro attempts to apply a style name that cannot be found in the document, VBA will generate an 'Item not found' runtime error. Using a ComboBox populated directly from the document's active styles prevents this by ensuring only valid styles are selected.