Macros to Protect and Unprotect documents

H

Heidi

Hello. We have thousands of documents that we need to
protect at the file level. I thought that perhaps a
macro would help with the issue. However, there seems to
be a sequencing problem. I can write a macro to protect,
but not save, a document. But, I can't write one to
unprotect it as the macro recorder is locked when I enter
a protected document (the only way that I can get to
Tools | Unprotect). Have you any suggestions for this
problem or for protecting large numbers of files? Thank
you sincerely for your time.
 
J

Jeff Jones

Heidi,
What you are asking to do is easily done once the syntax is
correct. Additionally, I'd explore having a macro spin through the
files in a folder to open, protect, and do a save and close. All can
be done with macros. I know because I've done all of these things.

You haven't stated if you want a password so I'll include both a
protect with and without a password and an unprotect as well as a save
and close..

Protect: WordBasic.ToolsProtectDocument NoReset:=1, Type:=2
The NoReset option will retain data in any MS Word form fields.

If you want a password, use this to protect the document.
WordBasic.ToolsProtectDocument DocumentPassword:="password here", _
NoReset:=1, Type:=2

Unprotect:
WordBasic.ToolsUnProtectDocument

Save and Close a document:
ActiveDocument.Close SaveChanges:=wdSaveChanges

If you want the logic to open, change, and save a document and then go
on to the next document, let me know and I'll include it in another
reply.

Hope this helps.

Jeff
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

HI Heidi,

While Jeff's code probably works, there is no need to invoke the WordBasic
object.

The following macro will open all of the documents in a folder, apply
protection do them and then save them:

Dim myFile As String, PathToUse As String, myDoc As Document
'Change the Drive and Path in the following line to suit
PathToUse = "D:\Documents\"
myFile = Dir$(PathToUse & "*.doc")
While myFile <> ""
'Open document
Set myDoc = Documents.Open(PathToUse & myFile)
'Protect document
myDoc.Protect wdAllowOnlyFormFields, NoReset
'Save and Close
myDoc.Close SaveChanges:=wdSaveChanges
'Next file in folder
myFile = Dir$()
Wend

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.
Hope this helps
Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top