unprotect a word document

197 views
Skip to first unread message

Jeff Wright

unread,
Apr 25, 2011, 11:31:08 AM4/25/11
to
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


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

Jeff Wright

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

can use:
$wordDoc.Unprotect()
$wordDoc.Protect("wdAllowOnlyFormFields")

Marcello

unread,
Apr 30, 2011, 6:16:04 AM4/30/11
to
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.

Lg
Marcello


Message has been deleted

Madina Saitakhmetova

unread,
Oct 4, 2021, 10:40:50 AMOct 4
to
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")
$objDoc.Activate();
$objDoc.Unprotect();

/// do what you need to do

$objDoc.Protect($ProtectionType,$True);
Reply all
Reply to author
Forward
0 new messages