Replace Text inside Text Box in Word DOcument

Hi All,

Currently i make automation to generate document letter using word.
For this case i’m using activity Easy Word Activtiy from BalaReva.

I found some issue when using activtiy ‘Find and Replace’ didn’t affect text inside Shapes (Text Box)

Template Document
image

test.docx (15.8 KB)

Sample Output Document
image

Did anyone have solution for this case?

Thank You

@aliaga

Why not use the official word activity…as the same functionality is already present?

Cheers

Hi @Anil_G ,

The reason why i didn’t use official word activity it because impact template in my document.

Thankyou.

Hi @aliaga ,

You can also replace using powershell code; simply invoke powershell and specify the path to it.

# Define paths to your files

$wordTemplatePath = "C:\Users\Laptop_User\Desktop\Word Replace\Testtmp.docx"

# Text replacements (array of hash tables with 'Find' and 'Replace' values)

$replacements = @(

    @{ Find = "[Name1]"; Replace = "vinit" },

    @{ Find = "[Name]"; Replace = "mhatre" }

)


# Open the Word document

$word = New-Object -ComObject Word.Application

$doc = $word.Documents.Open($wordTemplatePath)

# Iterate through each replacement and perform the text replacement
foreach ($replacement in $replacements) {

    $findText = $replacement.Find

    $replaceText = $replacement.Replace

    $findReplace = $doc.Content.Find

    $findReplace.Execute($findText, $null, $null, $null, $null, $null, $null, $null, $null, $replaceText, 2)

}

#save doc

$doc.Save()

$doc.Close()

$word.Quit()

Regards,
Vinit Mhatre

@aliaga

Can you give a sample file and code here

Cheers

Hi @Anil_G ,

Attached sample code :
Text Box Document.zip (81.2 KB)

Thank You