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
Bookmarks