PDA

View Full Version : Solved: Error Handling for Very Simple Macros



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

mdmackillop
03-23-2005, 06:13 PM
Hi DB
Change your subs to the following

Sub Prot()
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="pwd"
End If
End Sub

Sub Unprot()
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:="pwd"
End If
End Sub

Anne Troy
03-23-2005, 06:23 PM
:) Thanks! I had something VERY similar, but it was combined with other code, so I wasn't sure what all belonged.

Now...could you help me edit the other code to just put TWO toolbar buttons on the STANDARD toolbar instead of on the menu? I hate to be so pompous to put my name on the menu. LOL!! OR...perhaps just add another toolbar buttons toolbar?

mdmackillop
03-23-2005, 06:28 PM
Simplest and best would be one button with a caption change from Protect to Unprotect .....but time for bed here:sleeping:

Anne Troy
03-23-2005, 06:30 PM
Sure! Leave me hanging in the lurch.

Thanks, sweetie!

I'm gonna mark this one solved anyway. :)

Colo
03-24-2005, 12:59 AM
Dreamboat, I'm not sure if you like this, because this is not a ToggleControl that you wanted. But please give this a try.


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 a As String

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.

'++ Just in case - Colo
CommandBars("Menu Bar").Controls(strMenuName).Delete

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 = "Protect"

With CommandBars("Menu Bar").Controls
Set cmd = .Add(Type:=msoControlPopup, Before:=11)
End With
cmd.Caption = strMenuName

With cmd.Controls
Set mybutton = cmd.Controls.Add(Type:=msoControlButton)
With mybutton
.Caption = a
.OnAction = "PseudoToggle"
.Parameter = .Caption
End With
End With
End If
End Sub
Private Sub PseudoToggle()
With Application.CommandBars("Menu Bar").Controls(strMenuName)
If .Controls(1).State <> msoButtonDown Then
.Controls(1).State = msoButtonDown
Prot
Else
.Controls(1).State = msoButtonUp
Unprot
End If
End With
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

mdmackillop
03-24-2005, 04:26 PM
Hi DB,
Try the attached.