G
Greg Maxey
I have been tinkering with some code for requiring a more robust password
for Word documents. I am not a computer security expert so my use of "more
robust" may be laughable. The goal is to require a password from 5 to 10
characters long that includes at least one upper case letter, lower case
leter, number and special character. The code shown below seems to be
working as desired. It was put together piecemeal without a lot of thought
given to the clearest path from A to Z. It could be fraught with poor
practices, inefficiencies, or even clear no-nos.
Appreciate any constuctive critical comments that would point any of those
out. Thanks.
Sub SetRobustPassword()
Dim pStr As String
Dim pStrConfirm As String
Dim bValidated As Boolean
Dim i As Long
Dim pInvalidStr As String
Dim bValidLen As Boolean, bUC As Boolean, bLC As Boolean, bNum As Boolean,
bSC As Boolean
Do
'Set all boolean variables to false
bValidated = False: bValidLen = False: bUC = False: bLC = False: bNum =
False: bSC = False
'Define the password
pStr = InputBox("Enter your password." & vbCr + vbCr & "The password " _
& "must contain 5 to 10 characters." & vbCr & vbCr & "It must
include one upper case " _
& "character, one lower case character, one numerical character, "
_
& "and one of the following special characters: @#$%*.", _
"Create Password")
If StrPtr(pStr) = 0 Then Exit Do 'User clicked cancel
'Validate password meets parameters
For i = 1 To Len(pStr)
'Verify proper length
If i = 1 Then
Select Case Len(pStr)
Case Is < 5
MsgBox "Your password is too short. The password must contain
between five and ten characters. " _
& "Please try again.", vbInformation + vbOKOnly, "Password Invalid"
Exit For
Case Is > 10
MsgBox "Your password is too long. The password must contain
between five and ten characters. " _
& "Please try again.", vbInformation + vbOKOnly, "Password Invalid"
Exit For
Case Else
bValidLen = True
End Select
End If
'When condition met set boolean variable to true
If Mid(pStr, i, 1) Like "[A-Z]" Then bUC = True
If Mid(pStr, i, 1) Like "[a-z]" Then bLC = True
If Mid(pStr, i, 1) Like "[0-9]" Then bNum = True
If InStr("@#$%*", Mid(pStr, i, 1)) > 0 Then bSC = True
'If all conditions met set boolean variable to true
If bUC And bLC And bNum And bSC Then
bValidated = True
Exit For
End If
Next i
'Notify user of invalid conditions
If Not bValidated And bValidLen Then
pInvalidStr = "The password must contain"
If Not bUC Then pInvalidStr = pInvalidStr & " at least one UCase
character,"
If Not bLC Then pInvalidStr = pInvalidStr & " at least one LCase
character,"
If Not bNum Then pInvalidStr = pInvalidStr & " at least one numerical
character,"
If Not bSC Then pInvalidStr = pInvalidStr & " at least one special
character (@#$%*),"
'Clean up the string
pInvalidStr = Left(pInvalidStr, Len(pInvalidStr) - 1) & "."
On Error Resume Next
pInvalidStr = Left(pInvalidStr, InStrRev(pInvalidStr, ",") - 1) & " and"
& Mid(pInvalidStr, InStrRev(pInvalidStr, ",") + 1)
MsgBox pInvalidStr
pInvalidStr = ""
'Confirm valid passwords
ElseIf bValidated Then
pStrConfirm = InputBox("Re-enter your password.", "Confirm Password")
If pStrConfirm <> pStr Then
bValidated = False
MsgBox "Your password was not confirmed. Please try again.",
vbInformation + vbOKOnly, "Confirmation Failed"
End If
End If
Loop Until bValidated
'Set password
If bValidated Then
'ActiveDocument.Password = pStr 'STETTED for testing purposes.
'ActiveDocument.Save
MsgBox "Your password has been set."
End If
End Sub
--
Greg Maxey
See my web site http://gregmaxey.mvps.org
for an eclectic collection of Word Tips.
"It is not the critic who counts, not the man who points out how the strong
man stumbles, or where the doer of deeds could have done them better. The
credit belongs to the man in the arena, whose face is marred by dust and
sweat and blood, who strives valiantly...who knows the great enthusiasms,
the great devotions, who spends himself in a worthy cause, who at the best
knows in the end the triumph of high achievement, and who at the worst, if
he fails, at least fails while daring greatly, so that his place shall never
be with those cold and timid souls who have never known neither victory nor
defeat." - TR
for Word documents. I am not a computer security expert so my use of "more
robust" may be laughable. The goal is to require a password from 5 to 10
characters long that includes at least one upper case letter, lower case
leter, number and special character. The code shown below seems to be
working as desired. It was put together piecemeal without a lot of thought
given to the clearest path from A to Z. It could be fraught with poor
practices, inefficiencies, or even clear no-nos.
Appreciate any constuctive critical comments that would point any of those
out. Thanks.
Sub SetRobustPassword()
Dim pStr As String
Dim pStrConfirm As String
Dim bValidated As Boolean
Dim i As Long
Dim pInvalidStr As String
Dim bValidLen As Boolean, bUC As Boolean, bLC As Boolean, bNum As Boolean,
bSC As Boolean
Do
'Set all boolean variables to false
bValidated = False: bValidLen = False: bUC = False: bLC = False: bNum =
False: bSC = False
'Define the password
pStr = InputBox("Enter your password." & vbCr + vbCr & "The password " _
& "must contain 5 to 10 characters." & vbCr & vbCr & "It must
include one upper case " _
& "character, one lower case character, one numerical character, "
_
& "and one of the following special characters: @#$%*.", _
"Create Password")
If StrPtr(pStr) = 0 Then Exit Do 'User clicked cancel
'Validate password meets parameters
For i = 1 To Len(pStr)
'Verify proper length
If i = 1 Then
Select Case Len(pStr)
Case Is < 5
MsgBox "Your password is too short. The password must contain
between five and ten characters. " _
& "Please try again.", vbInformation + vbOKOnly, "Password Invalid"
Exit For
Case Is > 10
MsgBox "Your password is too long. The password must contain
between five and ten characters. " _
& "Please try again.", vbInformation + vbOKOnly, "Password Invalid"
Exit For
Case Else
bValidLen = True
End Select
End If
'When condition met set boolean variable to true
If Mid(pStr, i, 1) Like "[A-Z]" Then bUC = True
If Mid(pStr, i, 1) Like "[a-z]" Then bLC = True
If Mid(pStr, i, 1) Like "[0-9]" Then bNum = True
If InStr("@#$%*", Mid(pStr, i, 1)) > 0 Then bSC = True
'If all conditions met set boolean variable to true
If bUC And bLC And bNum And bSC Then
bValidated = True
Exit For
End If
Next i
'Notify user of invalid conditions
If Not bValidated And bValidLen Then
pInvalidStr = "The password must contain"
If Not bUC Then pInvalidStr = pInvalidStr & " at least one UCase
character,"
If Not bLC Then pInvalidStr = pInvalidStr & " at least one LCase
character,"
If Not bNum Then pInvalidStr = pInvalidStr & " at least one numerical
character,"
If Not bSC Then pInvalidStr = pInvalidStr & " at least one special
character (@#$%*),"
'Clean up the string
pInvalidStr = Left(pInvalidStr, Len(pInvalidStr) - 1) & "."
On Error Resume Next
pInvalidStr = Left(pInvalidStr, InStrRev(pInvalidStr, ",") - 1) & " and"
& Mid(pInvalidStr, InStrRev(pInvalidStr, ",") + 1)
MsgBox pInvalidStr
pInvalidStr = ""
'Confirm valid passwords
ElseIf bValidated Then
pStrConfirm = InputBox("Re-enter your password.", "Confirm Password")
If pStrConfirm <> pStr Then
bValidated = False
MsgBox "Your password was not confirmed. Please try again.",
vbInformation + vbOKOnly, "Confirmation Failed"
End If
End If
Loop Until bValidated
'Set password
If bValidated Then
'ActiveDocument.Password = pStr 'STETTED for testing purposes.
'ActiveDocument.Save
MsgBox "Your password has been set."
End If
End Sub
--
Greg Maxey
See my web site http://gregmaxey.mvps.org
for an eclectic collection of Word Tips.
"It is not the critic who counts, not the man who points out how the strong
man stumbles, or where the doer of deeds could have done them better. The
credit belongs to the man in the arena, whose face is marred by dust and
sweat and blood, who strives valiantly...who knows the great enthusiasms,
the great devotions, who spends himself in a worthy cause, who at the best
knows in the end the triumph of high achievement, and who at the worst, if
he fails, at least fails while daring greatly, so that his place shall never
be with those cold and timid souls who have never known neither victory nor
defeat." - TR