logo
search
VBA & Macro Problems

How to Replace Text with a Custom Document Property Value Using VBA

Maira MehtabMaira Mehtab Sep 22, 2026 869 views

Question details

The user needs to replace specific text strings in a Word document with the actual value of a custom document property via a VBA macro, rather than just inserting the property's name.

Product
Word
Device & OS
not provided
Scenario
Automating document text replacement using a Word VBA macro based on pre-defined custom document properties.
Observed behavior
When attempting to replace text using VBA, users often struggle with syntax and end up either inserting the property name itself as text or encountering type mismatch errors during the replacement process.
Before you start

Ensure you have the Developer tab enabled in your word processor and have already created the custom document properties you intend to reference within your VBA macro.

Solution 1Recommended

Extract the Property Value as a String and Replace Text

Use this method to explicitly read the custom document property's value, convert it to a string, and pass it to the Find and Replace function.

When automating documents, directly referencing a custom property name in the replacement string might just insert the name itself. By explicitly requesting the .Value property and converting it to a string using the CStr() function, you ensure the actual data is successfully extracted and applied as text.

1
Open the VBA Editor

Press Alt + F11 to open the Visual Basic for Applications (VBA) editor in your word processor.

2
Declare your variables

Set up your string variables to hold the property name and the extracted value. For example: Dim strReplacement As String and Dim strPropertyName As String.

3
Extract the custom property value

Assign the property's value to your string variable using this exact syntax: strReplacement = CStr(ActiveDocument.CustomDocumentProperties(strPropertyName).Value).

4
Execute the Find and Replace

Use the Selection.Find.Execute or Range.Find.Execute method in your macro, and set the ReplaceWith parameter to your strReplacement variable.

Static Value vs. Dynamic Field: This method inserts the property value as static text. If the custom property value changes later, the text in the document will not update automatically.
Automate Documents with WPS Writer

Use WPS Writer to Run VBA Macros and Manage Document Properties

WPS Office provides robust support for VBA macros and custom document properties, allowing you to seamlessly automate text replacements just like in Microsoft Word. Its highly compatible environment ensures your existing macros run smoothly without needing major code adjustments.

  1. 1. Download and Install WPS Office: Get the latest version of WPS Office and ensure you have the VBA module installed and enabled.
  2. 2. Open your Macro-Enabled Document: Launch WPS Writer and open your .docm or .doc file containing the custom properties and scripts.
  3. 3. Access the Developer Tools: Navigate to the Developer tab on the ribbon and click on the 'VBA Editor' icon to access your code.
  4. 4. Run the Replacement Macro: Paste your custom property string replacement code into your module and run it to replace the text seamlessly.
Fully compatible with Microsoft Word VBA macros and .docm formats.Easily manage Custom Document Properties via the intuitive WPS Writer interface.Lightweight application that compiles and runs macros efficiently without lag.Familiar ribbon interface makes the transition seamless for VBA developers.
microsoft office alternative - wps office

Frequently Asked Questions

Why am I getting a 'Type Mismatch' error when reading a custom property in VBA?

This usually happens if the custom property is empty, contains a boolean/date, or isn't natively formatted as a string. Using the CStr() function explicitly converts the property's value into a string, which is strictly required for the replacement text parameter.

Can I update the replaced text automatically if the custom property changes later?

If you use the static Find and Replace method in VBA to insert the string value, the text will not update automatically. To make it dynamic, you must insert a DOCPROPERTY field via VBA instead of replacing it with static text.

Where do I create Custom Document Properties before running the macro?

You can create custom properties by going to the File menu, selecting Info or Properties, navigating to Advanced Properties, and adding a new property with a specific name, type, and value under the Custom tab.

Does the ActiveDocument.CustomDocumentProperties syntax work in WPS Office?

Yes, WPS Office's VBA environment is highly compatible with the standard Office Object Model. Standard VBA syntax like ActiveDocument.CustomDocumentProperties works identically in WPS Writer when managing document properties.