Changes

Including Check Boxes on an MS Word Merge Document

1,860 bytes added, 14:56, 20 August 2013
See Also
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 [[Custom Field Type: Special - MS Word Merge|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.
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''
Note: these instructions are for MS Word 2003. If you are using ==Example==The following example demonstrates how to check a different version check box on the steps may vary. Please consult MS Word Help for assistancedocument 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.''
Create ===MS Word Template===On the MS Word Template document create a ''Text Form Field, then double''. Double-click on it the field and enter a 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
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.
Finally open the Tools menu > Macro > Visual Basic Editor
Double-click on "ThisDocument" on the left-hand pane and then add the following script:
<pre>
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
Next create '--- check the source text and tick the “Check Box Form Field”, doubleassociated target checkbox if appropriate If (source.Range.Text = "CheckMe") Then target.Value = True '---click on it and enter a name in Delete the “Bookmark” source field source. Be sure that you have created a “Check Box Form Field”, not just a Check BoxRange.0 Delete
The “check box form field” is named target in this exampleExitSub:End Sub</pre>
'''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()''
Finally open ===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 Tools menu > Macro > VB Editor and add check box will be checked to the following script:'''source''' Text Form Field.<pre>Private Sub Document_Open()Do not assign anything to the '''target''' Check Box Form Field.
'--- define variables==Notes==The [[User|user]] who performs the MS Word Merge must have macros enabled for MS Word or it will not run. Dim sourcefield As Bookmark:* Macros can be enabled via the Tools menu > Macros > Security Set sourcefield = ActiveDocument:* 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.Bookmarks("sourcefield ")
Dim target As CheckBox
Set target = ActiveDocument.FormFields("target").CheckBox
'--- check 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 and check “Text Form Fields” will be deleted by the macro each time you open the associated checkbox if appropriate If (sourcefielddocument.Range.Text = "CheckMe") Then targetYou should only allow the macro to run when you have actually created the document via the MS Word Merge in [[SmartSimple]].Value = True
'--- Delete the source bookmark, including the Text Form Field
sourcefield.Range.Delete
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 '--- Delete this Macro Set vbCom = Application.VBE.ActiveVBProject.VBComponents vbCom''source''' Text Form Fields.Remove VBComponent:=vbComAny 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.Item("Document_Open")
End Sub
</pre>
The user who performs Hint: You may want to exclude the part of the macro that deletes the MS Word Merge must have Macros enabled for MS Word or it will not run. Macros can be enabled via source fields while you are working on the Tools menu > Macros > SecurityThe setting can be set to either Medium or Lowdocument. Medium is recommended, which means the user This will have allow you to agree to allow see if the source fields are populating with the Macro information you would expect to run each time the document is openedsee.
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.==See Also=={{WordMergeLinks}}
Note[[Category: 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.Custom Fields]]
8,849
edits