How to Replace Text with a Custom Document Property Value Using VBA
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.
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.
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.
Press Alt + F11 to open the Visual Basic for Applications (VBA) editor in your word processor.
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.
Assign the property's value to your string variable using this exact syntax: strReplacement = CStr(ActiveDocument.CustomDocumentProperties(strPropertyName).Value).
Use the Selection.Find.Execute or Range.Find.Execute method in your macro, and set the ReplaceWith parameter to your strReplacement variable.
Insert a Dynamic DOCPROPERTY Field via VBA
If you need the inserted text to update automatically whenever the custom property changes, insert a dynamic field instead of performing a static text replacement.
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. Download and Install WPS Office: Get the latest version of WPS Office and ensure you have the VBA module installed and enabled.
- 2. Open your Macro-Enabled Document: Launch WPS Writer and open your .docm or .doc file containing the custom properties and scripts.
- 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. Run the Replacement Macro: Paste your custom property string replacement code into your module and run it to replace the text seamlessly.

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.




