How to Find Words with Special Characters and Change Font via Macro in Word
Question details
The user needs a method to identify complete words containing special characters within a document and apply a specific font (like EB Garamond) to the entire word, while also resolving an 'Invalid outside procedure' VBA error.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Formatting specific words throughout a document based on character patterns using wildcard searches and VBA automation.
- Observed behavior
- Standard wildcard searches only apply the font formatting to the matching special characters instead of the whole word containing them. When attempting to run a VBA script to solve this, an 'Invalid outside procedure' error occurs because the code lacks proper procedure declarations.
Ensure you have access to the Developer tab in your Word processor to use the VBA editor. It is always recommended to save a backup copy of your document before running new macros.
Create a VBA Macro Enclosed in a Sub Procedure
Use a properly structured VBA macro to loop through the document, find the specific special characters using wildcards, and apply the font change to the entire matched word.
A standard Word wildcard search is great for finding text patterns, but it limits formatting changes to the exact matched string. To format the entire word that contains a special character, you need a VBA macro.
If you encounter an 'Invalid outside procedure' error when running your script, it means the code was pasted directly into the module without being wrapped in a subroutine. All VBA executable code must reside between a 'Sub' and 'End Sub' declaration.
Press Alt + F11 to open the Visual Basic for Applications (VBA) editor. In the Project pane on the left, right-click your document name, select 'Insert', and click 'Module'.
At the top of the new module window, type 'Sub MyFindAndReplaceMacro()' and press Enter. The editor will automatically generate the 'End Sub' line for you.
Inside the procedure, add 'Selection.HomeKey wdStory' to ensure the search starts at the beginning of the document. Set up a 'Selection.Find' loop with '.MatchWildcards = True' to search for your special character pattern.
Within the active find loop, add the command 'Selection.Words(1).Font.Name = "EB Garamond"'. This ensures that the font change is applied to the first full word of the current selection, rather than just the matched characters.
Click anywhere inside your code block and press F5, or click the green Run button in the toolbar, to execute the macro and update the fonts in your document.

Use WPS Writer for Advanced Document Macros
WPS Writer provides robust developer tools with full VBA macro support, allowing you to easily run custom scripts for complex formatting tasks, such as finding special characters and changing word fonts.
- 1. Enable the Developer Tools: Open WPS Writer, navigate to the settings or ribbon customization, and ensure the 'Developer' tab is enabled.
- 2. Access the VBA Editor: Click on the Developer tab and select 'Macros', or press Alt + F11 to launch the built-in VBA editor.
- 3. Insert and Run Your Macro: Insert a new module, paste your Find and Replace macro securely inside a Sub procedure block, and click Run to format your document automatically.

Frequently Asked Questions
Why do I get an 'Invalid outside procedure' error in VBA?
This error is triggered when executable VBA code is pasted into a module but is not contained within a proper procedure block. You must ensure your code begins with a declaration like 'Sub MacroName()' and concludes with 'End Sub'.
Can I use standard Find and Replace instead of a macro to format the whole word?
While standard Find and Replace supports wildcards, it typically only highlights and formats the exact characters matched by the wildcard pattern. To expand the formatting to the entire word surrounding the special characters, a macro utilizing 'Selection.Words(1)' is required.
How do I enable wildcard searching in a VBA macro?
To enable wildcard searching in VBA, you must set the property '.MatchWildcards = True' within your 'Selection.Find' block. This tells the search engine to interpret characters like asterisks (*) or question marks (?) as wildcard expressions.




