Including Check Boxes on an MS Word Merge Document

From SmartWiki
Jump to: navigation, search

This article explain how to use the data stored within SmartSimple to determine whether or not a Check Box on an MS Word Merge document is checked.

The same principles can be used for many other advanced controls of MS Word Merge documents based on underlying SmartSimple data.

General Procedure

The MS Word Merge Custom Field allows you to merge text stored within SmartSimple into an MS Word document.

You cannot merge information directly into a Check Box Form Field on the MS Word document to make it checked or unchecked. Instead it can be done as follows:

  • Create a source Text Form Field on the MS Word document to store the value that determines whether or not the Check Box should be checked.
  • Create the target Check Box Form Field on the MS Word document
  • Create a VB Script that runs automatically when the document opens and does the following:
  • Reads the value in the source Text Form Field and changes the value of the Check Box Form Field to True (which will make it checked), if the source value dictates that it should be checked.
  • Deletes the source Text Form Field

Example

The following example demonstrates how to check a check box on the MS Word document if the source field within SmartSimple has "CheckMe" as the stored value.

Note: these instructions are for MS Word 2003. If you are using a different version the steps may vary. Please consult MS Word Help for assistance.

MS Word Template

On the MS Word Template document create a Text Form Field. Double-click on the field and enter the name source in the “Bookmark” field.

The source field is where the merge data will be stored. When the MS Word Merge document is opened, a macro will use this value in this field to determine whether or not the associated checkbox should be checked. The field will then be deleted by the macro.

Next create the Check Box Form Field, double-click on it and enter the name target in the “Bookmark” field. Be sure that you have created a “Check Box Form Field”, not just a Check Box


Finally open the Tools menu > Macro > Visual Basic Editor Double-click on "ThisDocument" on the left-hand pane and then add the following script:

Private Sub Document_Open()

    '--- Exit if an error is encountered
    On Error GoTo ExitSub

    '--- define variables
    Dim source As Bookmark
    Set source = ActiveDocument.Bookmarks("source")

    Dim target As CheckBox
    Set target = ActiveDocument.FormFields("target").CheckBox

    '--- check the source text and tick the associated target checkbox if appropriate
    If (source.Range.Text = "CheckMe") Then target.Value = True
 
    '--- Delete the source field
    source.Range.Delete

ExitSub:
End Sub

NOTE: the Document_Open subroutine is NOT executed automatically in MS Word 2002 and earlier. If using earlier versions of Word replace the line:

Private Sub Document_Open()
with
Sub AutoOpen()

MS Word Merge Custom Field

On the MS Word Merge Custom Field within SmartSimple you should assign the Custom Field that determines whether or not the check box will be checked to the source Text Form Field.

Do not assign anything to the target Check Box Form Field.

Notes

The user who performs the MS Word Merge must have macros enabled for MS Word or it will not run.

  • Macros can be enabled via the Tools menu > Macros > Security
  • The setting can be set to either Medium or Low. Medium is recommended, which means the user will have to agree to allow the macro to run each time the document is opened.


When you are creating the original MS Word Merge document you will need to disable macros while you work on the document. Otherwise the source “Text Form Fields” will be deleted by the macro each time you open the document. You should only allow the macro to run when you have actually created the document via the MS Word Merge in SmartSimple.


The "On Error GoTo ExitSub" command in the script is very important. Once the document has been opened the first time the macro deletes all the source Text Form Fields. Any time the document is opened subsequently the macro will try to reference these fields, and since they no longer exist the command will fail and the macro will exit.


Hint: You may want to exclude the part of the macro that deletes the source fields while you are working on the document. This will allow you to see if the source fields are populating with the information you would expect to see.

See Also

To set up a Word merge:

Advanced Word Merge Features Include: