batch set the password for word documents (1 Viewer)

Rewly

New member
Local time
Today, 02:30
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?
 

Ranman256

Well-known member
Local time
Today, 05:30
Joined
Apr 9, 2015
Messages
4,337
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
 

isladogs

MVP / VIP
Local time
Today, 09:30
Joined
Jan 14, 2017
Messages
18,186
That's a nice bit of code and may be useful to me as well
Thanks
 

Lsudls

New member
Local time
Today, 02:30
Joined
Jun 13, 2017
Messages
1
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.
 

Rewly

New member
Local time
Today, 02:30
Joined
Jun 8, 2017
Messages
2
Thanks for all help! The macros are amazing!
 

Users who are viewing this thread

Top Bottom