Can I check if all excels in a folder have been given password protection?

jscranton

Well-known Member
Joined
May 30, 2011
Messages
707
First, I am not trying to crack any passwords. I am, instead, trying to make sure that a large folder of excels all have been password protected. Obviously, the issue with something like the below is that I have to open the password protected wb to set the variable (which I can't do with a password protected book).

Any thoughts?


Code:
Sub IsWorkbookProtected()Dim wb As Workbook
Dim myPath
Dim myFile

  myPath = "SomePath\"


  Do While myFile <> ""

      Set wb = Workbooks.Open(Filename:=myPath & myFile)


     With wb
       If .ProtectWindows Or .ProtectStructure Then
         Debug.Print .PasswordEncryptionAlgorithm
      Else
        Debug.Print "This workbook is not password protected"
      End If
     End With

      myFile = Dir

Loop


End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi,
try following & see if does what you want

Code:
Sub IsWorkbookProtected()
    Dim wb As Workbook
    Dim myPath As String, myFile As String
    Dim listFiles As Range
    Dim r As Long
    
    myPath = "C:\SomePath\"
    
'range to list all unprotected files
    Set listFiles = ThisWorkbook.Sheets(1).Range("A1")
    
    myFile = Dir(myPath & "*.xl*", vbDirectory)
    
    Application.ScreenUpdating = False
    Do While myFile <> ""
        If myFile <> ThisWorkbook.Name Then
            On Error Resume Next
'open workbook with junk password
            Set wb = Workbooks.Open(FileName:=myPath & myFile, Password:="abczzzzzzz")
            If Err.Number = 0 Then
'file not protected - add to list
                listFiles.Offset(r, 0).Value = myFile
                r = r + 1
            End If
            wb.Close False
        End If
        On Error GoTo 0
        myFile = Dir
        Set wb = Nothing
    Loop
    Application.ScreenUpdating = True
End Sub

Dave
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top