Results 1 to 8 of 8

Thread: [RESOLVED] Workbook Open - Pasword protect 2 levels

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Location
    NZ
    Posts
    178

    Resolved [RESOLVED] Workbook Open - Pasword protect 2 levels

    Hi there.

    On opening a workbook (XL2003) I want users to enter a password
    which will give them readOnly permissions or access to the whole
    book.

    Here is the code

    VB Code:
    1. Private Sub Workbook_Open()
    2.  
    3. Dim Message, Title, Default, Password As String
    4. Message = "Enter your password" ' Set prompt.
    5. Title = "Password" ' Set title.
    6. Default = " " ' Set default.
    7. ' Display message, title, and default value.
    8. Password = InputBox(Message, Title, Default)
    9.  
    10. If Password = "test" Then
    11. Workbooks.ReadOnly = True
    12. Else
    13. Workbooks.ReadOnly = False
    14. End If
    15.  
    16. End Sub
    an error occurs on "Workbooks.ReadOnly = True" line.

    I also want to put an errorchecking code for invalid passwords.

    I know I can use the "save as" option but that is not what I need.

    Cheers

    Lionel DownUnda
    Last edited by lionelnz; Jan 21st, 2007 at 08:28 PM. Reason: VBCode
    Lionel DownUnda (NZ)
    XP Prof SP3, Office 2002 SP3
    If the universe is expanding what is it expanding into?

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Workbook Open - Pasword protect 2 levels

    The Open method of the workbooks collection has a Password property so no need to handle it all yourself.

    Workbooks.ReadOnly error because no workbook is open yet.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Location
    NZ
    Posts
    178

    Angry Re: Workbook Open - Pasword protect 2 levels

    Thanks for the quick reply.

    If you are referring to the "save as" option it is not suitable for my needs & here is why.

    I have about 10 staff who need to fill in timesheets using XL SS on a public folder on the server. The staff need full access & management such as myself need to be able to view (read only) the timesheet without the ability to change data. Using the "save as" function the staff need to input 2 passwords.

    The staff (in general) are not overly computer literate so I was hoping to be able to write code so that only one password is entered and depending on the password gives the user fullaccess or readonly. This will also save management having to remember different passwords for readonly access to different staff files.

    Lionel downunda
    Lionel DownUnda (NZ)
    XP Prof SP3, Office 2002 SP3
    If the universe is expanding what is it expanding into?

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Workbook Open - Pasword protect 2 levels

    Why would you need to enter in 2 pwds? With the Open function you only supply the pwd once. If you dont have it then it can be opened as read only. Once the workbook is saved with a pwd, it is only needed again to open it on the next open.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Location
    NZ
    Posts
    178

    Re: Workbook Open - Pasword protect 2 levels

    Quote Originally Posted by RobDog888
    If you dont have it then it can be opened as read only. Once the workbook is saved with a pwd, it is only needed again to open it on the next open.
    And that is the problem. The purpose of the workbook is a timesheet for about 10 staff. The wkbk is saved to a very public folder and some staff are sensitive about who sees their timesheets. (the workbook will have timesheets for 12 months set up with one month being in one worksheet so their wil be 12 wksheets at least for the work book.)

    Therefore I need one password that will let managemnet read only (only mmt will have that password) & the individual staff wil have their own password which will give access to their own timesheet workbboks.

    I have been thinking about a creative solution & here is rough psuedocode

    Workbook has all but one insignificant sheet XlVeryHidden
    On workbook open event
    Enter password
    if password = "readonly" then
    unhide appropriate sheets
    make sheets readonly
    Else password = "read/write access"
    unhide appropriate sheets
    make sheets read/write access
    End if

    If I have XLVeryhidden when opening the workbook, I wouldn't need Before_save event to make sheets XLVeryHidden again should I?

    Do you think this would work?

    Key points to remember are that it is a simple spreadsheet that does not hold overly sensitive data and that staff are not overly PC Literate.

    Your feedback is appreciated.
    Last edited by lionelnz; Jan 22nd, 2007 at 07:49 PM.
    Lionel DownUnda (NZ)
    XP Prof SP3, Office 2002 SP3
    If the universe is expanding what is it expanding into?

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Location
    NZ
    Posts
    178

    Unhappy Re: Workbook Open - Pasword protect 2 levels

    There is one workbook with 12 sheets (one for every month).

    I want to be able to have one password only that a user enters and depending on the password gives the user read/write access OR read only.

    The key is that the workbook has to be unavailble (protected) unless the user enters the right password for a) full access or b) read only.

    I want only one password to be used at any time and this is why the "save as" option is not useful for the end users in my case.

    Cheers anyway
    Lionel DownUnda (NZ)
    XP Prof SP3, Office 2002 SP3
    If the universe is expanding what is it expanding into?

  7. #7
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Workbook Open - Pasword protect 2 levels

    Okay

    Here is a general idea on how you can go about it. i can think of two ways

    WAY 1) When a user opens the workbook he/she is prompted for a password. Lets say the user enters "admin" or "guestpass" for a password depending on the password we can assign the user read/write or read only access

    For this method we will use 13 sheets in a workbook. 12 for months and 1 for hiding the password data.

    In sheet1
    In cell A1 type USER and B1 type PASSWORD
    In cell A2 type ADMINISTRATOR and B2 type admin
    In cell A3 type GUEST and B3 type guestpass

    Now create a form and insert three controls. A Combobox, A textbox and 1 commandbutton.

    Set the combobox rowsource property to sheet1!A2:A3
    Set the textbox password char to '*'

    Now rest is writing a code for password checking which i am sure you can do that. compare the values of textbox with the range("B2").value or range("B3").value

    In the workbook open event make the userform visible

    WAY 2) This is the 2nd way. see the file attached

    Hope I have understood you correctly and it helps...

    Note: I have just shown you the basic approach. The coding part is yours

    [Edit 25/03/2007] : Since the post is resolved.. deleting the attachment as my total attachements have crossed the allowed limit
    Last edited by Siddharth Rout; Mar 25th, 2007 at 08:32 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Location
    NZ
    Posts
    178

    Resolved Re: Workbook Open - Pasword protect 2 levels

    Thanks for this. I certainly like the idea of a userform. I needed something by tomorrow & so this is what I have for now.
    Can I use masking for user password input box in VBA (without userform)?
    If so how?

    VB Code:
    1. Private Sub Workbook_Open()
    2. 'Provide 3 level protection
    3. 'by using only one password
    4.  
    5. Dim Password As String
    6. Dim sh As Worksheet
    7. Worksheets("Sheet1").Activate
    8. 'Protect & hide all but 1st wksht
    9. For Each sh In Worksheets
    10.     If (sh.Name <> "Sheet1") Then
    11.     sh.Protect
    12.     sh.Visible = xlSheetVeryHidden
    13.     End If
    14. Next sh
    15.  
    16. 'Ask for user password
    17. Password = InputBox("Password")
    18.  
    19.  
    20. 'Tutor password unprotect sheets & unlock cells
    21.  
    22.     If (Password = "t1") Then
    23.     For Each sh In Worksheets
    24.         If (sh.Name <> "Sheet1") Then
    25.             sh.Visible = xlSheetVisible
    26.             sh.Unprotect
    27.             sh.Range("A3:C4").Locked = False
    28.             sh.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    29.             sh.EnableSelection = xlUnlockedCells
    30.         End If
    31.       Next sh
    32.            
    33. 'Management pwrd to read only
    34.     ElseIf (Password = "t2") Then
    35.         For Each sh In Worksheets
    36.             If (sh.Name <> "Sheet1") Then
    37.                 sh.Visible = xlSheetVisible
    38.             End If
    39.             sh.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    40.             sh.EnableSelection = xlNoSelection
    41.         Next sh
    42.                
    43. 'Supervisor/developer pwrd to maintain wkbk
    44.     ElseIf (Password = "s") Then
    45.         For Each sh In Worksheets
    46.             sh.Visible = xlSheetVisible
    47.             sh.Unprotect
    48.         Next sh
    49.     End If
    50. End Sub

    If you can suggest any code tidying up I would be grateful.
    Lionel DownUnda (NZ)
    XP Prof SP3, Office 2002 SP3
    If the universe is expanding what is it expanding into?

Posting Permissions

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



Click Here to Expand Forum to Full Width