Link to home
Create AccountLog in
Avatar of Christophe
ChristopheFlag for France

asked on

Filling word with powershell

Hello,

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

Christophe

# 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 = "microsoft.office.interop.word.WdSaveFormat" -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 = $word.documents.open($source)
$doc2 = $word2.Documents.add($template)
 
# Copy
$range = $doc.Range()
$copy = $range.Copy()
 
# Paste
$range2 = $doc2.Range()
$range2.Paste($copy)
 
# Save
$doc2.saveas([ref]$destination, [ref]$saveFormat::wdFormatDocument)
 
# Close Word
$word.Application.ActiveDocument.Close()
$word.Quit()
$word2.Application.ActiveDocument.Close()
$word2.Quit()

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Christophe

ASKER

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?

Thanks
Christophe
Avatar of Norie
Norie

Christophe

What exactly did you try and how did it not work?
Norie,

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

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

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
Christophe

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 = $word.documents.open("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