PDA

View Full Version : [SOLVED:] Rename checkbox bookmarks in word



GoKats78
07-23-2014, 10:38 AM
I have a word document with close to 200 check boxes. I use a bit of code to count the number of check boxes checked. I am modifying the document - adding, deleting and moving some check boxes around.
I need a method to rename the bookmarks for each of the check boxes beginning with Check1

12014

macropod
07-24-2014, 04:55 AM
What kind of checkboxes? Apart from which, I can't see why you need to worry about the bookmarks - you should be able to add/delete/relocate and test the checkboxes irrespective of whether they have any bookmarks.

GoKats78
07-24-2014, 07:50 AM
I have code that counts the "checked" checkboxes and uses the bookmark names to do so..

They are used on an entry form (for a horse show) ..the code counts the classes entered and is part calcualtion of the total cost.
Different sections are totaled differently.

macropod
07-24-2014, 02:56 PM
None of that requires bookmarks and, as you're now finding, relying on bookmarks makes the document more difficult to maintain. You still haven't answered my question...

GoKats78
07-24-2014, 04:04 PM
OK..so if I don't use the bookmark name...how do I count the number of specific checkboxes that are checked?

macropod
07-24-2014, 05:33 PM
how do I count the number of specific checkboxes that are checked?
By looping through them and testing them. You've already said they're organised by Section, so that looping can be done on a Section-by-Section basis.

You still haven't answered my question...

GoKats78
07-25-2014, 02:52 AM
What kind of check boxes? that question? I guess i din't realize there were different kinds.

here's a section of my code...I think you have a better solution than i am using.


Sub CheckedNo()Dim Count, n, aVar, num As Integer
Count = 0
For n = 1 To 124 'number of check boxes
If ActiveDocument.FormFields("Check" & n).CheckBox.Value = True Then
Count = Count + 1
End If
Next n
For Each aVar In ActiveDocument.Variables
If aVar.Name = "Count" Then num = aVar.Index
Next aVar
If num = 0 Then
ActiveDocument.Variables.Add Name:="Count", Value:=Count
Else
ActiveDocument.Variables(num).Value = Count
End If
End Sub

macropod
07-25-2014, 04:23 PM
You are using formfield checkboxes; other kinds include ActiveX and content control checkboxes.

Instead of the code you are now using, you could use:

Sub CheckedNo()
Dim Count, n As Long
With ActiveDocument
For n = 1 To .FormFields.Count
With .FormFields(n)
If .Type = wdFieldFormCheckBox Then
If .CheckBox.Value = True Then
Count = Count + 1
End If
End If
End With
Next n
On Error Resume Next
.Variables.Add Name:="Count", Value:=Count
On Error GoTo 0
.Variables("Count").Value = Count
End With
End Sub
PS: you previously mentioned using Sections, but there's nothing to do with Sections in your code.

GoKats78
07-28-2014, 10:44 AM
I just posted one part of the code...that counted one section of the document (boxes 1-124). I use the same code for the other sections, using different "Dims" and checkbox numbers.
I just went back, deleted and re-added check boxes so the numbers all worked out..I was tryng to find a way to avoid doing that!

macropod
07-28-2014, 05:27 PM
If, as you had previously said, your document used Sections, you could use code like:

Sub CheckedNo(Sctn As Long)
Dim Count, n As Long
With ActiveDocument.Sections(Sctn)
For n = 1 To .FormFields.Count
With .FormFields(n)
If .Type = wdFieldFormCheckBox Then
If .CheckBox.Value = True Then
Count = Count + 1
End If
End If
End With
Next n
On Error Resume Next
.Variables.Add Name:="Count" & Sctn, Value:=Count
On Error GoTo 0
.Variables("Count" & Sctn).Value = Count
End With
End Sub
to update a 'Count' Variable that includes the Section # (e.g. 'Count1'). Then, for a given Section, you'd call the code with something like:
Call CheckedNo(1)
where the '1' is the number of the Section you want to process.

As I've said all along, there is no need to use bookmarks. Doing so just makes the document harder to maintain.