INvoke Code to replace editabl regions text in restricted word document

Hi i have a word dcoument which is restricted, with some regions for editing.
But i tried the below invoke to find the editable fields to replace the text, and it doesnt seems to replace.
Originally using the 2nd code for replace, but it doesnt work at all.

image

For Each storyRange As Microsoft.Office.Interop.Word.Range In odoc.StoryRanges
Console.writeline(storyRange.Editors.Count.toSTRing)
If storyRange.Editors.Count > 0 Then
With storyRange.Find
.Text = “clientName”
.Replacement.Text = dict.ToString
.Forward = True
.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue
.Execute(Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
End With
End If
Next

oDoc.Content.Find.Execute(FindText:=“clientName”, ReplaceWith:=“ANABSD” , Replace:= Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)

Hi @TyraS

Might be the text is inside a protected region and the find execute cannot modify locked content, so you need to directly access the editable ranges such as content controls or bookmarks and set their range text instead of relying on a global find replace.

Happy Automation

When a Word document is protected, Find/Replace doesn’t work on locked regions, even if your code loops through ranges.

If you want to do it still, you must unprotect the document in code before replacing. Once replaced you can protect it back.

the “clientName” is not locked, it is editable, if manual, still can replace. but using invoke code not able to.

the “clientName” is not locked, it is editable, if manual, still can replace. but using invoke code not able to.

Is your clientName inside a content control, table cell, header/footer, comment, textbox/shape, or is Track Changes enabled?

hi yes, in a table cell

As long as the table cell is part of an editable region, you can replace text.

Instead of code, Use UiPath Word Activities.

Activities - Replace Text in Document

Using Word Application Scope, open your Word file, Then Inside the scope, add Replace Text activity.
Set Find Text = “clientName”
Set Replace With = “your value”
If its find text in mutiple places, you have option to replace all as well, just enable toggle button if required.

Then UiPath will replace the text accordingly.