PDA

View Full Version : Activate macro only if checkbox checked



orjanmen
04-29-2015, 01:14 AM
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?

gmaxey
04-29-2015, 06:01 AM
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

gmayor
04-29-2015, 06:08 AM
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).

orjanmen
05-04-2015, 03:14 AM
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 :-)

gmaxey
05-04-2015, 03:45 AM
I suspect that the userform and the checkbox1 on it are not in scope when you execute TestMacro.

gmayor
05-04-2015, 04:01 AM
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

orjanmen
05-04-2015, 06:34 AM
Hi. Thank you so much Graham.







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.