Discussion:
unprotect a word document
(too old to reply)
Jeff Wright
2011-04-25 15:31:08 UTC
Permalink
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
2011-04-25 17:21:28 UTC
Permalink
Doh...answered my own question:

can use:
$wordDoc.Unprotect()
$wordDoc.Protect("wdAllowOnlyFormFields")
--
--------------------------------- --- -- -
Posted with NewsLeecher v4.0 Final
Web @ http://www.newsleecher.com/?usenet
------------------- ----- ---- -- -
Marcello
2011-04-30 10:16:04 UTC
Permalink
Hi Jeff

Your Post was inspiring to me. Thanks.
Post by Jeff Wright
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
Madina Saitakhmetova
2021-10-04 14:40:45 UTC
Permalink
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);

Madina Saitakhmetova
2021-10-04 14:25:58 UTC
Permalink
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);
Continue reading on narkive:
Loading...