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 / Creating Word document...
Power Automate
Answered

Creating Word documents and Changing contents of word document using PAD

(0) ShareShare
ReportReport
Posted on by 8

I need to create new files when a case is registered. I need to create 3 documents in which the contents need to be replaced with new values. I have successfully created new folder, rename the folder, and rename the created files. Now I need to replace the contents of the newly created and renamed files with new prescribed values. I tried doing this by individually opening each word document and UI elements to open Find and Replace enter old values and new values and click on replace. This process works sometimes but goes error most of the times. Can this community help me in getting best approach.

Screenshot 2023-04-23 135320.png
Screenshot 2023-04-23 135333.png
I have the same question (0)
  • Verified answer
    Nived_Nambiar Profile Picture
    18,138 Super User 2026 Season 1 on at

    Hi @TarunKittu 

     

    For word document, you can give a try with powershell script which does the replace operation in word 

     

    Nived_Nambiar_0-1682256425662.png

     

    powershell script used 

     

    # Open Word and create a new document object
    $word = New-Object -ComObject Word.Application
    $doc = $word.Documents.Open("%word_doc%")
    
    # Find and replace text in the document
    $find = "%find_text%"
    $replace = "%replace_text%"
    $matchCase = $false
    $matchWholeWord = $false
    $matchWildcards = $false
    $matchSoundsLike = $false
    $matchAllWordForms = $false
    
    $doc.Content.Find.Execute($find, $matchCase, $matchWholeWord, $matchWildcards, $matchSoundsLike, $matchAllWordForms, $true, 1, $false, $replace, 2)
    
    # Save and close the document
    $doc.Save()
    $doc.Close()
    
    # Quit Word
    $word.Quit()

    Here %word_doc% , %replace_text%, %find_text% are variables from PAD for filepath, text to replace, text to find respectively in powershell script

    Hope this helps 🙂

    Mark it as solution if it resolves your query 🙂

  • TarunKittu Profile Picture
    8 on at

    @Nived_Nambiar Thanks for your reply. No, this didn't resolve my query. I'm getting the attached screenshot as error. 

    Regards

    Screenshot 2023-04-25 070927.png
  • Nived_Nambiar Profile Picture
    18,138 Super User 2026 Season 1 on at

    Hi @TarunKittu 

     

    to make the script work, you need to enable macros in word 

     

    refer this link 

    https://www.wikihow.com/Enable-Macros-in-Microsoft-Word

     

  • TarunKittu Profile Picture
    8 on at

    @Nived_Nambiar Thanks for the reply, but that doesn't help. I have enabled running macros on word now it is not showing error but the text is not getting replaced in the word document.

    I'm getting the following error in the variable script error

    You cannot call a method on a null-valued expression.
    
    
    At C:\Users\tarun\AppData\Local\Temp\Robin\4orfl4nzys1.tmp.ps1:14 char:1
    + $doc.Content.Find.Execute($find, $matchCase, $matchWholeWord, $matchW ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
    
    You cannot call a method on a null-valued expression.
    At C:\Users\tarun\AppData\Local\Temp\Robin\4orfl4nzys1.tmp.ps1:17 char:1
    + $doc.Save()
    + ~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
    
    You cannot call a method on a null-valued expression.
    At C:\Users\tarun\AppData\Local\Temp\Robin\4orfl4nzys1.tmp.ps1:18 char:1
    + $doc.Close()
    + ~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
  • Nived_Nambiar Profile Picture
    18,138 Super User 2026 Season 1 on at

    Hi @TarunKittu 

     

    I think $doc variable is null. Just check two things here

     

    1. whether the filepath (ie %word_doc% variable from PAD) is the correct path

     

    2.  Also check whether that file is not opened already, while running the PAD script 

     

     

     

  • TarunKittu Profile Picture
    8 on at

    @Nived_Nambiar Thanks for the reply, I understood that actually before coming here for solution because I entered the path of file directly in the script and it worked. But if I give it through variable it is not able to assign to $doc variable. I have checked file path in PAD is correct and file is also not opened

  • Nived_Nambiar Profile Picture
    18,138 Super User 2026 Season 1 on at

    Hi @TarunKittu 

     

    Then it would be the problem with variable assignment 

     

    Could you show how the variable for filepath is assigned and how you specify the variable in the powershell script which i have shared ?

  • Riyaz_riz11 Profile Picture
    4,214 Super User 2026 Season 1 on at

    Hi @TarunKittu ,

    Can you follow this steps.

    Ahammad_Riyaz_1-1683003493403.png

    First step {{UpcomingCashChart}} create like this in your word file and pass that in the run vb script action.

     

     

    Ahammad_Riyaz_0-1683003454838.png

    
    
    Dim in_WordDocPath 'Path of the Word document
    Dim in_AnchorText 'Text to identify the placeholder for the content
    dim filesys
    Set filesys = CreateObject("Scripting.FileSystemObject")
    
    
    
    
    in_WordDocPath = "%strOutput_file%"
    
    
    
    Dim objWord, objDoc
    Set objWord = CreateObject("Word.Application")
    objWord.DisplayAlerts = False
    Set objDoc = objWord.Documents.Open(in_WordDocPath)
    objWord.Visible = False
    
    With objWord.Selection.Find
    .Text = "{{UpcomingCashChart}}"
    .Forward = True
    .Execute
    End With
    objWord.Selection.TypeText ("%strPName%")
    objWord.Selection.Style = "Normal"
    objDoc.Save
    objDoc.Close

    %strOutput_file% define Output path

    strPName% define the value what need to be pass.

     

    Regards

    Ahammad Riyaz

    --------------------------------------------------------------------------------
    If this post helps answer your question, please click on “Accept as Solution” to help other members find it more quickly. If you thought this post was helpful, please give it a Thumbs Up.

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
David_MA Profile Picture

David_MA 262 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 167

#3
Haque Profile Picture

Haque 154

Last 30 days Overall leaderboard