Anne Troy
03-23-2005, 05:35 PM
Sub Prot()
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="pwd"
End Sub
Sub Unprot()
ActiveDocument.Unprotect Password:="pwd"
End Sub
For people who work on forms a lot, such as small offices, it might be better to have macros to reprotect documents if they unprotected them momentarily for slight changes.
So I made a custom toolbar (yep..don't worry..I had help), and these are the macros it will run. What I want to do is not get errors for the user if they click the wrong button. So, if it's already protected, I don't want the code to yell at them or dump them into the VBE.
Help!!
Here's the entire code, just in case:
Const strMenuName As String = "&Dreamboat's Menu"
Private Sub Document_Open()
'The Dim statements make the rest of the code easier to create.
Dim cmd As CommandBarPopup
Dim mybutton As CommandBarButton
Dim i As Integer
Dim A(2) As Variant
CustomizationContext = ActiveDocument
On Error Resume Next
'This checks if the menu already exists. If it does, it does not create a new one.
'The ampersand (&) in the name of the menu underlines
'the letter that follows it to give
'it a keyboard command (Alt-m) as many menus have.
CommandBars("Menu Bar").Controls(strMenuName).Caption = strMenuName
If Not Err.Number = 0 Then
On Error GoTo 0
'Note that the parts of the array are ("Title of menu option","Macro to Run",
'FaceID for toolbar button)
A(1) = Array("Protect", "Prot", 92)
A(2) = Array("UnProtect", "UnProt", 85)
With CommandBars("Menu Bar").Controls
Set cmd = .Add(Type:=msoControlPopup, Before:=11)
End With
cmd.Caption = strMenuName
For i = 1 To UBound(A)
With cmd.Controls
Set mybutton = .Add(Type:=msoControlButton)
With mybutton
.Caption = A(i)(0)
.OnAction = A(i)(1)
.FaceId = A(i)(2)
End With
End With
Next i
End If
End Sub
Private Sub Document_Close()
'This closes the Templates toolbar when the document is closed. It also keeps the user from
'changing the template. This is what we call an *on-event* procedure (macro) because it is
'run when the document is closed.
On Error Resume Next
CommandBars("Menu Bar").Controls(strMenuName).Delete
End Sub
Sub Prot()
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="pwd"
End Sub
Sub Unprot()
ActiveDocument.Unprotect Password:="pwd"
End Sub
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="pwd"
End Sub
Sub Unprot()
ActiveDocument.Unprotect Password:="pwd"
End Sub
For people who work on forms a lot, such as small offices, it might be better to have macros to reprotect documents if they unprotected them momentarily for slight changes.
So I made a custom toolbar (yep..don't worry..I had help), and these are the macros it will run. What I want to do is not get errors for the user if they click the wrong button. So, if it's already protected, I don't want the code to yell at them or dump them into the VBE.
Help!!
Here's the entire code, just in case:
Const strMenuName As String = "&Dreamboat's Menu"
Private Sub Document_Open()
'The Dim statements make the rest of the code easier to create.
Dim cmd As CommandBarPopup
Dim mybutton As CommandBarButton
Dim i As Integer
Dim A(2) As Variant
CustomizationContext = ActiveDocument
On Error Resume Next
'This checks if the menu already exists. If it does, it does not create a new one.
'The ampersand (&) in the name of the menu underlines
'the letter that follows it to give
'it a keyboard command (Alt-m) as many menus have.
CommandBars("Menu Bar").Controls(strMenuName).Caption = strMenuName
If Not Err.Number = 0 Then
On Error GoTo 0
'Note that the parts of the array are ("Title of menu option","Macro to Run",
'FaceID for toolbar button)
A(1) = Array("Protect", "Prot", 92)
A(2) = Array("UnProtect", "UnProt", 85)
With CommandBars("Menu Bar").Controls
Set cmd = .Add(Type:=msoControlPopup, Before:=11)
End With
cmd.Caption = strMenuName
For i = 1 To UBound(A)
With cmd.Controls
Set mybutton = .Add(Type:=msoControlButton)
With mybutton
.Caption = A(i)(0)
.OnAction = A(i)(1)
.FaceId = A(i)(2)
End With
End With
Next i
End If
End Sub
Private Sub Document_Close()
'This closes the Templates toolbar when the document is closed. It also keeps the user from
'changing the template. This is what we call an *on-event* procedure (macro) because it is
'run when the document is closed.
On Error Resume Next
CommandBars("Menu Bar").Controls(strMenuName).Delete
End Sub
Sub Prot()
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="pwd"
End Sub
Sub Unprot()
ActiveDocument.Unprotect Password:="pwd"
End Sub