#1
|
|||
|
|||
Macro to run when a checkbox is selected
Hello. I have created a word document (2010) using content control boxes. Users have a an option to check a box besides each of the following:
Resolution Information Direction What I would like to happen is if a user checks the box Resolution the next line would begin with the word "THAT" and they type the resolution. If the user checks the box for either Information or Direction, the next line would start with the word "Staff" and then they can keep typing. So for example, if a user select Resolution, on the next line the text would be something like this: THAT authority is given to proceed with the purchase. If they select Information or Resolution, the next line would begin with Staff submit this report as information..... Is this possible? I am not an expert and am learning as I go. I am thinking a macro might work. Thank you. Cathy |
#2
|
|||
|
|||
Unfortunately CCs don't have a built-in change event. You can create custom change events however they can be intimidating and unwieldy:
http://gregmaxey.com/word_tip_pages/...om_events.html About the best you can do is employ the built-in On_Exit event where the event occurs when the user checks then tabs or clicks to the next control. What is the range identifier of your "next line?" Is it another titled content control? |
#3
|
|||
|
|||
Hello. Thank you for replying. The next line is just the start of a paragraph. After the user selects one if the check boxes they will put their cursor a the beginning of a new blank line.
Cathy |
#4
|
|||
|
|||
Put the following code in the ThisDocument module of your VB Project:
Code:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean) Dim oRng As Word.Range Set oRng = ContentControl.Range.Paragraphs(1).Next.Range Select Case ContentControl.Title Case "A" 'Change to match your titles If ContentControl.Checked Then oRng.Text = "XXXX " oRng.Collapse wdCollapseEnd oRng.Select End If Case "B" 'Change to match your titles If ContentControl.Checked Then oRng.Text = "YYYY " oRng.Collapse wdCollapseEnd oRng.Select End If Case "C" 'Change to match your titles If ContentControl.Checked Then oRng.Text = "ZZZZ " oRng.Collapse wdCollapseEnd oRng.Select End If End Select lbl_Exit: Exit Sub End Sub |
#5
|
|||
|
|||
Thank you. As I am learning what will this code do and do I need to make changes to it.
Thank you. Cathy |
#6
|
|||
|
|||
After you change it to suit your specific need, it will do what you asked for it to do with the understanding that it is not automatic based on simply checking the box. The user has to check the box and then exit that box.
If you read the code you will discover what changes you need to make to it. To be honest, you have choose a rather poor design. Check boxes are not mutually exclusive so there is nothing to keep your users from checking all three. |
#7
|
|||
|
|||
Perfect thank you so much for your help Greg. This works. Just for reference in the future what would be the better option than using check boxes to achieve the same results?
Cathy |
#8
|
|||
|
|||
The easiest way to ensure that only one choice is selected is to use a dropdown list. So instead of 3 checkboxes, use a dropdown list with three choices.
|
#9
|
|||
|
|||
That totally makes sense. Again, many thanks. Enjoy your day.
Cathy |
#10
|
|||
|
|||
Perhaps more elegant (but much more complicated) is a custom change event.
See: http://gregmaxey.com/word_tip_pages/...n_buttons.html |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Clicking the selected Content Control checkbox returns wrong control in vba event | DougsGraphics | Word VBA | 2 | 06-24-2015 07:31 AM |
Use a checkbox and a macro to paste text | destdixon | Word | 1 | 01-14-2013 07:29 AM |
Asking for Macro to Reset Checkbox | Control | Excel Programming | 4 | 12-11-2012 07:24 AM |
Macro to calculate a value of a checkbox. | stephen_ | Excel Programming | 1 | 06-27-2012 04:57 PM |
macro on checkbox | macrohelp | Word VBA | 0 | 03-06-2009 03:33 PM |