ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Error Trap for bypassing Password Protection (https://www.excelbanter.com/excel-programming/300491-error-trap-bypassing-password-protection.html)

Arawn

Error Trap for bypassing Password Protection
 
I have a macro where I am cycling through all of the *.XLS files in a
directory, but I have to segregate any file that has a non-blank
password out of that directory.

My current code keeps stopping with 1004-Runtime Errors for an invalid
password.
Ideally, I would like it to detect if a password is present, and if it
is blank, unprotect it; if not, then move the file to the specified
directory.

My code works fine with regard to the file-open password, but not with
any other protection scheme.

'===========================CODE

If ActiveWorkbook.MultiUserEditing = True Then
ActiveWorkbook.UnprotectSharing
ActiveWorkbook.ExclusiveAccess
End If

If ActiveWorkbook.ProtectStructure = True Then
If Err.Number < 0 Then GoTo HAS_WORKBOOK_LEVEL_PASSWORDS
ActiveWorkbook.Unprotect Password:=""
GoTo HAS_BLANK_WORKBOOK_LEVEL_PASSWORDS

HAS_WORKBOOK_LEVEL_PASSWORDS:

i = i + 1 '===File Counter for Directory
ActiveWorkbook.Close
Name temp_name As PWP_DIR_AND_FILE
GoTo Begin
Err.Clear
End If

HAS_BLANK_WORKBOOK_LEVEL_PASSWORDS:
Err.Clear



If ActiveWorkbook.ProtectWindows = True Then
On Error GoTo HAS_WORKBOOK_WINDOW_LEVEL_PASSWORDS
ActiveWorkbook.Unprotect Password:=""
GoTo HAS_BLANK_WORKBOOK_WINDOW_LEVEL_PASSWORDS
HAS_WORKBOOK_WINDOW_LEVEL_PASSWORDS:
i = i + 1
ActiveWorkbook.Close
Name temp_name As PWP_DIR_AND_FILE
GoTo Begin
Err.Clear
End If
HAS_BLANK_WORKBOOK_WINDOW_LEVEL_PASSWORDS:
Err.Clear



If ActiveSheet.ProtectContents = True Then
If Err.Number < 0 Then GoTo HAS_SHEET_PASSWORDS
ActiveSheet.Unprotect Password:=""
GoTo HAS_BLANK_SHEET_PASSWORD
HAS_SHEET_PASSWORDS:
i = i + 1
ActiveWorkbook.Close
Name temp_name As PWP_DIR_AND_FILE
Err.Clear
GoTo Begin
End If

HAS_BLANK_SHEET_PASSWORD:
Err.Clear

'==============================END CODE=====

I have been prowling the forums for an acceptable answer, but none has
availed itself as of yet.

Any help would be appreciated. Thanks!

~Arawn


All times are GMT +1. The time now is 08:49 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com