Log in

View Full Version : VBA: Keeping track of CheckBoxes count



Shenhav
12-30-2012, 04:05 AM
Hi, I'm new to this forum.

I have a word doc (2003) which allows the user to run a macro to create tables.
Each table contains some form fields which he can write into, and 2 checkboxes, to check Male/Female.
After the file is complete, i need the vba code to scan the file and realize which boxes were checked and to which table they belong.

I tried working with bookmakrs of course, but the problem is that the bookmarks, which were originally given specifically to the checkboxes, refer to the WHOLE CELL.
thus, they don't show the True/False values.

Also, i noticed that when a table is created, another set of bookmarks is automatically given to the checkboxes (beside the bookmakrs which were intentionally given by code).
these bookmarks DO refer to the true/false value, but the number which are given to them is impossible to follow.
This is because a user can decide to insert a table between tables 2 and 3 for example. His new table checkboxes' bookmakrs will get the last numbers, as this example shows:

Before a table was inserted between 3 and 4:
table 1: Check1, Check2
table 2: Check3, Check4,
table 3: Check5, Check6
table 4: Check7, Check8


After a table was inserted between 3 and 4:
table 1: Check1, Check2
table 2: Check3, Check4,
table 3: Check9, Check10
table 4: Check5, Check6
table 5: Check7, Check8


I'm really stuck :help
Do you have any ideas how can I ultimately connect between a checkbox to a table?

macropod
12-30-2012, 05:05 AM
Hi Shenhav,

Checkbox formfields can have their own internal bookmarks (which you set via the chekbox properties), besides which, it is possible to bookmark a cell's contents without bookmarking the whole cell (just make sure the end-of-cell marker isn't selected when applying the bookmark).

In any event, it isn't necessary to work with the bookmarks. Instead, you can simply test the formfields according to the cells they're in. For eaxample:
Sub Demo()
Dim wdCell As Cell, Chk1Count As Long, Chk2Count As Long
With ActiveDocument.Tables(1).Range
For Each wdCell In .Columns(2).Cells
If wdCell.Range.FormFields(1).CheckBox.Value = True Then
Chk1Count = Chk1Count + 1
End If
Next
For Each wdCell In .Columns(3).Cells
If wdCell.Range.FormFields(1).CheckBox.Value = True Then
Chk2Count = Chk2Count + 1
End If
Next
MsgBox Chk1Count & vbTab & Chk2Count
End With
End Sub

fumei
12-30-2012, 11:58 AM
And just as a point of information...

The automatic, default internal bookmarks for formfields are numbered in the order they are created.

While the table index numbers are numbered in the order the table is in the document.

Thus your:

table 1: Check1, Check2
table 2: Check3, Check4,
table 3: Check9, Check10
table 4: Check5, Check6
table 5: Check7, Check8