Results 1 to 18 of 18

Thread: Word 2007 Checkbox change color when checked

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    5

    Word 2007 Checkbox change color when checked

    Need a little help here. In Word, I have a table with a check box in the first column. What I want to happen is when I check the box have the whole row fill in with a certain color. Can someone provide me with some code for this?

    While searching the forums I saw some on changing font colors. Below is the start of that code, but It wouldn't work.

    Private Sub CheckBox81_Click()
    If CheckBox81.Value = vbChecked Then ForeColor = vbBlue

    End Sub

    Thanks for any help you can provide.

  2. #2
    Hyperactive Member
    Join Date
    Oct 2010
    Location
    Indiana
    Posts
    457

    Re: Word 2007 Checkbox change color when checked

    For starters you need to use the Microsoft Forms 2.0 Checkbox (judging by your code it looks like you are), the regular checkbox does not have the option to change colors.

    Your code is close, Try it like:
    vb Code:
    1. Private Sub CheckBox81_Click()
    2.  
    3.      If CheckBox81.Value = vbChecked Then
    4.           CheckBox81.ForeColor = vbBlue
    5.      End If
    6.  
    7. End Sub

  3. #3
    Hyperactive Member
    Join Date
    Oct 2010
    Location
    Indiana
    Posts
    457

    Re: Word 2007 Checkbox change color when checked

    Correction:
    You may want to change it to a different color if someone unchecks it, because with the above code when they check it it will turn blue and stay blue. So....
    vb Code:
    1. Private Sub CheckBox81_Click()
    2.  
    3.      If CheckBox81.Value = vbChecked Then
    4.           CheckBox81.ForeColor = vbBlue
    5.      Else:
    6.           CheckBox81.ForeColor = vbRed
    7.      End If
    8.  
    9. End Sub
    Replace vbRed with the color you want it to be when they uncheck it.

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    5

    Re: Word 2007 Checkbox change color when checked

    That's great I'll try it and let you know how it works

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    5

    Re: Word 2007 Checkbox change color when checked

    Works great. Thanks!!!

    Next challenge. Is there a way to highlight the entire row say yellow when I check the box?

    Thanks!

  6. #6
    Hyperactive Member
    Join Date
    Oct 2010
    Location
    Indiana
    Posts
    457

    Re: Word 2007 Checkbox change color when checked

    I'll have to look at it later, but here's the code to highlight the selected text.
    Code:
        Selection.Range.HighlightColorIndex = wdYellow

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Word 2007 Checkbox change color when checked

    vb Code:
    1. ThisDocument.Tables(1).Rows(rownum).Range.HighlightColorIndex = wdBlue
    change rownum and table index to suit
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  8. #8

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    5

    Re: Word 2007 Checkbox change color when checked

    thanks guys for the help!! A couple more questions. Below is the code now that I have:

    Private Sub CheckBox81_Click()
    If CheckBox81.Value = vbChecked Then
    CheckBox81.ForeColor = vbBlack
    Else:
    CheckBox81.ForeColor = vbRed
    ThisDocument.Tables(6).Rows(3).Range.HighlightColorIndex = wdYellow

    End If

    End Sub

    The Checkbox 81 is in row 3 of table 6 however it won't highlight that row. It will highlight any other row though. So if I change the row to say 4 then when I check the box in row 3 row 4 highlights. Any ideas?

  9. #9

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    5

    Re: Word 2007 Checkbox change color when checked

    Ok so this document is evolving as I think more and more.

    What would be nice, but not sure if it's possible is when I check one of the boxes have it bring up a table of existing info requesting additional information to the item checked? Make sense?

    I have a 13 page doc already with a table with many check boxes at the beginning of each row. Some of these will require inputs, but I don't want to clutter the doc up with all these tables if folks don't need certain rows checked.

    Let me know what you guys think.

    Thanks for the help!!

  10. #10
    Hyperactive Member
    Join Date
    Oct 2010
    Location
    Indiana
    Posts
    457

    Re: Word 2007 Checkbox change color when checked

    Word is much harder to work with for handeling data, its harder to look up information, and its harder to get it to do what you want. Access and Excel break the data up and allow you to look up/reference it many ways, while keeping a clean user interface.

    If you are wanting a cleaner interface, and have more capabilities, you should look at moving your project to an Excel workbook, or even better, Access. This will allow you to reference/lookup information much easier, and the data can come from hidden sheets/tables that the user will never see if you don't want them to.

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Word 2007 Checkbox change color when checked

    The Checkbox 81 is in row 3 of table 6 however it won't highlight that row.
    i tested this in a single table, without issue

    What would be nice, but not sure if it's possible is when I check one of the boxes have it bring up a table of existing info requesting additional information to the item checked?
    you can completely hide the table and only show again when the checkbox is checked
    vb Code:
    1. Private Sub CheckBox3_Click()
    2. If CheckBox3.Value Then
    3. ThisDocument.Tables(1).Range.Font.Hidden = False
    4. ThisDocument.Tables(1).Rows.Height = 16
    5. CheckBox3.Caption = "Hide Table"
    6.  
    7. Else
    8. ThisDocument.Tables(1).Range.Font.Hidden = True
    9. ThisDocument.Tables(1).Rows.Height = 0
    10. CheckBox3.Caption = "Show Table"
    11. End If
    12. End Sub
    you could also use a toggle button instead of check box
    if the checkbox is within the table then that row needs to be sized to hold /show the checkbox, while the rest of the rows do not show
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  12. #12
    New Member
    Join Date
    Feb 2016
    Posts
    4

    Re: Word 2007 Checkbox change color when checked

    Hi, I'm completely new with programming in MS Word, I know this is an old thread, but can someone explain how I use this code with the visual basic feature in word to get color changing check boxes? Also how do I use the Microsoft Forms 2.0 checkbox?

  13. #13
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Word 2007 Checkbox change color when checked

    forms 2.0 controls are the controls added to userforms or documents (activex controls) in word or excel

    add a checkbox and try the code above, make sure the name of the check box matches the code
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  14. #14
    New Member
    Join Date
    Feb 2016
    Posts
    4

    Re: Word 2007 Checkbox change color when checked

    Quote Originally Posted by westconn1 View Post
    forms 2.0 controls are the controls added to userforms or documents (activex controls) in word or excel

    add a checkbox and try the code above, make sure the name of the check box matches the code
    So I added the code (in the right spot?) When I save it I don't see any difference.. http://screencast.com/t/794pexZ7bhL

    The checkbox I used was in the developer tab: http://screencast.com/t/5ufdnuK9.. how do I know what the name is?

    Sorry for my ignorance.. trying to learn.

  15. #15
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Word 2007 Checkbox change color when checked

    how do I know what the name is?
    right click on it and properties

    So I added the code (in the right spot?)
    to put in the right spot, use the drop down boxes at the top of the code pane, if the event is not there, you are in the wrong code pane
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  16. #16
    New Member
    Join Date
    Feb 2016
    Posts
    4

    Re: Word 2007 Checkbox change color when checked

    So right clicking on the checkbox doesn't show a properties to find the name: http://screencast.com/t/wus5T3dR5JNE Obviously I'm not looking in the right place, I also see the dropboxes in the code pane but what event am I looking for?

    Is there a lesson on how to work in word's VB for complete novices?

  17. #17
    New Member
    Join Date
    Feb 2016
    Posts
    4

    Re: Word 2007 Checkbox change color when checked

    Quote Originally Posted by westconn1 View Post
    right click on it and properties
    So I changed the tag name in the properties of a test checkbox to 'myCheckbox' http://screencast.com/t/6fzOPc7Mwi3e

    when I changed the name in the document code section for the above code I got this message when i tried to run it: http://screencast.com/t/5umqY8G5W

    any other ideas?

  18. #18
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Word 2007 Checkbox change color when checked

    the tag property is not the name property

    if you want people to look at your images, put them in your post, not some external link

    I also see the dropboxes in the code pane but what event am I looking for?
    in the left side one look for the mycheckbox and select, once selected the right side one will have the options for mycheckbox
    if mycheckbox is not in the left side one see if there is any checkbox, as that is probably the correct name for your checkbox, or if no checkbox there the control added is not the correct type of control, that is it could be a form checkbox (which has no events), as different to an activex (forms2) checkbox

    Is there a lesson on how to work in word's VB for complete novices?
    google can help
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width