Using Powershell open and edit an existing word document RRS feed

  • Question

  • Hey guys,

    I'm trying to use Powershell to open an existing word document that has a few sections, headings and empty tables.

    I've managed to open the word document, but what I'd like to do is move around the word document and enter text.

    Is there a way to send "move" commands into Word, or find/replace?

    My end goal is to replace some of the headings and add text into specific cells of the existing tables.

    Any help or guidance would be much appreciated.

    • Moved by Bill_Stewart Thursday, December 13, 2018 3:19 PM This is not Office application object model training forum
    Wednesday, September 19, 2018 5:27 AM

All replies

  • There is n o "move" commands in Word. You can enumerate the various properties and objects.

    You will need to first learn the Word object model and how to use it with PowerShell.


    Wednesday, September 19, 2018 5:46 AM
  • How about using bookmarks as locations?

    If I have a word document with bookmarks at key locations, is there a way I move the cursor those bookmarks and enter text?

    • Edited by amary96 Wednesday, September 19, 2018 6:30 AM
    Wednesday, September 19, 2018 6:06 AM
  • Just specify the bookmark by name.


    Wednesday, September 19, 2018 6:32 AM
  • I dont know if youre being intentionally vague or dont understand what Im asking.

    I can use the code below to place text into the location of the bookmark, which in this example is in the second column of a table.

    How do I then have it move into the next column to add other text?

    $objWord = New-object -comobject Word.Application  
    $objWord.Visible = $True 
    $objDoc = $objWord.Documents.Open("C:\Temp\Test doco.docx") 
    $objRange = $objDoc.Bookmarks.Item("bookmark1").Range.text = "Test 123"

    Wednesday, September 19, 2018 6:52 AM
  • Like I said, there is no "move" in Word.  You need to address the object directly.  A table in word is an object that has rows and columns.


    Wednesday, September 19, 2018 6:53 AM
  • OK, understand now. Thanks.
    Wednesday, September 19, 2018 6:58 AM
  • $doc = $wd.Documents.Open($filename)
    $t = $doc.Bookmarks['mytable'].Range.Tables[1]


    Wednesday, September 19, 2018 7:10 AM
  • $wd = New-object -comobject Word.Application  
    $wd.Visible = $true 
    $doc = $wd.Documents.Open($filename)
    # get the table
    $t = $doc.Bookmarks['mytable'].Range.Tables[1]
    # address by row
    # address by Cell(x,y)


    Wednesday, September 19, 2018 7:22 AM