Resting Anchor

The Anchorage

Personal website of Gregory K. Maxey, Commander USN (Retired)

Interactive Userform Checkboxes
(A Microsoft Word Help & Tip page by Gregory K. Maxey)

DISCLAIMER/TERMS OF USE

The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.

Click to acces PayPal Verification Service Click to acces PayPal Verification Service

However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.

If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!

Click to donate British Pound Sterling                   Click to donate US dollars                   Click to donate EU euros

The purpose of this Microsoft Word Tips & Microsoft Word Help page is provide and demonstrate a technique for creating interactive userform/document checkboxes.

Toggle Object© checkboxes

The first method employs a userform with standard checkbox controls and a document using Toggle Object© checkboxes.  This method is best suited for Word 2003 and Word 2007 documents.

Site Note IconNote:  In this tips page I have assumed that you are familiar with the Visual Basic Editor (VBE) and that you are familiar with creating and employing a userform.  If you need more information see:  Install/Employ VBA Procedures and Create & Employ a Userform

interactive uf checkboxes 1
Typical Userform

interactive uf checkboxes 2
Typical document with Toggle Object© checkboxes.

This method takes a bit of work but I think it is worth it.

  1. Create and save a new blank template.  I called mine "Interactive Document Checkboxes.dotm"
  2. Create a Toggle Object©:
interactive uf checkboxes 3
Start of MacroButton field

interactive uf checkboxes 4

Insert symbol in field

interactive uf checkboxes 5
Completed field

interactive uf checkboxes 6
Field result

Site Note IconNote:  Show "Symbol" dialog using Insert tab>Symbols>Symbol>More Symbols.

  1. Add the VBA procedures that creates the ToggleObject© and makes the MacroButton field and displayed result interactive with a mouse click:
VBA Script:
Option Explicit

Sub frmCheckit()
  ToggleIt
lbl_Exit:
  Exit Sub
End Sub

Sub ToggleIt()
Dim oFld As Word.Field
Dim oRngTarget As Word.Range
Dim i As Long
Dim strCharNum As Long
  'The target field (symbol you clicked)
  Set oFld = Selection.Fields(1)
  i = Determine_i(oFld)
  'Define target (or where symbol goes)
  Set oRngTarget = oFld.Code.Characters(i)
  'What is displayed now?
  strCharNum = AscW(oRngTarget.Text)
  'Toggle it
  If strCharNum = 168 Then
    oRngTarget.Font.Name = "Wingdings"
    oRngTarget.Text = ChrW(254)
  Else
    oRngTarget.Font.Name = "Wingdings"
    oRngTarget.Text = ChrW(168)
  End If
  'Clear variables
  Set oRngTarget = Nothing
  Set oFld = Nothing
lbl_Exit:
  Exit Sub
End Sub

Function Determine_i(ByRef oFld As Word.Field) As Long
Dim oRngTarget As Word.Range
Dim i As Long
Dim StrFont As String
Dim strCharNum As Long
  Determine_i = 0
  If oFld.Type <> wdFieldMacroButton Then
    MsgBox "This is not a valid field to test"
    Exit Function
  End If
  'Count number of characters in field's code
  i = oFld.Code.Characters.Count
  'Define target (or where symbol goes)
  Set oRngTarget = oFld.Code.Characters(i)
  'Does a space or spaces follow the symbol in the field code?
  Do While AscW(oRngTarget.Text) = 32
    'Move target until clear of spaces
    i = i - 1
    Set oRngTarget = oFld.Code.Characters(i)
  Loop
  'Since the symbol should be the last non-space character in the field:
  Determine_i = i
  'Clear variables
  Set oRngTarget = Nothing
lbl_Exit:
  Exit Function
End Function

'***** OPTIONAL *****
Sub AutoNew()
  'Enable single mouse click toggle.
  Options.ButtonFieldClicks = 1
lbl_Exit:
  Exit Sub
End Sub
Sub AutoClose()
  Options.ButtonFieldClicks = 2
lbl_Exit:
  Exit Sub
End Sub
'***** OPTIONAL *****
  1. The final step is to make the document checkboxes interactive with the userform checkboxes and vise-versa:

Site Note IconNote: As fields don't present a defined range object, the bookmarks are necessary to define and identify the fields for processing.

VBA Script:
Option Explicit
Private i As Long
Private oDoc As Word.Document
Private lngChar As Long

Private Sub UserForm_Initialize()
Dim oBM As Bookmarks
Set oDoc = ActiveDocument
Set oBM = oDoc.Bookmarks
  For i = 1 To 4
    lngChar = Determine_i(oBM("CB" & i).Range.Fields(1))
    If AscW(oBM("CB" & i).Range.Fields(1).Code.Characters(lngChar)) = 254 Then
      Me.Controls("CheckBox" & i).Value = True
    Else
      Me.Controls("CheckBox" & i).Value = False
    End If
  Next i
  Set oDoc = Nothing
  Set oBM = Nothing
lbl_Exit:
  Exit Sub
End Sub

Private Sub CommandButton1_Click()
Dim oRng As Word.Range
  Set oDoc = ActiveDocument
  For i = 1 To 4
    Set oRng = oDoc.Bookmarks("CB" &amp; i).Range
    lngChar = Determine_i(oRng.Fields(1))
    If Me.Controls("Checkbox" &amp; i).Value = True Then
      SetIt oRng.Fields(1), lngChar, True
    Else
      SetIt oRng.Fields(1), lngChar, False
    End If
    oDoc.Bookmarks.Add "CB" &amp; i, oRng
  Next i
  Me.Hide
  Set oDoc = Nothing
  Set oRng = Nothing
lbl_Exit:
  Exit Sub
End Sub

VBA Script:
Sub SetIt(ByRef oFld As Word.Field, lngTarget As Long, bChecked As Boolean)
Dim oRngTarget As Word.Range
  Set oRngTarget = oFld.Code.Characters(lngTarget)
  If bChecked Then
    oRngTarget.Font.Name = "Wingdings"
    oRngTarget.Text = ChrW(254)
  Else
    oRngTarget.Font.Name = "Wingdings"
    oRngTarget.Text = ChrW(168)
  End If
 'Clear variables
  Set oRngTarget = Nothing
  Set oFld = Nothing
lbl_Exit:
  Exit Sub
End Sub
  1. If you have followed the steps carefully then your document checkboxes should now be interactive with the userform checkboxes.  If you had troubles, I've included a demonstration template containing the example userform and code that you can download using a link a the end of this tips page.  

Built-in interactive checkboxes

Word 2003/20072010 all include legacy form field interactive checkboxes.  While probably redundant to have a userform linked to a online (protected) form, it can certainly be done.  I've included an example of using legacy form field checkboxes linked with a userform in the demonstration template.

Word 2010 introduced interactive checkboxes to the content control collection.  These are ideal objects for linking with a userform and the process practical defines simple!!

  1. Insert the content control checkboxes in the document.
  2. Title each checkbox with a unique sequential title
  3. Set the desired "checked" and "unchecked" symbols
interactive uf checkboxes 7

interactive uf checkboxes 8
  1. Copy and paste the following code in the Userform module:
VBA Script:
Option Explicit
Private i As Long
Private oCC As ContentControl
Private Sub UserForm_Initialize()
  For i = 1 To 4
    Set oCC = ActiveDocument.SelectContentControlsByTitle("Check" & i).Item(1)
    Me.Controls("CheckBox" & i).Value = oCC.Checked
  Next i
  Set oCC = Nothing
lbl_Exit:
  Exit Sub
End Sub
Private Sub CommandButton1_Click()
  For i = 1 To 4
    Set oCC = ActiveDocument.SelectContentControlsByTitle("Check" & i).Item(1)
    oCC.Checked = Me.Controls("Checkbox" & i).Value
  Next i
  Set oCC = Nothing
  Me.Hide
lbl_Exit:
  Exit Sub
End Sub
  1. That's all there is to it!! Your document and userform checkboxes should be fully interactive.

You can download a demonstration template containing the document fields, content controls, userforms and VBA code used to create the examples presented in this tips page here: Interactive Userform Checkboxes

That's it! I hope you have found this tips page useful and informative.

Share

DISCLAIMER/TERMS OF USE

The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.

Click to acces PayPal Verification Service Click to acces PayPal Verification Service

However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.

If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!

Click to donate British Pound Sterling                   Click to donate US dollars                   Click to donate EU euros

Search my site or the web using Google Search Engine

Google Search Logo