logo
search
Others

How to Execute a VBA Command Stored in a String in Word

Rana GarciaRana Garcia Sep 25, 2026 871 views

Question details

The user wants to dynamically build a fully formed Word VBA command as a text string and execute it during runtime.

How to Execute a VBA Command Stored in a String in Word
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.
Before you start

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.

Solution 1Recommended

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.

1
Define a reusable formatting procedure

Create a Sub or Function that accepts a Word Range and specific formatting parameters (e.g., Boolean variables for Bold and Italic).

2
Apply explicit formatting properties

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).

3
Pass dynamically determined arguments

In your main macro, evaluate your conditions and pass the required formatting arguments to your new procedure rather than building a string command.

Use Explicit VBA Logic Instead of String Execution
Security and Stability: Executing arbitrary strings as code is generally a security risk. Using explicit logic prevents malicious code injection and simplifies debugging.

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. 1. Install WPS Office: Download and install WPS Office, then open your Word document in WPS Writer.
  2. 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. 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. 4. Execute using eval(): Use the native JavaScript eval(yourStringCommand) function to dynamically execute your formatting instructions directly.
Supports modern JavaScript (JSA) with native eval() capabilities for string execution.Fully compatible with Microsoft Word documents (.docx, .docm) and formatting.Lightweight, highly secure, and easier to debug than traditional VBA.Free to download and use for daily document processing and automation.
microsoft office alternative - wps office

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.