Link to home
Start Free TrialLog in
Avatar of newparadigmz
newparadigmzFlag for United States of America

asked on

Request for VBA code to password protect all Word docs in a folder

Title says it all, doesn't matter if code is in Word or Excel, but it is Word docs that I need to protect. Password can be the same for all.

Thanks!
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Why is this an Excel problem? I can do Word
ASKER CERTIFIED SOLUTION
Avatar of dlmille
dlmille
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And, here's the same code but written to run from Word.  I just pasted it in a new module in my Normal template for a quick test:
Option Explicit

Const Passwd = "Password"
Sub protectAllWordInFolder()
Dim dialogFile As FileDialog
Dim fDir As String
Dim fName As String
Dim strPath As String
Dim fPathFname As String
Dim oWordDoc As Word.Document
Dim xMsg As Long

    strPath = ThisDocument.Path 'initial path for directory prompt
    
    ' Open the file dialog
    Set dialogFile = Application.FileDialog(msoFileDialogFolderPicker)
    With dialogFile
        .AllowMultiSelect = False
        .InitialView = msoFileDialogViewDetails
        .InitialFileName = strPath & "\"
        .Title = "Select Folder for Processing"
        .Show
    End With
    If dialogFile.SelectedItems.Count > 0 Then
        fDir = dialogFile.SelectedItems(1)
        
        fName = Dir(fDir & "\*.doc*")
        
        If fName <> vbNullString Then
            
            Do
                fPathFname = fDir & "\" & fName
                'Application.Visible = True
                On Error Resume Next
                Set oWordDoc = Application.Documents.Open(FileName:=fPathFname)
                oWordDoc.Password = Passwd
                If Err.Number <> 0 Then
                    xMsg = MsgBox("Could not open word document or change password: " & fPathFname, vbYesNo, "Skip and Continue? (YES) Abort? (NO)")
                    If xMsg = vbNo Then GoTo gracefulExit
                    If Not oWordDoc Is Nothing Then oWordDoc.Close savechanges:=False
                Else
                    oWordDoc.Close savechanges:=True
                End If
                fName = Dir()
            Loop Until fName = vbNullString
            
        End If
    Else
        fDir = ""
    End If
        
gracefulExit:

    'cleanup
    If Not oWordDoc Is Nothing Then oWordDoc.Close savechanges:=False

    Set dialogFile = Nothing
End Sub

Open in new window


Cheers,

Dave
Avatar of newparadigmz

ASKER

Once again, very much appreciated.

Agreed on running from Excel, also increases odds of an answer as there are much more experts in that than Word.

two quick questions for my curiousity please;

Why is the constant outside of the sub?

How come I do not see the Word docs open on the Open command?
They always do for Excel.
>> Why is the constant outside of the sub?
because I wanted you to notice it.  

I also generally put constants outside the subs, and more often make them public as my code gets larger and I'm sharing with other modules.  Just my personal style.

>>How come I do not see the Word docs open on the Open command?
because the oWordApp is not visible.  Uncomment line 33 if you want to see it, but only for debugging purposes as it can run more slowly and you might interact between it and the running macro - not a good combination for fast typists like me who inadvertently interacts with Windows prompts about 5% of the time when popups hit me in the face while I'm typing, lol.

Dave