VBS Script to remove protection from all word docs in directories

S

Samson

We have literally tens of thousands of documents that are protected and some
are even password protected. I am trying to find a way to remove the
protection from multiple files without having to open every one and skip
those with a password. Any help is appreciated.
 
G

Graham Mayor

How are the documents protected if not by a password? See
http://www.gmayor.com/Remove_Password.htm

Is the password the same for each document? If so it would be fairly simple
to batch process the folder(s) to open the documents and remove the
passwords. The following macro will open and if necessary apply one or both
passwords to the documents in order to open them and set the passwords to
nul. The documents for which the password is correct are saved to the
TargetFolder which here is an existing sub folder of the original folder
called Temp. You could error trap that too if you want.

Sub BatchRemovePassword()
Dim OpenPwd As String
Dim WritePwd As String
Dim myFile As String
Dim PathToUse As String
Dim TargetPath As String
Dim iFld As Integer

OpenPwd = "password"
WritePwd = "another"

With Dialogs(wdDialogCopyFile)
If .Display <> 0 Then
PathToUse = .Directory
Else
MsgBox "Cancelled by User"
Exit Sub
End If
End With
If Documents.Count > 0 Then
Documents.Close savechanges:=wdPromptToSaveChanges
End If
If Left(PathToUse, 1) = Chr(34) Then
PathToUse = Mid(PathToUse, 2, Len(PathToUse) - 2)
End If

TargetPath = PathToUse & "Temp\"

myFile = Dir$(PathToUse & "*.doc")
While myFile <> ""
On Error GoTo WrongPwd:
Documents.Open FileName:=myFile, _
PasswordDocument:=OpenPwd, _
WritePasswordDocument:=WritePwd
With ActiveDocument
.ReadOnlyRecommended = False
.Password = ""
.WritePassword = ""
End With
ActiveDocument.SaveAs TargetPath & ActiveDocument.Name
ActiveDocument.Close savechanges:=wdDoNotSaveChanges
WrongPwd:
If Err.Number = 5408 Then 'Document is Password-protected and was NOT
Opened
Err.Clear
Resume Next
End If
myFile = Dir$()
Wend
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
Joined
Nov 5, 2025
Messages
16
Reaction score
0
If your MS Word (.doc/.docx) file is not opening due to a password or protection, Softaken Freeware Word Password Remover Tool is an easy and reliable solution. This tool helps you quickly remove editing protection, read-only restrictions, and formatting protection from Word documents. The software is very easy to use. Simply add the Word file and remove the protection in just a few clicks. Importantly, the original data of the file remains intact, and the content is not altered during this process. The Freeware Word Password Remover Tool supports all major MS Word versions and Windows operating systems. This tool can be very useful for users who need a fast and effective solution to their Word document password problems.
 
Top