How to Execute a VBA Command Stored in a String in Word
Question details
The user wants to dynamically build a fully formed Word VBA command as a text string and execute it during runtime.

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- Trying to dynamically apply text formatting (like Bold or Italic) to specific target strings found during a macro operation.
- Observed behavior
- Word VBA lacks a general-purpose Eval function to parse and execute arbitrary VBA statements stored as strings, and Application.Run only calls existing procedures.
Ensure your document is saved as a Macro-Enabled Document (.docm) before testing new scripts, and back up your text to prevent accidental formatting losses during macro execution.
Use Explicit VBA Logic Instead of String Execution
Since Word VBA cannot directly evaluate string commands, mapping parameters to explicit formatting properties is the safest and most standard approach.
Instead of constructing executable code as text, you should create a procedure that dynamically applies formatting based on variable states. This prevents execution errors and maintains the security of your macro.
Create a Sub or Function that accepts a Word Range and specific formatting parameters (e.g., Boolean variables for Bold and Italic).
Use a Select Case or If...Then block inside the procedure to map the passed variables to the actual formatting properties (e.g., Range.Font.Bold = True).
In your main macro, evaluate your conditions and pass the required formatting arguments to your new procedure rather than building a string command.

Apply Formatting Using VBA Find and Replace
If the goal is simply to change formatting for specific words, utilizing the built-in Find object is more efficient than building string commands.
Dynamically Execute String Commands with WPS JS Macros
Unlike traditional Word VBA, WPS Office natively supports modern JavaScript Macros (JSA). Since JavaScript natively includes the eval() function, you can dynamically build strings and execute them as code effortlessly while maintaining full compatibility with Microsoft Word formats.
- 1. Install WPS Office: Download and install WPS Office, then open your Word document in WPS Writer.
- 2. Open the JS Macro Editor: Navigate to the 'Tools' tab on the ribbon and select 'Macro' > 'JS Macro Editor' to launch the modern development environment.
- 3. Write your JS Macro: Write your script using standard JavaScript syntax to programmatically build your command string based on your document's text.
- 4. Execute using eval(): Use the native JavaScript eval(yourStringCommand) function to dynamically execute your formatting instructions directly.

Frequently Asked Questions
Why doesn't Application.Run work for executing arbitrary string commands in Word VBA?
Application.Run is designed strictly to call predefined macros or procedures by their name. It does not contain a compiler or parser to interpret and execute arbitrary VBA syntax or statements on the fly.
Can I dynamically create a macro as a string and execute it in VBA?
While it is technically possible to programmatically write code to the VBProject module using VBA Extensibility and then call it, this method is heavily restricted by macro security settings ('Trust access to the VBA project object model') and is generally not recommended for regular document formatting tasks.
What is the best alternative to Eval for formatting in Word?
The most robust alternative is passing parameters to a predefined formatting procedure. You can evaluate the conditions dynamically in your code and pass variables (such as isBold = True) to a function that applies the formatting using standard Word object properties.
Does WPS Office support macros and dynamic execution?
Yes, WPS Office fully supports traditional VBA macros as well as modern JavaScript macros (JSA). The JavaScript Macro environment offers modern programming conveniences, including the native eval() function, making string evaluation straightforward.




