batch set the password for word documents

Rewly

New member
Local time
Yesterday, 18:56
Joined
Jun 8, 2017
Messages
2
Hi.

I have quite a few word files and I want to set the password for them all to protect them.

Do I have to set the password one by one? Any way to batch set the password for several word documents?
 
in word VBE, paste the code below into a module.
then call it with

ScanFilesInDir "C:\Users\user\documents"

it will scan all .doc or .docX and save them with a password.

BE SURE TO REPLACE YOUR PASSWORD BELOW IN THE CODE


Code:
'---------------
Public Sub ScanFilesInDir(ByVal pvDir)
'---------------
Dim fso
Dim oFolder, oFile
Dim vName, vSrc

On Error GoTo errImp

If Right(pvDir, 1) <> "\" Then pvDir = pvDir & "\"

Set fso = CreateObject("Scripting.FileSystemObject")
Set oFolder = fso.GetFolder(pvDir)

For Each oFile In oFolder.Files
    vFil = pvDir & oFile.Name

    If InStr(vFil, ".doc") > 0 And InStr(vFil, pvID) > 0 Then       'ONLY DO .doc files
           vSrc = pvDir & oFile.Name
           'MsgBox vSrc

        Documents.Open vSrc
        vName = ActiveDocument.Name
        'MsgBox vName
        ActiveDocument.SaveAs vName, Password:="password" 
        ActiveDocument.Close False
    End If
Next

Set fso = Nothing
Set oFile = Nothing
Set oFolder = Nothing
Exit Sub

errImp:
MsgBox Err.Description, vbCritical, "clsImport:ImportData()" & Err
Exit Sub
Resume
End Sub
 
That's a nice bit of code and may be useful to me as well
Thanks
 
Hi,

As have mentioned above, You can use VBA codes.

1.Prevent Multiple Documents from Opening

Sub ProtectMultiDocWithOpenPassword()
Dim objDoc As Document
Dim strPassword As String, strFile As String, strFolder As String

strPassword = "123"
strFolder = "E:\Temp\test"
strFile = Dir(strFolder & "*.docx", vbNormal)

While strFile <> ""
Set objDoc = Documents.Open(FileName:=strFolder & strFile)

With objDoc
.Password = strPassword
.SaveAs2 FileName:=objDoc.FullName, Password:=strPassword
.Close
End With
strFile = Dir()
Wend
End Sub

2.Prevent Multiple Documents from Editing

Sub ProtectMultiDocWithEditPassword()
Dim objDoc As Document
Dim strPassword As String, strFile As String, strFolder As String

Set objDoc = ActiveDocument
strPassword = "456"
strFolder = "C:\Users\Public\Documents\New folder"
strFile = Dir(strFolder & "*.docx", vbNormal)

While strFile <> ""
Set objDoc = Documents.Open(FileName:=strFolder & strFile)

objDoc.Protect Password:=strPassword, NoReset:=False, Type:= _
wdAllowOnlyReading, UseIRM:=False, EnforceStyleLock:=False
objDoc.Save
objDoc.Close
strFile = Dir()
Wend
End Sub

You can search the article on google:

"How to Quickly Protect a Batch of Word Documents with Password"

Good luck.
 
Thanks for all help! The macros are amazing!
 

Users who are viewing this thread

Back
Top Bottom