web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Replacing text in the ...
Power Automate
Answered

Replacing text in the header of multiple Word documents

(0) ShareShare
ReportReport
Posted on by 101

I'm trying to find and replace specific text in multiple Word documents (in a folder). To do that I'd have thought looping through the list of files and applying an appropriate action would be possible.

 

The newly released "Find and replace words in Word document" looks as if it should do the job, but unfortunately it doesn't appear to work with text contained in a header. Am I missing something?

 

I've tried to take a different approach and focus on the Word window in order to send a CTRL + H (to bring up the Replace dialog), but I receive a "Window wasn't found" message. What am I doing wrong?

PAD.Designer_winRGca3vY.png

I have the same question (0)
  • MichaelAnnis Profile Picture
    5,727 Moderator on at

    If you are sure the name convention is correct, I would apply a wait. 

    wait for window to exist (window name)

    focus window. 

     

    alternatively, I would strongly recommend some VBA or office scripts for this. GPT could probably write it in a heartbeat. 

    good luck. 

  • WillSG Profile Picture
    352 Moderator on at

    Hi @Spinner  I hope you are doing well.

     

    This is an interesting new use case, the Word Actions are new, so they (MS) will need improvement down the road.

     

    However, you can use the Write to Word Document Action

     

    This action allows you to write before or after a Bookmark on Word.

     

    So, here are the steps you need to follow:

     

    1. Open Excel and double click in the header of your Word Template file and then click Insert.
    2. Now, click the option Bookmark (in the header you don’t need to select nothing)
    3. Use the Bookmark Name Header.
    4. Press the bottom Add.

     

    Here is the snipped code:

     

    Word.WriteToWord.WriteStartOfBookmark Instance: WordInstance Text: $'''This is a new test''' AppendNewLine: False Bookmark: $'''Header'''

     

     

    Please let me know if it works, happy automation!,


    If I have addressed your inquiry successfully, kindly consider marking my response as the preferred solution. If you found my assistance helpful, a 'Thumbs Up' would be greatly appreciated.

     

    Additionally, I offer specialized consultancy and development services leveraging PAD. If you're interested in exploring these services further, feel free to DM me, and we can initiate a discussion.

     

    Kind regards,

     

    Will SG

    Managing Director & Automation Lead

    RAMS CR (Recruitment & Automation)

    LinkedIn Profile

     

    WillSG_Screenshot 2024-02-12 122909.png

  • Spinner Profile Picture
    101 on at

    Thank you both for your replies.

     

    @MichaelAnnis  To be honest I'm not sure I have the correct name convention. I've asked it to focus on the window with title CurrentItem.Name* where CurrentItem is one item in a list of Files. I'd have thought that would be enough to locate a Word window where the title is [filename].docx - Word. If I'm wrong please let me know.

     

    @WillSG  Doesn't that both require manually inserting a bookmark into each document? Also, doesn't that insert rather than replace?

  • MichaelAnnis Profile Picture
    5,727 Moderator on at

    You can find the name by doing the following.  The name of an open file is the same every time.

    In the focus window, when you choose window mode: by title and/or class name, when you hit the dropdown for Window Title, it will show a list of all the open windows.

    MichaelAnnis_0-1707920936900.png

     

    Mine ended up being a format of "%CurrentItem.NameWithoutExtension% - Word" (no quotes).

     

    Wildcards can be used, so %CurrentItem.NameWithoutExtension%* should be good as well, and this should work for any file regardless of extension, and not having to put the " - [Program]" at the end. 

     

    So, maybe you can try either of these naming conventions, or simply open one, select it for window title, and fill in the variables as needed to match.

     

    Good luck!

     

  • Verified answer
    Spinner Profile Picture
    101 on at

    Thanks @MichaelAnnis .

    As it happens your latest message came in while I was composing another reply. After reading another thread you posted on and experimenting further I found that the issue with locating the window was linked to Power PX being enabled. Once I re-created the flow without Power FX, I was able to find and focus on the Word window.

     

    My issue now relates to send the right combination of keys to perform the find and replace. This combination works when entered manually, but not when part of an action.

     

    {Control}({H})
    {Alt}({N})
    DATE
    {Alt}({I})
    FILENAME
    {Alt}({A})

     

    The action opens the Find and Replace box and enters the find and replace terms, but then opens some sort of Word split pane at the bottom (a specialised header viewer?), and doesn't activate the Replace All button. It seems instead to be activating the Find Next button.

     

    WINWORD_3cyr4XPu8R.png

     

    It may be important to note that I'm searching for words that form part of Word fields. (I'm using DATE and FILENAME as simplified examples of fields; they aren't the actual target and replacement fields that I want to use, but the same applies to these simpler examples.)

     

    Any ideas?

  • Spinner Profile Picture
    101 on at

    Further update: I think I've cracked it. Despite the fact Power Automate Desktop lists Enter as an example of a special key that can be sent (either as {Enter} or {Return}, depending on which part you believe), it seems that it will also interpret line breaks as Enters. So the line breaks in my above series of simulate key presses were triggering the default action on the window, which is of course Find Next.

     

    Removing them and putting all the keys in one unbroken single line caused Word to interpret the commands as desired.

  • MichaelAnnis Profile Picture
    5,727 Moderator on at

    @Spinner 

    Warning / Urgent:

     

    You cannot replace a Word Merge Field with find and replace on the surface of the word document.  It changes the display, but does not actually change the underlying Word Merge Field.

    This is my example:
    The <<Fund>> gets changed to <<Year-End>> by finding Fund and replacing with year_end.

    MichaelAnnis_0-1707928922323.png

    MichaelAnnis_1-1707928962607.png

    But, when I use "Preview Results", I am still getting the Fund Code (not my Year_end date):

    MichaelAnnis_2-1707929017919.png

    As soon as I uncheck preview results, the display goes right back to what it was before...because I never actually changed the underlying merge field.

    MichaelAnnis_3-1707929157756.png


    So, let's fix this.

    Before you do your find/replace, send keys for {Alt}({F9}).  This will make the Mergefields appear in an editable format:

    MichaelAnnis_4-1707929228161.png

    Now, you can do find and replace.  Per my example, I recommend using:
    find:  MERGEFIELD Fund
    replace:  MERGEFIELD Year_End

    This also stops you from accidentally picking up anything else that might have the same text elsewhere in the document.  The result is still in that editable format, but we have actually now changed the underlying field.

    MichaelAnnis_5-1707929391715.png

    Once all Replace actions have been taken, send keys {Alt}({F9}) again to set the Mergefields back to display mode and not in edit mode.

    MichaelAnnis_6-1707929486581.png

    Finally, I tested with Preview Results to ensure this flow works:

    MichaelAnnis_7-1707929524109.png


    I hope this helps,

     

    Mike

  • Spinner Profile Picture
    101 on at

    @MichaelAnnis

    Thanks Michael, this is good advice. As it happens, though, I was already working with editable fields (i.e. having pressed Alt + F9). The fields I was updating weren't Merge fields (MERGEFIELD) but more generic Word fields (in this case, I was replacing LINK fields with INCLUDETEXT fields).

     

    Appreciate the extensive guide, though, and your help throughout.

  • MichaelAnnis Profile Picture
    5,727 Moderator on at

    Glad to hear it, sorry for my confusion.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 519 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 296 Moderator

#3
abm abm Profile Picture

abm abm 232 Most Valuable Professional

Last 30 days Overall leaderboard