Difference between revisions of "Including Check Boxes on an MS Word Merge Document"

From SmartWiki
Jump to: navigation, search
Line 25: Line 25:
 
<pre>
 
<pre>
 
Private Sub Document_Open()
 
Private Sub Document_Open()
 +
 +
    '--- Exit if an error is encountered
 +
    On Error GoTo ExitSub
  
 
     '--- define variables
 
     '--- define variables
     Dim sourcefield As Bookmark
+
     Dim source As Bookmark
     Set sourcefield = ActiveDocument.Bookmarks("sourcefield ")
+
     Set source = ActiveDocument.Bookmarks("source")
  
 
     Dim target As CheckBox
 
     Dim target As CheckBox
 
     Set target = ActiveDocument.FormFields("target").CheckBox
 
     Set target = ActiveDocument.FormFields("target").CheckBox
  
     '--- check the source text and check the associated checkbox if appropriate
+
     '--- check the source text and tick the associated target checkbox if appropriate
     If (sourcefield.Range.Text = "CheckMe") Then target.Value = True
+
     If (source.Range.Text = "CheckMe") Then target.Value = True
 
+
     '--- Delete the source bookmark, including the Text Form Field
+
     '--- Delete the source field
     sourcefield.Range.Delete
+
     source.Range.Delete
 
 
    '--- Delete this Macro
 
    Set vbCom = Application.VBE.ActiveVBProject.VBComponents
 
    vbCom.Remove VBComponent:=vbCom.Item("Document_Open")
 
  
End Sub
+
ExitSub:
 +
End Sub
 
</pre>
 
</pre>
  
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 user who performs the MS Word Merge must have Macros enabled for MS Word or it will not run.  
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.
+
:* 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.
  
You will have to go to Tools>Macro>Security - Trusted Publishers and check Trust access to Visual Basic Editor (or Visual Basic Project) before running the code. Change "Module1" to suit.
 
  
 
Note: 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.
 
Note: 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.

Revision as of 12:13, 22 June 2009

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 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.


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.


Create a Text Form Field, then double-click on it and enter a name in the “Bookmark” field.


The field (named sourcefield in this example) is where the merge data is 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 a name in the “Bookmark” field. Be sure that you have created a “Check Box Form Field”, not just a Check Box. 0

The “check box form field” is named target in this example


Finally open the Tools menu > Macro > VB Editor and 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

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.


Note: 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.