Help Tweaking a Macro

B

Bruce Bumbier

I am trying to run this Macro that removes the same password from multiple
Word files, but I don't know programming and I need to know where to insert
the existing password to unlock each file.

Public Sub UnPasswordALL()

Dim FirstLoop As Boolean
Dim strFileName As String
Dim sPassword1 As String
Dim sPassword2 As String
Dim strPath As String
Dim oDoc As Document
Dim Response As Long
Dim fDialog As FileDialog

Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)

With fDialog
.Title = "Select Folder and click OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User", , "Password"
Exit Sub
End If
strPath = fDialog.SelectedItems.Item(1)
If Right(strPath, 1) <> "\" Then strPath = strPath + "\"
End With

sPassword1 = InputBox("Enter Password to open Document")
sPassword2 = InputBox("Enter Password to edit Document")

On Error Resume Next
Documents.Close saveChanges:=wdPromptToSaveChanges
FirstLoop = True

strFileName = Dir$(strPath & "*.doc")
While strFileName <> ""
Set oDoc = Documents.Open(strPath & strFileName,
PasswordDocument:=sPassword1, WritePasswordDocument:=sPassword2)
If FirstLoop Then
With ActiveDocument
.Password = ""
.WritePassword = ""
End With
FirstLoop = False

Response = MsgBox("Do you want to process" & "the rest of the files in
this folder", vbYesNo)
If Response = vbNo Then Exit Sub
Else
With ActiveDocument
.Password = ""
.WritePassword = ""
End With
End If
oDoc.Close saveChanges:=wdSaveChanges
strFileName = Dir$()
Wend
End Sub

Thanks
 
L

Lene Fredborg

The to code lines:
sPassword1 = InputBox("Enter Password to open Document")
sPassword2 = InputBox("Enter Password to edit Document")
each displays a dialog box that asks you to enter a password. In the first
dialog box you must enter the password specified in Tools > Options >
Secutiry tab > “Password to openâ€. In the second dialog box you must enter
the password specified in Tools > Options > Secutiry tab > “Password to
modifyâ€.

The values you enter will be used to open the documents. Therefore, you do
_not_ need to change the code.

A little more about how the macro works:
The passwords you enter are stored in the variables sPassword1 and sPassword2.

Later in the code, the values are used when opening each document – this
happens in the following line:
Set oDoc = Documents.Open(strPath & strFileName,
PasswordDocument:=sPassword1, WritePasswordDocument:=sPassword2)

Finally, both passwords are replaced by empty strings (i.e. the passwords
are removed) in these lines:
.Password = ""
.WritePassword = ""

The macro runs through all files in the folder you select if you answer yes
to “Do you want to process the rest of the files in this folder".

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 

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