unprotect a word document

Skip to first unread message

Jeff Wright

Apr 25, 2011, 11:31:08 AM4/25/11
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")
# 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



--------------------------------- --- -- -
Posted with NewsLeecher v4.0 Final
Web @ http://www.newsleecher.com/?usenet
------------------- ----- ---- -- -

Jeff Wright

Apr 25, 2011, 1:21:28 PM4/25/11
Doh...answered my own question:

can use:


Apr 30, 2011, 6:16:04 AM4/30/11
Hi Jeff

Your Post was inspiring to me. Thanks.
But I have another question on this:

> We want the user to be able to fill out the
> fields but not accidentally edit the form itself.

Shouldn't you use for this very reason a template (dotx) instead of a
document (docx)?
As default dotx creates a NEW document based on the template and does not
OPEN the template itself. The template will remain unaffected.


Message has been deleted

Madina Saitakhmetova

Oct 4, 2021, 10:40:50 AM10/4/21
If someone stumbled on this post like me trying to do Protect/Unprotect in Powershell with the option “Filling in Forms” to prevent users from editing the content outside the fillable areas, I am adding the code that worked for me:

$ProtectionType = [Microsoft.Office.Interop.Word.WdProtectionType]::wdAllowOnlyFormFields

$objWord = New-Object -comobject Word.Application
$objDoc = $objWord.Documents.Open("E:\Files\WordDocument.docx")

/// do what you need to do

Reply all
Reply to author
0 new messages