Consulting

Results 1 to 12 of 12

Thread: Word Check Boxs.

  1. #1

    Word Check Boxs.

    I have a set of 4 checkboxs which when checked I'd like to use the result to alter a word table, but how do you retrieve the value of a word checkbox & use it?

    The Word table I have working fine with nested IF statements, but I would like to use the values from the checkboxs in the IF statements to set the table content.

    This is the checkboxs.

    Capture.JPG

    This table & other similar need to have the content changing depend which checkbox is ticked.

    Capture1.JPG

    Please ignore the table content they are just test values.

    Can anyone help please?

  2. #2
    What sort of check boxes have you inserted? Form fields? ActiveX? Content Controls?
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    I've used the legacy form field check boxes in Word 2007, & only one of the check boxes will ever be checked at one time.

    I have made it work fine populating the table using a dropdown, but for people using this form a checkbox is better.
    Last edited by Buccaneer66; 01-13-2015 at 04:27 AM.

  4. #4
    See http://word.mvps.org/faqs/tblsfldsfm...FmFldChbxs.htm.

    You can read the value of a chackbox using VBA e.g.

    Dim oFld As FormField
    Set oFld = ActiveDocument.FormFields("Check1")
    MsgBox oFld.CheckBox.Value
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    Thank you that worked perfectly.

    I'm not very good at VBA how can I now use that value in an IF statement so that I can set values in my table depending which checkbox is ticked?

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    For that, you could use code like:
    Sub MakeCheckBoxesExclusive()
    Dim oField As FormField, i As Long, j As Long
    For Each oField In Selection.Frames(1).Range.FormFields
      With oField
        If .Type = wdFieldFormCheckBox Then
          i = i + 1
          If .Range.Start = Selection.FormFields(1).Range.Start Then
            j = i
            .CheckBox.Value = True
          Else
            .CheckBox.Value = False
          End If
        End If
      End With
    Next oField
    Select Case j
      Case 1
      'Do whatever is required if the 1st checkbox is checked
      MsgBox "Do whatever is required if the 1st checkbox is checked"
      Case 2
      MsgBox "Do whatever is required if the 2nd checkbox is checked"
      Case 3
      MsgBox "Do whatever is required if the 3rd checkbox is checked"
      Case Else
      MsgBox "Do whatever is required if any other checkbox is checked"
    End Select
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    Thank you

    When I run that code it gave a syntax error for this line Dim oField As FormField, i As Long, j As Long For Each oField In Selection.Frames(1).Range.FormFields

    To fill in my word table in each cell I've put a nested IF statement so that depending on which checkbox is checked it puts different data into the cell, is this the best way of doing it?

  8. #8
    That sequence is two lines. There should be a line break before "For Each" - see http://www.gmayor.com/installing_macro.htm .
    The macro processes the document according to what is selected. In Paul's example that is to display a message box, but you could use it to write a value to the cell directly. You could use code like that in the attached example document to fill the table cell (A1 in the example) with whatever text you require. The code works using an entry and an exit macro on each of the check boxes, to establish the value of the checkbox and process accordingly. It works when you tab out of the fields.
    Attached Files Attached Files
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  9. #9
    Thank you

    I've just got back to this after a few days & when I run the code I get an error.

    Run-Time error '5941':
    The Requested member of the collection does not exist.
    The debugger points to this line
    For Each oField In Selection.Frames(1).Range.FormFields
    .

    It's obviously me doing something wrong.
    Sorry to keep bugging you guys.

  10. #10
    Silly me I just spotted your example & got it working.

    Because my document has up to a dozen tables all dependent on what box is ticked, I tried to use an IF statement in my table cells that referenced a bookmarked cell containing the result from your macro, trouble is it book marks the cell structure as well.
    Is there a way to maybe right the values from the macro to a docvariable field that I can use in an IF statement?

    Chis

  11. #11
    Using my example in the previous attachment, lose the references to oRng and replace with

       Select Case CheckBoxName
            Case Is = "Check1"
                ActiveDocument.Variables("varName").Value = "This is the text associated with CheckBox 1"
            Case Is = "Check2"
                ActiveDocument.Variables("varName").Value = "This is the text associated with CheckBox 2"
            Case Is = "Check3"
                ActiveDocument.Variables("varName").Value = "This is the text associated with CheckBox 3"
            Case Is = "Check4"
                ActiveDocument.Variables("varName").Value = "This is the text associated with CheckBox 4"
            Case Is = ""
                ActiveDocument.Variables("varName").Value = " "
        End Select
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  12. #12
    Thank you so much Graham.

    I now have things working as I want it, although it's going to take me a while set out all the rables.

Posting Permissions

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