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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Replace a text directl...
Power Automate
Answered

Replace a text directly in a word doc

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hi,

 

I have to find the text in the word doc and replace it with the new version value. Is there any option in PAD to perform the task?

I believe we can do it for the text file, but I need to replace it directly in the word doc. Please help.

 

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

    Inside of word, use “send keys” to use the hot keys to navigate the menu to find/replace, and enter what you need to at that screen. Best of luck!

  • VJR Profile Picture
    7,635 on at

    Hi @Anonymous 

     

    2 options as below (not in any order).

     

    1.  Via a macro

     

    - Write the below macro in an excel file

    - Call the macro via "Run Excel macro" from PAD

    - Has the ability to pass parameters from PAD to Excel macro

    - Refer comments in bold below

     

    Dim WordApp As Word.Application  'Go to Tools menu -> References -> select Microsoft Word x.0 Object Library
    Dim WordDoc As Word.Document
    Dim WordContent As Word.Range

    dim varWordTemplateToUse as string

    varWordTemplateToUse= "Full Path of Word document here" 'Can also be passed as parameter from PAD

    Set WordApp = CreateObject("Word.Application")
    WordApp.Visible = True
    Set WordDoc = WordApp.Documents.Open(varWordTemplateToUse)
    Set WordContent = WordDoc.Content

    '- FIND & REPLACE
    With WordContent.Find
    .Forward = True
    .ClearFormatting
    .MatchCase = False
    .Wrap = wdFindContinue
    .Text = ""                             'either pass a variable without quotes or directly the text to search
    .Replacement.Text = ""       'either pass a variable without quotes or directly the text to replace
    .Execute Replace:=wdReplaceAll
    End With

     

    WordDoc.Save
    WordDoc.Close
    WordApp.Quit

    Set WordDoc = Nothing
    Set WordApp = Nothing

     

    PS: I did not use the code box here since it does not allow bold formatting.

     

    2. via Find & Replace

    Per suggestion from @MichaelAnnis do a sendkeys on Control H to bring up the Find & Replace dialog box.

     

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    I tried both the options. I tried using the excel macros and I am getting the macro security warning, seems I need to contact IT to rectify it.

     

    I tried using the Ctrl +H after the word doc is opened but could not able to see the find/replace box.

    Radhika1_0-1637776744655.png

    Did I create the flow right? and also I need to find and replace more values. 

     hope I can pass the values as a list for finding and replacing, right?

  • VJR Profile Picture
    7,635 on at

    @Anonymous ,

     

    - If you are allowed to enable macros by yourself then see if the options are available for you.

    https://www.ablebits.com/office-addins-blog/2020/03/11/enable-disable-macros-excel/

     

    If macros are not allowed for you then writing a similar code in a .vbs script file (lots of them available on the web) and calling them from PAD will do the same.

     

    - Wait stage in between opened up the Replace box

    This is the command I used in Sendkeys

     

    {Control}({H})

     

    VJR_1-1637810159911.png

     

     

    Sendkeys is not going to be straightforward but you can try and see if it works out with your requirements.

     

     

    PS: I see in your screenshot that you are opening a .docm file which means it is already a macro enabled word document. So if you are able to write a macro within that document itself this requirement will work for you.

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    I tried writing a macro within the word document providing the path to the excel to fetch the find and replace text. It is working fine. After it finishes it process, when I try to open the excel mentioned in the macro enabled word doc, it says that "the excel is open in another application. Please close it and try again". It is so annoying and not able to delete the excel too, because for the next time, I need to modify the values in the excel. 

     

    I closed the word doc but still receives the message.

     

    Could you able to figure out the reason for this? 

  • VJR Profile Picture
    7,635 on at

    @Anonymous 

    I tried writing a macro within the word document providing the path to the excel

    It is not clear to me if the macro is written in Word then what is the Excel used for.

    Please share details with screenshots and also what code are you using to point to an Excel file which is getting locked.

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    Here is the word macro code. The line in blue colour italics is mentioned to open the excel which has two columns 'Findtext' and 'replacetext'. I have more than 20 values to find and replace, so I am passing the values through an excel. After replacement of the values, I am not able to open up the excel "ref.xlsx". Please let me know if you need more information.

     

    screenshot of the error message.

    Radhika1_0-1637920370436.png

     

     

    Sub Main()

    Dim xl As Object 'Excel.Application

    Dim wb As Object 'Excel.Workbook

    Dim ws As Object 'Excel.Worksheet

    Dim rng As Object 'Excel.Range

    Dim cl As Object  'Excel.Range

    Set xl = CreateObject("Excel.Application")

    Set wb = xl.Workbooks.Open("Downloads\ref.xlsx") 

    Set ws = wb.Sheets(1) '##Modify as needed

    Set rng = ws.Range("A1", ws.Range("A29"))

    For Each cl In rng

        Call Macro1(cl.Value, cl.Offset(0, 1).Value)

    Next

    End Sub

    ---------------------------------------------------------------------------------------------

    Sub Macro1(findText$, replaceText$)

    '

    ' Macro1 Macro

    '

    '

        Selection.Find.ClearFormatting

        Selection.Find.Replacement.ClearFormatting

        With Selection.Find

            .Text = findText

            .Replacement.Text = replaceText

            .Forward = True

            .Wrap = wdFindContinue

            .Format = False

            .MatchCase = False

            .MatchWholeWord = False

            .MatchWildcards = False

            .MatchSoundsLike = False

            .MatchAllWordForms = False

        End With

        Selection.Find.Execute Replace:=wdReplaceAll

        Selection.Find.Execute

    End Sub

     

  • Verified answer
    VJR Profile Picture
    7,635 on at

    After the "Next" use wb.close to close the workbook that was opened by the blue statement.

     

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    Nice observation! Thanks. It is working now.

  • jiamin Profile Picture
    12 on at

    Hello, I have trouble replacing text in a word document directly using 'replace text' as this function only works in notepad. Could someone help me with this issue, thank you!!!! Please refer to the picture attached. The problem arises from 'read text from file' all the way to 'replace text'. Is there another method to replace the words in the document efficiently apart from Control H replace method? 

     

    jiamin_1-1650348305589.png

     

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Leaderboard > Power Automate

#1
Haque Profile Picture

Haque 557

#2
Valantis Profile Picture

Valantis 328

#3
David_MA Profile Picture

David_MA 264 Super User 2026 Season 1

Last 30 days Overall leaderboard