PDA

View Full Version : [SOLVED:] .HasPassword property



Zack Barresse
10-27-2004, 04:35 PM
Hello,

Okay, I've beat my head against the wall here. I'm trying to set/unset workbook (and worksheet, but that's not the issue at hand) protection via code. I'm trying to check if the workbook has a password before I set any kind of password. The process I'm using isn't working. It always evaluates to False. Anybody see anything wrong off hand? (I'm sure I'm looking too closely..)


If ActiveWorkbook.HasPassword Then
If MsgBox("Are you sure you want to delete the WorkBook Protection?", _
vbYesNo, "Delete WorkBook Password?") = vbNo Then Exit Sub
'Do something here...
Else
MsgBox "You don't have a password on this Workbook!", vbInformation, "ERROR"
End If


By off-chance, does anybody know how to get Excel's native password dialog box to pop-up, as so we can use that instead? I hate doing more than is necessary.. :rolleyes:

AJS
10-27-2004, 05:26 PM
I haven't checked, but should it be:


If ActiveWorkbook.HasPassword = True Then
instead?

TonyJollans
10-27-2004, 05:46 PM
Hi Zack,

The HasPassword property reflects the saved workbook and only gets changed when you save the workbook. Adding a password alone won't change it.

The Dialog can be shown with ..

Application.Dialogs(xlDialogWorkbookProtect).Show

MUSASHI
10-27-2004, 06:02 PM
btw Tony, I love Magritte!!

johnske
10-27-2004, 06:09 PM
Hi zack,

Note that with respect to the xldialog Tony has given above...

If the book is unprotected it will show the "protect" dialog box,

if the book is already protected it will show the "unprotect" dialog box instead

john :bink:

Jacob Hilderbrand
10-27-2004, 07:41 PM
Try this:


If ActiveWorkbook.ProtectStructure = True Or ActiveWorkbook.ProtectWindows = True Then
MsgBox "It's already protected."
Else
MsgBox "It's not protected."
End If

Zack Barresse
10-27-2004, 11:50 PM
Thanks guys! Much appreciated. :yes

I (upon testing w/ file tomorrow) think I will take an incorporation of both techniques. Thanks again for the input. :)

johnske
10-28-2004, 03:12 AM
PS
Zack, I just tried your original code and added "= True" and it also works..i.e.



If ActiveWorkbook.HasPassword = True Then
If MsgBox("Are you sure you want to delete the WorkBook Protection?", _
vbYesNo, "Delete WorkBook Password?") = vbNo Then Exit Sub
'Do something here...
Else
MsgBox "You don't have a password on this Workbook!", vbInformation, "ERROR"

John :bink:

EDIT: Looking back, I see now that AJS has already suggested that addition.. :)

johnske
10-28-2004, 03:57 AM
Hi Zack, just went back, tried again, and this time it DIDN'T work - after playing around, the "HasPassword" keyword doesn't refer to the password 'protect workbook' available from the tools menu, it refers to the password applied in the "Save As" options. i.e. The password required to actually open/access the workbook at all...:bink:

Zack Barresse
10-28-2004, 09:12 AM
Hmm, interesting. Also, in regards to the '= True' portion, you don't need to put that as it's assumed for True (as we're talking Boolean) if omitted. But thanks. :) All efforts are extremely appreciated!! :yes