i'm trying to write a Powershell-script that edits Text Form Fields in Microsoft Office Word 2007. It should find a Form Field via the Bookmark I configured before and write a Text into it. The default text I wrote into it for test purposes is "Something". That's what I have so far:
$document = 'D:\Powershell\Test.docx'
$Word = New-Object -Com Word.Application
$Word.Visible = $True
$doc = $word.Documents.Open($document)
$text = "Hello"
$bookmark = "server1"
$doc.Bookmarks.Item($bookmark).Range.Text.Replace("Something", $text)
While it works in the console since the output is:
FORMTEXT Hello
Word still displays the String I inserted manually before. When I type in:
$doc.Bookmarks.Item($bookmark).Range.Text
The output is:
FORMTEXT Something
I already tried:
$Word.ActiveDocument.Reload()
$Word.ActiveDocument.Fields.Update()
$doc.PrintPreview()
$doc.ClosePrintPreview()
$doc.Bookmarks.Item($bookmark).Range.Fields.Update()
But nothing seems to work. Has somebody an idea how to write something in that Text Form Field permanently? Alternatively if that's easier I could use a (rich) Text Content Control (which seem to be newer). Those don't use a bookmark but a tag and title. Thanks for you help in advance . PS:It doesn't work with MS Word 2016 either.