Filling word with powershell


I would like to fill a word document with powershell. I have specified bookmarks in my word's template. I would like my script pulls data from another file, populates bookmarks in word template and saves a new word document in a sharepoint website.

I have started with the script below to try but I do not know how I can manage bookmarks. About saving to sharepoint location it is a second step and I will try to find solution.

Thank you in advance for your help


# Variables used to get text from text file

$Title = get-content "C:\location\source.txt" | Select -Index 0
$LineOne = get-content "C:\location\source.txt" | Select -Index 1
$LineTwo = get-content "C:\location\source.txt" | Select -Index 2
$LineThree = get-content "C:\location\source.txt" | Select -Index 3
$Date = get-content "C:\location\source.txt" | Select -Index 4

$source = "C:\location\source.txt"
$destination = "C:\user\Downloads\newfile.docx"
$template = "C:\location\template.dotx"
[ref]$SaveFormat = "" -as [type]
# Open Word
$word = New-Object -comobject word.application
$word.visible = $false
$word2 = New-Object -ComObject word.application
$word2.visible = $false
# Create new from template
$doc = $$source)
$doc2 = $word2.Documents.add($template)
# Copy
$range = $doc.Range()
$copy = $range.Copy()
# Paste
$range2 = $doc2.Range()
# Save
$doc2.saveas([ref]$destination, [ref]$saveFormat::wdFormatDocument)
# Close Word

Open in new window

ChristopheIT CoordinatorAsked:
Who is Participating?
NorieVBA ExpertCommented:

You should be able to use something like this to work with bookmarks.
$doc2.Bookmarks.Item("MyBookMark").Range.Text = "MyText"

Open in new window

ChristopheIT CoordinatorAuthor Commented:
Hello Norie,

Thank you for your help. I am trying your solution but it does not work. I am not sure how I can implement your code.

Could you give me an example?

NorieVBA ExpertCommented:

What exactly did you try and how did it not work?
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

ChristopheIT CoordinatorAuthor Commented:

I have created a bookmark in word template. Bookmark is named T2

# Paste
$range2 = $doc2.Bookmarks.Item("T2").Range.Text = "MyText"

Open in new window

The requested member of the collection does not exist.
At H:\Scripts\Jaquettes\cdtest.ps1:31 char:11
+ $range2 = $doc2.Bookmarks.Item("T2").Range.Text = "MyText"
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At H:\Scripts\Jaquettes\cdtest.ps1:32 char:1
+ $range2.Paste($copy)
+ ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
NorieVBA ExpertCommented:

Here is a very simple example with a document containing a bookmark named 'T2'.
# Open Word and set text of bookmark
$newtext = "This is the new text."
$word = New-Object -comobject word.application
$word.visible = $true
$doc = $"C:\test\test.docx")
$bookmark = $doc.Bookmarks("T2")
$bookmark.Range.Text = $newtext

Open in new window

Are you sure that the bookmark does exist?

I don't know Powershell, but it must use the Word Object Model in a similar way to VBA.

The VBA code that Norie has  posted will work - once. However it is likely to overwrite the bookmark itself, so that it isn't there for a second attempt. I use a procedure like this:
Sub FillBookmark(doc As Document, strBookMark As String, strNewText As String)
    Dim rng As Range
    Set rng = doc.Bookmarks(strBookMark).Range
    rng.Text = strNewText
    doc.Bookmarks.Add strBookMark, rng 're-add the bookmark in case it was overwritten
End Sub

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.