|

|
|
The following code contained in the UserForm initialize and command
button click events makes the checkboxes in the UserForm
interactive with the checkboxes in the document. The state of the
UserForm checkboxes will always match the state of the document
checkboxes when the UserForm is displayed.
For example, if the UserForm is initially called with an AutoNew
macro in the template and the checkboxes in the template are all empty
then the UserForm will display all empty checkboxes. Changing the state of
one or more of the UserForm checkboxes will be reflected in the state of
the corresponding checkbox in the document when the UserForm is executed
(i.e., OK button clicked). If you provide a means to manually call
the UserForm in an existing document (e.g., for certain editing) the
state of the checkboxes in the UserForm will reflect the state of the
corresponding checkbox in the document. |
|
|
Option Explicit
Private i As Long
Private oFF As FormFields
Private Sub UserForm_Initialize()
Set oFF = ActiveDocument.FormFields
For i = 1 To 4
'Assumes UserForm and Document
Form use default control (Checkbox) and bookmark (Check) names
Me.Controls("CheckBox" & i).Value = oFF("Check" &
i).CheckBox.Value
Next i
Set oFF = Nothing
End Sub
Private Sub CommandButton1_Click()
Set oFF = ActiveDocument.FormFields
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
End If
For i = 1 To 4
oFF("Check" & i).CheckBox.Value = Me.Controls("Checkbox" &
i).Value
Next i
Set oFF = Nothing
ActiveDocument.Protect wdAllowOnlyFormFields, NoReset:=True
Me.Hide
End Sub |
|
Personally I don't like the appearance of a formfield checkbox. I
don't like the "X" or the grey field background. I like the look of a check mark like shown
here. |
|

|
|
Achieving this look 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.dot"
2. Next you need to create a couple of autotext entries.
Most Autotext
entries are just bits of text or graphics that are stored in your
document template for convienent reuse. Ours are a bit more
complex. We need to create AutoText entries that that display the
checked and uncheced box symbols within a MacroButton field and specify
a property of the text that can be distinguished by Word's VBA object
model. I am going to detail the steps for this task using
Word2007. You can find similar procedures if you are using ealier
versions of Word at Add Interactive
Toggle Objects to Word Documents.
a. Create a pair of field code braces
by pressing CTRL+F9.
b. Type the following text, excluding
the quotation marks, insided the
field code braces: "MACROBUTTON Checkit "
c.
Using Insert>Symbols>Symbol>More Symbols, select the "Wingdings" font
and insert the Wingdings symbol 111 (the empty box). Your field
should look like the example below. |
|

|
|
d. Right click the field code and select
"toggle field codes." A empty box will be displayed. |
|

|
|
e. Select the empty box symbol.
Display the font dialog box using CTRL+d and verify the font color is
set to "Automatic." |
|
f. With the empty box still selected,
click ALT+F3 to display the Create New Building Block dialog. |
|

|
|
g. Name the entry "UCB," change the
"Gallery" field to "AutoText" and change the "Save in" field to your
template name. Then click "OK" |
|

|
|
h. With the empty box still selected,
click Insert>Links>Bookmarks and bookmark the selection as "CB1."
i. Create a second pair of field code
braces by pressing CTRL+F9. In this field type: "MACROBUTTON
UnCheckit " and insert the WingDing symbol 254
(checked box) |
|

|
|
j. Toggle the new field and select the
check mark symbol displayed. Using the font dialog box (this is
very important), set the font color to "Black."
k. With the check mark symbol still selected, press ALT+F3 and
name the entry "CB." Be sure to create your entry in the
"AutoText" gallery and save it in your named template.
l. Bookmark the selected check mark
symbol using the name "CB2."
m. The AutoText entries are now
complete and your template is displaying a empty check box and a check
mark box.
3. Next we need to create the VBA code that makes these symbols
interactive with a mouse click.
a. Open the VBA editor (ALT+Fll).
If not displayed, display the Project Explorer using CTRL+r.
b. Click on your template name and.
using the "Insert" dropdown menu, insert a project module. In the
project module, insert the following code: |
|
|
Option Explicit
Sub CheckIt()
Dim oBMs As Bookmarks
Dim oStr As String
Dim oRng As Word.Range
'ActiveDocument.Unprotect 'Activate this
line if using a protected Word form
Set oBMs = ActiveDocument.Bookmarks
oStr = Selection.Bookmarks(1).Name
Set oRng = oBMs(oStr).Range
ActiveDocument.AttachedTemplate.AutoTextEntries("CB").Insert Where:=oRng
oBMs.Add oStr, oRng
oRng.Font.Color = wdColorBlack
'ActiveDocument.Protect
wdAllowOnlyFormFields, NoReset:=True 'Activate this line as required.
End Sub
Sub UncheckIt()
Dim oBMs As Bookmarks
Dim oStr As String
Dim oRng As Word.Range
'ActiveDocument.Unprotect 'Activate this
line if using a protected Word form
Set oBMs = ActiveDocument.Bookmarks
oStr = Selection.Bookmarks(1).Name
Set oRng = oBMs(oStr).Range
ActiveDocument.AttachedTemplate.AutoTextEntries("UCB").Insert Where:=oRng
oBMs.Add oStr, oRng
oRng.Font.Color = wdColorAutomatic
'ActiveDocument.Protect
wdAllowOnlyFormFields, NoReset:=True 'Activate this line as required.
End Sub |
|
c. Close the VBA editor and test you
symbols. Double clicking on either symbol will cause it to change
states. |
|
4. Now lets make these symbols interactive with a UserForm.
a. Add a couple more empty box symbols using Insert>QuickParts>Building
Block Organizer. Select the UCB AutoText entry and select
"Insert." |
|
b. Select the new entries and bookmark
them using the named "CB3" and "CB4." respectively. |
|

|
|
The following code in the UserForm will make these custom check mark
boxes interactive with the UserForm similiar to the Word protected for
example shown above. The only difference is these check mark boxes
are not in a tab sequence like those of a protected form. They must be
selected and toggled using the mouse. |
|
|
Option Explicit
Private i As Long
Private oDoc As Word.Document
Private Sub UserForm_Initialize()
Set oDoc = ActiveDocument
'oDoc.Unprotect 'Activate this line if
using a protected form
Dim oBM As Bookmarks
Set oBM = oDoc.Bookmarks
For i = 1 To 4
If oBM("CB" & i).Range.Font.Color = wdColorAutomatic Then
Me.Controls("CheckBox" & i).Value =
False
Else
Me.Controls("CheckBox" & i).Value =
True
End If
Next i
End Sub
Private Sub CommandButton1_Click()
Dim oRng As Word.Range
Set oDoc = ActiveDocument
For i = 1 To 4
Set oRng = oDoc.Bookmarks("CB" & i).Range
If Me.Controls("Checkbox" & i).Value = True Then
oDoc.AttachedTemplate.AutoTextEntries("CB").Insert Where:=oRng, RichText:=True
oRng.Font.Color = wdColorBlack
Else
oDoc.AttachedTemplate.AutoTextEntries("UCB").Insert Where:=oRng,
RichText:=True
oRng.Font.Color = wdColorAutomatic
End If
oDoc.Bookmarks.Add "CB" & i, oRng
Next i
'oDoc.Protect wdAllowOnlyFormFields,
NoReset:=True 'If using a protected form
Me.Hide
End Sub |
|
Click this link
Interactive Document Checkboxes to download a zip file
containing a template with the examples and code shown above. |
|
|
Looking for something else?
|
|
|
|
|