Consulting

Results 1 to 7 of 7

Thread: Activate macro only if checkbox checked

  1. #1
    VBAX Newbie
    Joined
    Apr 2015
    Posts
    3
    Location

    Question Activate macro only if checkbox checked

    Hi. Go a issue I would like help to resolve.

    If i press ":" then all text to the left gets bold. This macro is golden at work, but some of us don't like it. I would like to have a dialogue box where I need to check a checkbox to activate this macro.

    IF checkbox checked THEN run macro IF pressed ":".

    Is this possible?

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,337
    Location
    I assume ":" I a keyboard shortcut to run some "named" macro.

    No you can put a condition on a shortcut. The shortcut is either assigned to the macro or it isn't.

    You can modify the "named" macro e.g.

    Sub NamedMacro()
    If Not Checkbox.Checked then Exit Sub
    'The rest of your code.

    End If
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    I would imagine a colon used as a keyboard shortcut would be very irritating to anyone using correct punctuation in a document. It would make far more sense to use a shortcut that is not used in normal everyday language to call the macro. ALT+Shift+Colon should be free.

    Better still don't provide the macro to users who don't want it.

    Otherwise users are going to have to opt in or opt out of using it, and in that case you would have to modify the macro to look for something in the user's normal template such as a docvariable value and only run the macro if the docvariable is present (or missing).
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  4. #4
    VBAX Newbie
    Joined
    Apr 2015
    Posts
    3
    Location
    Quote Originally Posted by gmaxey View Post
    Sub NamedMacro()
    If Not Checkbox.Checked then Exit Sub
    'The rest of your code.

    End If
    Thanks Gmaxey.

    Now I have made a Checkbox (Checkbox1) in a userform (Userform1). I have put your macro on top of existing macro (The macro makes three line shift if i press hotkey F9). It looks like this:
    Sub TestMacro ()
    If Not Checkbox1.Checked Then Exit Sub
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.TypeParagraph
    End Sub
    When I run this macro, an error occurs saying "variable not defined" and reffers to Checkbox1.

    What may be wrong?



    Gmayor: This document in not used for day to day writing, but for interpreting from spoken language to text. In this job correct punctation don't apply :-)

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,337
    Location
    I suspect that the userform and the checkbox1 on it are not in scope when you execute TestMacro.
    Greg

    Visit my website: http://gregmaxey.com

  6. #6
    If the checkbox is in a userform then you have to call the userform from your macro in order to determine if it is checked. That was not what Greg envisaged and would hardly be convenent.

    Ideally you need three macros. 1 to set a variable that records whether to run the macro. 2. to run the macro and 3. to reset

    Run the Test macro and make a choice, then run it again.

    Option Explicit
    
    Function RunMyMacro() As Boolean
    Dim oVar As Variable
    Dim bVar As Boolean
        For Each oVar In ThisDocument.Variables
            If oVar.name = "varRunMacro" Then
                bVar = True
                Exit For
            End If
        Next oVar
        If Not bVar Then
            If MsgBox("Run the macro?", vbYesNo) = vbYes Then
                ThisDocument.Variables("varRunMacro").Value = True
            Else
                ThisDocument.Variables("varRunMacro").Value = False
            End If
        End If
        RunMyMacro = CBool(ThisDocument.Variables("varRunMacro").Value)
    lbl_Exit:
        Exit Function
    End Function
    
    Sub Reset()
    Dim oVar As Variable
        For Each oVar In ThisDocument.Variables
            If oVar.name = "varRunMacro" Then
                oVar.Delete
                Exit For
            End If
        Next oVar
    lbl_Exit:
        Exit Sub
    End Sub
    
    Sub Test()
        If RunMyMacro Then
            Selection.TypeParagraph
            Selection.TypeParagraph
            Selection.TypeParagraph
        End If
    lbl_Exit:
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  7. #7
    VBAX Newbie
    Joined
    Apr 2015
    Posts
    3
    Location
    Hi. Thank you so much Graham.


    Quote Originally Posted by gmayor View Post

        If Not bVar Then
            If MsgBox("Run the macro?", vbYesNo) = vbYes Then
                ThisDocument.Variables("varRunMacro").Value = True
    This one worked great if I wanted a message box to turn the macro on/off. But I am thinking about expanding the function to work on many macros. That means the user must choose on/off the first time he press the hotkey on every macro. I would rather have a list of all the macros in a Userform, so the user could switch the macro on/off before he starts interpreting (f.ex. with checkbox).


    Once again, thank you for your time.

Posting Permissions

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