+ Reply to Thread
Results 1 to 4 of 4

password to save

Hybrid View

  1. #1
    Registered User
    Join Date
    03-05-2010
    Location
    Nottingham, England
    MS-Off Ver
    Excel 2003
    Posts
    4

    password to save

    I need to create some code to prompt a user to provide a password to save/save as. I have a costing model on excel which needs to be shared by our sales reps. on our network server, but not saved anywhere else.

    Apologies if already posted, but i have been trawling the site and found nothing suitable

    Thanks in advance

    The Cat

  2. #2
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: password to save

    I think this is what you want. It asks the user for a password right before the file is saved. Put it in the thisWorkBook object of VBA.
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
        Dim thePass As String
        thePass = Application.InputBox("Enter Password", "Password Request", "", , , , , 2)
        Me.Password = thePass
    End Sub
    Bob
    Click my star if my answer helped you. Mark the thread as [SOLVED] if it has been.

  3. #3
    Registered User
    Join Date
    03-05-2010
    Location
    Nottingham, England
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: password to save

    Thanks Blane
    Although this code askes for the same password to open and the prompt to save can be cancelled and the file saved anyway

  4. #4
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: password to save

    here's a version that allows you to cancel the save. The first executable line will skip
    asking for a password if there already is one. You can comment that out if you wish.
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
        
        ' if the file already has a password, then just proceed with save
        If (Me.Password <> "") Then Exit Sub
        
        Dim thePass As Variant
        
        ' ask the user for the password for this file
        thePass = Application.InputBox("Enter Password", "Password Request", "", , , , , 2)
        
        ' if InputBox returns a string value, then the user
        ' provided a password, so save the password and
        ' do not cancel the save action
        If (VarType(thePass) = vbString) Then
            Me.Password = thePass
            Cancel = False
            
        ' user canceled the password dialog - cancel the save action
        Else
            Cancel = True
        End If
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1