Consulting

Results 1 to 10 of 10

Thread: .HasPassword property

  1. #1
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location

    .HasPassword property

    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..

  2. #2
    VBAX Regular AJS's Avatar
    Joined
    Sep 2004
    Location
    Melbourne
    Posts
    61
    Location
    I haven't checked, but should it be:

    If ActiveWorkbook.HasPassword = True Then
    instead?

  3. #3
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  4. #4
    VBAX Regular MUSASHI's Avatar
    Joined
    Oct 2004
    Location
    Corpus Christi, Texas (Very South)
    Posts
    44
    btw Tony, I love Magritte!!

  5. #5
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    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

  6. #6
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Try this:

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

  7. #7
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Thanks guys! Much appreciated.

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

  8. #8
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    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

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

  9. #9
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    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...

  10. #10
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    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!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •