Consulting

Results 1 to 7 of 7

Thread: Solved: Error Handling for Very Simple Macros

  1. #1
    Site Admin
    The Princess
    VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location

    Solved: Error Handling for Very Simple Macros

    [vba]Sub Prot()
    ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="pwd"
    End Sub

    Sub Unprot()
    ActiveDocument.Unprotect Password:="pwd"
    End Sub[/vba]

    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:

    [vba]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[/vba]
    ~Anne Troy

  2. #2
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi DB
    Change your subs to the following
    [VBA]
    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

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    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?
    ~Anne Troy

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Simplest and best would be one button with a caption change from Protect to Unprotect .....but time for bed here
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Sure! Leave me hanging in the lurch.

    Thanks, sweetie!

    I'm gonna mark this one solved anyway.
    ~Anne Troy

  6. #6
    MS Excel MVP VBAX Regular Colo's Avatar
    Joined
    May 2004
    Location
    Kobe, Japan
    Posts
    23
    Location
    Dreamboat, I'm not sure if you like this, because this is not a ToggleControl that you wanted. But please give this a try.

    [VBA]
    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

    [/VBA]

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi DB,
    Try the attached.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •