I have three buttons on a user from that lock different parts of a document dependant on which button is pressed, what I would like to do is ensure my code is as efficient as it can be, can anyone look at the code below and suggest any improvements please, or a way of shortening my code
cheers
gibbo
[VBA] Private Sub CommandButton1_Click()
On Error GoTo Err
ActiveDocument.Unprotect Password:="test"
ActiveDocument.Bookmarks("Log").Range.Text = CreateObject("WScript.Network").UserName _
& " Saved the application at " _
& Format(Now, "hh:mm dd/mm/yy") & vbNewLine
ActiveDocument.Sections(1).ProtectedForForms = True
ActiveDocument.Sections(2).ProtectedForForms = False
ActiveDocument.Sections(3).ProtectedForForms = False
ActiveDocument.Sections(4).ProtectedForForms = True
ActiveDocument.Protect Password:="test", noreset:=False, Type:= _
wdAllowOnlyFormFields
UserForm1.Show
Exit Sub
Err:
MsgBox "This Document cannot be sent, Please ensure you are pressing the correct button", vbCritical + vbOKOnly, "Error"
End Sub
Private Sub CommandButton2_Click()
On Error GoTo Err
If ActiveDocument.Sections(1).ProtectedForForms = True Then
ActiveDocument.Unprotect Password:="test"
ActiveDocument.Bookmarks("Log").Range.Text = CreateObject("WScript.Network").UserName _
& " Saved the application at " _
& Format(Now, "hh:mm dd/mm/yy") & vbNewLine
ActiveDocument.Sections(1).ProtectedForForms = True
ActiveDocument.Sections(2).ProtectedForForms = True
ActiveDocument.Sections(3).ProtectedForForms = False
ActiveDocument.Sections(4).ProtectedForForms = True
ActiveDocument.Protect Password:="test", noreset:=False, Type:= _
wdAllowOnlyFormFields
UserForm2.Show
End If
Exit Sub
Err:
MsgBox " This Document cannot be sent, Please ensure you are pressing the correct button", vbCritical + vbOKOnly, "Error"
End Sub
Private Sub CommandButton3_Click()
On Error GoTo Err
If ActiveDocument.Sections(1).ProtectedForForms = True And ActiveDocument.Sections(2).ProtectedForForms = True Then
ActiveDocument.Unprotect Password:="test"
ActiveDocument.Bookmarks("Log").Range.Text = CreateObject("WScript.Network").UserName _
& " Saved the application at " _
& Format(Now, "hh:mm dd/mm/yy") & vbNewLine
ActiveDocument.Sections(1).ProtectedForForms = True
ActiveDocument.Sections(2).ProtectedForForms = True
ActiveDocument.Sections(3).ProtectedForForms = True
ActiveDocument.Sections(4).ProtectedForForms = True
ActiveDocument.Protect Password:="test", noreset:=False, Type:= _
wdAllowOnlyFormFields
UserForm3.Show
Else
MsgBox "Parts 1 and 2 Must be completed first", vbCritical + vbOKOnly, "Error"
End If
Exit Sub
Err:
MsgBox "This Document cannot be sent, Please ensure you are pressing the correct button", vbCritical + vbOKOnly, "Error", vbCritical + vbOKOnly, "Error"
End Sub [/VBA]