PDA

View Full Version : Word Check Boxs.



Buccaneer66
01-13-2015, 03:01 AM
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.

12710

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

12711

Please ignore the table content they are just test values.

Can anyone help please?

gmayor
01-13-2015, 03:22 AM
What sort of check boxes have you inserted? Form fields? ActiveX? Content Controls?

Buccaneer66
01-13-2015, 03:33 AM
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.

gmayor
01-13-2015, 07:18 AM
See http://word.mvps.org/faqs/tblsfldsfms/ExclusiveFmFldChbxs.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

Buccaneer66
01-13-2015, 08:23 AM
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?

macropod
01-13-2015, 06:20 PM
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

Buccaneer66
01-14-2015, 12:36 AM
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?

gmayor
01-14-2015, 01:47 AM
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.

Buccaneer66
01-16-2015, 02:38 AM
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.

Buccaneer66
01-16-2015, 04:06 AM
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

gmayor
01-16-2015, 06:12 AM
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

Buccaneer66
01-16-2015, 06:53 AM
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.