Word VBA Macro to Convert Underscores to Italic and Asterisks to Bold
Question details
Automatically convert text enclosed in underscores to italic and text enclosed in double asterisks to bold using a Word VBA macro.

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- A document contains lightweight formatting markers such as _italic text_ and **bold text** that need to become actual Word formatting.
- Observed behavior
- The desired result is italic or bold text with the surrounding underscore or asterisk markers removed.
Use desktop Word with VBA support; Word for the web cannot run this macro. Test on a copy containing representative marker pairs, and save as a Word Macro-Enabled Document (*.docm) if you want to retain the macro.
Convert paired markers with a Word VBA wildcard macro
Use this macro to process the main document text in two passes: underscores become italic, and double asterisks become bold.
Wildcard Find captures the text inside each marker pair. Replacing the match with capture group 1 removes the markers, while replacement font settings apply the requested formatting.
The patterns below do not match across paragraph breaks. They are intended for simple paired markers, not full Markdown parsing.
In desktop Word, open the target document, then choose Developer > Visual Basic. If Developer is hidden, enable it in Word's ribbon settings. In the VBA editor, select the document project and choose Insert > Module.
Paste this code into the module: Sub ConvertTextMarkers() Dim previousUpdating As Boolean previousUpdating = Application.ScreenUpdating On Error GoTo Failed Application.ScreenUpdating = False ReplaceMarkedText "_([!_^13]@)_", True ReplaceMarkedText "\*\*([!*^13]@)\*\*", False Application.ScreenUpdating = previousUpdating Exit Sub Failed: Application.ScreenUpdating = previousUpdating MsgBox "Conversion stopped: " & Err.Description, vbExclamation End Sub Private Sub ReplaceMarkedText(ByVal pattern As String, ByVal makeItalic As Boolean) Dim target As Range Set target = ActiveDocument.Content With target.Find .ClearFormatting .Replacement.ClearFormatting .Text = pattern .Replacement.Text = "\1" .Forward = True .Wrap = wdFindStop .Format = True .MatchCase = False .MatchWholeWord = False .MatchWildcards = True .MatchSoundsLike = False .MatchAllWordForms = False If makeItalic Then .Replacement.Font.Italic = True Else .Replacement.Font.Bold = True End If .Execute Replace:=wdReplaceAll End With End Sub
Return to Word and choose Developer > Macros. Select ConvertTextMarkers and click Run. The macro processes the active document's main text, not just the selected text.
Inspect examples such as _sample text_ and **sample text**. Confirm that the markers are gone and the text is italic or bold. Also inspect any literal underscores or asterisks, since paired markers in ordinary text can be converted unintentionally.

Edit and format Word documents with WPS Writer
WPS Office is a free, lightweight alternative with a familiar interface and Word-format compatibility for an easy transition. In WPS Writer, you can apply bold or italic through Home > Font and remove text markers manually. It does not resolve Word VBA issues, and support for this macro depends on the WPS edition and environment.

Frequently Asked Questions
Can I run this macro in Word for the web?
No. Word for the web cannot run VBA macros. Open the document in desktop Word, then use Developer > Visual Basic to add the code and Developer > Macros to run it.
Why does the macro use \1 as the replacement text?
With Word wildcard matching enabled, parentheses create a capture group. The replacement text \1 inserts the first captured group, which is the text inside the markers, without the surrounding underscores or asterisks.
Does the macro process only the text I select?
No. It uses ActiveDocument.Content, so it processes the main document text. To restrict it to a selection, change Set target = ActiveDocument.Content to Set target = Selection.Range.Duplicate in the helper procedure, then select the desired text before running it.
Will existing bold or italic formatting be removed?
The macro only sets italic for underscore matches and bold for double-asterisk matches; it does not explicitly turn off the other font attribute. Check representative passages after running it, especially text that already contains mixed formatting.
Why are some marker pairs left unchanged?
The underscore pattern excludes internal underscores, and the double-asterisk pattern excludes internal asterisks. Both patterns exclude paragraph breaks. Inspect unchanged text for those characters, missing closing markers, or content in headers, footnotes, or text boxes outside the main document range.




