Jeff Wright
2011-04-25 15:31:08 UTC
I am using powershell to populate fields (bookmarks) in a word
document. The document has several other fields that a someone has
to edit. We want the user to be able to fill out the fields but not
accidentally edit the form itself. It seems like the best way to do
this is the restrict editing on the document to filling out the form
fields only. However, when I set up the document this way, I am
unable to populate the fields in via powershell. My thought was to
unprotect the document via the script, fill the fields, protect the
document and save it. Then the user could edit it. However, I can't
seem to get the syntax right. Can anyone provide some info on the
use of "unprotect/protect"? I am using Word 2010. Here is a snippet:
$NewDocName = "C:\some-path-to-newdoc.docx"
$DocTemplate = "C:\some-path-to-masterWorddoc.docx"
$msWord = New-Object -Com Word.Application
$msWord.visible = $false
$wordDoc = $msWord.Documents.Open("$DocTemplate")
$wordDoc.Activate()
# attempt to unprotect the document here. I did not put a
password on the file.
$wordDoc.Unprotect Password:=""
$objRange = $wordDoc.Bookmarks.Item("lastname").Range
$objRange.Text = "wright"
# attempt to protect the doc here:
$wordDoc.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
$filename = $NewDocName
$wordDoc.SaveAs([REF]$filename)
$wordDoc.Close()
$msWord.Application.Quit()
Thanks,
Jeff
document. The document has several other fields that a someone has
to edit. We want the user to be able to fill out the fields but not
accidentally edit the form itself. It seems like the best way to do
this is the restrict editing on the document to filling out the form
fields only. However, when I set up the document this way, I am
unable to populate the fields in via powershell. My thought was to
unprotect the document via the script, fill the fields, protect the
document and save it. Then the user could edit it. However, I can't
seem to get the syntax right. Can anyone provide some info on the
use of "unprotect/protect"? I am using Word 2010. Here is a snippet:
$NewDocName = "C:\some-path-to-newdoc.docx"
$DocTemplate = "C:\some-path-to-masterWorddoc.docx"
$msWord = New-Object -Com Word.Application
$msWord.visible = $false
$wordDoc = $msWord.Documents.Open("$DocTemplate")
$wordDoc.Activate()
# attempt to unprotect the document here. I did not put a
password on the file.
$wordDoc.Unprotect Password:=""
$objRange = $wordDoc.Bookmarks.Item("lastname").Range
$objRange.Text = "wright"
# attempt to protect the doc here:
$wordDoc.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
$filename = $NewDocName
$wordDoc.SaveAs([REF]$filename)
$wordDoc.Close()
$msWord.Application.Quit()
Thanks,
Jeff
--
--------------------------------- --- -- -
Posted with NewsLeecher v4.0 Final
Web @ http://www.newsleecher.com/?usenet
------------------- ----- ---- -- -
--------------------------------- --- -- -
Posted with NewsLeecher v4.0 Final
Web @ http://www.newsleecher.com/?usenet
------------------- ----- ---- -- -