Skip to main content

Replace Text

Finds and replaces text in the Word document. Supports both single text replacement and multiple replacements using a dictionary. All occurrences of the search text are replaced throughout the entire document.

Common Properties

  • Name - The custom name of the node.
  • Color - The custom color of the node.
  • Delay Before (sec) - Waits in seconds before executing the node.
  • Delay After (sec) - Waits in seconds after executing the node.
  • Continue On Error - Automation will continue regardless of any error. The default value is false.
info

If ContinueOnError property is true, no error is caught when the project is executed even if Catch node is used.

Input

  • File Descriptor - File descriptor ID from the Open Word node. Default variable is word_fd.

  • Replacements - Dictionary of text to replace. Default variable name is replacements. When provided, this takes priority over the Text and New Text inputs. Example:

    {
    "old text": "new text",
    "Hello": "Hi",
    "{{NAME}}": "John Smith"
    }
  • Text - (Legacy) Single text to find and replace. Used only if Replacements is not provided.

  • New Text - (Legacy) Replacement text for the single Text input. Used only if Replacements is not provided.

Use Cases

  • Updating template placeholders with actual data
  • Batch replacing multiple terms in documents
  • Personalizing form letters or contracts
  • Updating product names or terminology
  • Replacing merge fields with real values
  • Correcting repeated typos or outdated information
  • Converting template variables to actual content

Example: Single Replacement (Legacy Method)

Simple find and replace:

  1. Use Open Word to open the document
  2. Add the Replace Text node
  3. Configure the inputs:
    • File Descriptor: word_fd
    • Text: {{CUSTOMER_NAME}}
    • New Text: John Smith

Replace multiple placeholders at once:

// Create replacements dictionary
let replacements = {
"{{CUSTOMER_NAME}}": "Jane Doe",
"{{COMPANY}}": "ACME Corporation",
"{{DATE}}": "December 23, 2024",
"{{AMOUNT}}": "$1,500.00",
"{{INVOICE_NUMBER}}": "INV-2024-001"
};

// Use Replace Text node
// File Descriptor: word_fd
// Replacements: replacements

Template Document Example

Original Template

Invoice

Date: {{DATE}}
Invoice Number: {{INVOICE_NUMBER}}

Bill To:
{{CUSTOMER_NAME}}
{{COMPANY}}

Amount Due: {{AMOUNT}}

After Replacement

Invoice

Date: December 23, 2024
Invoice Number: INV-2024-001

Bill To:
Jane Doe
ACME Corporation

Amount Due: $1,500.00

Contract Template Example

// Template placeholders
let contractData = {
"{{EMPLOYEE_NAME}}": "Robert Johnson",
"{{POSITION}}": "Senior Software Engineer",
"{{START_DATE}}": "January 15, 2025",
"{{SALARY}}": "$120,000",
"{{DEPARTMENT}}": "Engineering",
"{{MANAGER}}": "Sarah Williams"
};

// Apply all replacements in one operation

Email Template Personalization

// Personalize email for each recipient
let emailData = {
"{{FIRST_NAME}}": "Alice",
"{{PRODUCT}}": "Premium Subscription",
"{{DISCOUNT}}": "20%",
"{{EXPIRY_DATE}}": "December 31, 2024",
"{{SUPPORT_EMAIL}}": "support@company.com"
};
tip

Use distinctive placeholder patterns like {{PLACEHOLDER}} or [PLACEHOLDER] to avoid accidentally replacing regular text in your documents.

Advanced Replacement Scenarios

Conditional Replacements

// Replace based on conditions
let status = "approved";
let replacements = {
"{{STATUS}}": status,
"{{MESSAGE}}": status === "approved"
? "Your request has been approved."
: "Your request is pending review."
};

Data-Driven Replacements

// From database or API data
let customerData = {
name: "Michael Chen",
email: "mchen@example.com",
phone: "(555) 123-4567",
accountNumber: "ACC-98765"
};

let replacements = {
"{{NAME}}": customerData.name,
"{{EMAIL}}": customerData.email,
"{{PHONE}}": customerData.phone,
"{{ACCOUNT}}": customerData.accountNumber
};

Formatting Numbers and Dates

// Format data before replacement
let amount = 1234.56;
let date = new Date();

let replacements = {
"{{AMOUNT}}": "$" + amount.toFixed(2), // $1234.56
"{{DATE}}": date.toLocaleDateString(), // 12/23/2024
"{{PERCENTAGE}}": "15.5%",
"{{QUANTITY}}": "1,000 units"
};

Common Errors

  • "Invalid File Descriptor" - Ensure the Open Word node was executed first.
  • "Text cannot be empty" - When using legacy mode, provide the text to search for.
  • "Search text cannot be empty in the Replacements dictionary" - All keys in the replacements dictionary must be non-empty strings.

Notes

  • All occurrences of the search text are replaced (wdReplaceAll).
  • Replacements are case-sensitive by default.
  • When using the Replacements dictionary, all replacements are performed sequentially.
  • The search does not preserve formatting - only text content is replaced.
  • If multiple replacements reference the same search text, the last one takes effect.
  • Replacement works on the entire document, including headers, footers, and text boxes.

Best Practices

  • Use the Replacements dictionary for multiple replacements - it's more efficient.
  • Choose unique, identifiable placeholder patterns (e.g., {{FIELD}}).
  • Test replacements on a copy of your template first.
  • Keep a list of all placeholders used in your templates.
  • Consider using uppercase for placeholder names for better visibility.
  • Validate that all placeholders have been replaced after the operation.
  • For complex scenarios, perform replacements in a specific order.

Placeholder Pattern Recommendations

// Good placeholder patterns
"{{FIELD}}" // Double curly braces
"[FIELD]" // Square brackets
"##FIELD##" // Hash symbols
"${FIELD}" // Dollar brace (like template literals)
"<FIELD>" // Angle brackets

// Avoid these patterns (too common in regular text)
"FIELD" // No delimiters
"_FIELD_" // Underscores only
"field" // Lowercase, no delimiters

See Also