A CheckBox Content Control


G

Greg Maxey

Hi All,

I have been working on some methods and code to create a custom Checkbox
ContentControl in Word2007. I don't know why MS left the checkbox out of
the ContentControl collection. I concede the legacy checkbox was clunky and
ugly, but is served a purpose.

The basic checkbox CC was pretty straight forward and just a simply
extention of the iteractive toggle objects method posted on my website.

First create a pair of macrobutton fields 1. {MacroButton Check
"Winddings blank box symbol" } and 2. { MacroButton UnCheck "Wingdings
Checked box symbol" } Note: Replace the value in quotes with the actual
symbol.

Toggled the field codes and create a pair of autotext entries of the field
results. Named the first "Uncheck Box" and the second "Check Box"

Insert a rich text Content Control in the document and placedthe "Unchecked
Box" AutoText entry in the control. Select the Content Control and create a
QuickPart "Check Box."

The macro code to make the CC interative is:

Sub Check()
ActiveDocument.AttachedTemplate.AutoTextEntries("Checked Box").Insert
Where:=Selection.Range
End Sub

Sub Uncheck()
ActiveDocument.AttachedTemplate.AutoTextEntries("Unchecked Box").Insert
Where:=Selection.Range
End Sub

Next I wanted to see if I could group a series of these CCs and make it so
that only one checkbox in the group could display the checked option.

I bounded couple groups of checkbox CCs with bookmark ranges (e.g.,
CheckGroup1, CheckGroup2).

The challenge I learned was identifying the CC that I was working on with
code. Despite some initial successes, I ran hard aground with all attempts
to incorporate the new ContentsControlOnEntry document event. If finally
came up with the code below which seems to work in both a protected document
and in documents where the forms CCs are all grouped into one big rich text
CC with editing restricted.

I wanted to share these methods and code with this group in hopes that
someone else can use it or improve upon it.


Option Explicit
Private pID As String
Private oCC As ContentControl
Sub Check()
pID = CC_ID
ActiveDocument.AttachedTemplate.AutoTextEntries("Check Box").Insert
Where:=Selection.Range
Evaluate pID
End Sub
Function CC_ID() As String
Dim i As Long
i = 0
For Each oCC In ActiveDocument.ContentControls
If Selection.InRange(oCC.Range) Then
i = i + 1
CC_ID = oCC.ID
If i = 2 Then Exit For 'Change value if CC is nested more than 1 deep.
End If
Next
End Function
Sub Uncheck()
ActiveDocument.AttachedTemplate.AutoTextEntries("Uncheck Box").Insert
Where:=Selection.Range
End Sub
Sub Evaluate(ByRef pID As String)
Dim oRng As Word.Range
On Error GoTo Err_Handler
If Selection.Range.InRange(ActiveDocument.Bookmarks("CheckGroup1").Range)
Then
Set oRng = ActiveDocument.Bookmarks("CheckGroup1").Range
GroupProcess pID, oRng
End If
If Selection.Range.InRange(ActiveDocument.Bookmarks("CheckGroup2").Range)
Then
Set oRng = ActiveDocument.Bookmarks("CheckGroup2").Range
GroupProcess pID, oRng
End If
Exit Sub
Err_Handler:
MsgBox Err.Number & " " & Err.Description
End Sub
Sub GroupProcess(ByRef pCC_ID As String, oRng As Word.Range)
For Each oCC In oRng.ContentControls
If oCC.ID <> pCC_ID Then
If
InStr(ActiveDocument.ContentControls(pCC_ID).Range.Fields(1).Code.Text,
"Uncheck") Then
ActiveDocument.AttachedTemplate.AutoTextEntries("Uncheck Box").Insert
Where:=oCC.Range
End If
End If
Next oCC
End Sub
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top