By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,677 Members | 1,069 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,677 IT Pros & Developers. It's quick & easy.

Help! Mail merge & using VB code - using form text fields

P: 2
Hi,
I wanted to use mail merge with forms but Text form fields are not retained during mail merge in Word, I got the code from Microsoft but it doesn't remember the text form field options such as the maximum length of the text (which I need) and the text format (would be ideal but can do without if need be)
I have posted the code below, so please can you help!?? Thanks in advance...


Expand|Select|Wrap|Line Numbers
  1. Sub PreserveMailMergeFormFieldsNewDoc()
  2.  
  3. Dim fFieldText() As String
  4. Dim iCount As Integer
  5. Dim fField As FormField
  6. Dim sWindowMain, sWindowMerge As String
  7.  
  8. On Error GoTo ErrHandler
  9.  
  10. ' Store Main merge document window name.
  11. sWindowMain = ActiveWindow.Caption
  12.  
  13. ' Because the document contains form fields,
  14. ' it should be protected, so unprotect document.
  15. If ActiveDocument.ProtectionType <> wdNoProtection Then
  16.    ActiveDocument.Unprotect
  17. End If
  18.  
  19. ' Loop through all text form fields
  20. ' in the main mail merge document.
  21. For Each aField In ActiveDocument.FormFields
  22.  
  23.    ' If the form field is a text form field...
  24.    If aField.Type = wdFieldFormTextInput Then
  25.  
  26.       ' Redim array to hold contents of text field.
  27.       ReDim Preserve fFieldText(1, iCount + 1)
  28.  
  29.       ' Place content and name of field into array.
  30.       fFieldText(0, iCount) = aField.Result
  31.       fFieldText(1, iCount) = aField.Name
  32.  
  33.       ' Select the form field.
  34.       aField.Select
  35.  
  36.       ' Replace it with placeholder text.
  37.       Selection.TypeText "<" & fFieldText(1, iCount) & "PlaceHolder>"
  38.  
  39.       ' Increment icount
  40.       iCount = iCount + 1
  41.  
  42.    End If
  43.  
  44. Next aField
  45.  
  46. ' Perform mail merge to new document.
  47. ActiveDocument.MailMerge.Destination = wdSendToNewDocument
  48. ActiveDocument.MailMerge.Execute
  49.  
  50. ' Find and Replace placeholders with form fields.
  51. doFindReplace iCount, fField, fFieldText()
  52.  
  53. ' Protect the merged document.
  54. ActiveDocument.Protect Password:="", NoReset:=True, _
  55.    Type:=wdAllowOnlyFormFields
  56.  
  57. ' Get name of final merged document.
  58. sWindowMerge = ActiveWindow.Caption
  59.  
  60. ' Reactivate the main merge document.
  61. Windows(sWindowMain).Activate
  62.  
  63. ' Find and replace placeholders with form fields.
  64. doFindReplace iCount, fField, fFieldText()
  65.  
  66. ' Reprotect the main mail merge document.
  67. ActiveDocument.Protect Password:="", NoReset:=True, _
  68.    Type:=wdAllowOnlyFormFields
  69.  
  70. ' Switch back to the merged document.
  71. Windows(sWindowMerge).Activate
  72.  
  73. ErrHandler:
  74.  
  75. End Sub
  76.  
  77.  
  78. Sub doFindReplace(iCount As Integer, fField As FormField, _
  79.    fFieldText() As String)
  80.  
  81. ' Go to top of document.
  82. Selection.HomeKey Unit:=wdStory
  83.  
  84. ' Initialize Find.
  85. Selection.Find.ClearFormatting
  86.  
  87. With Selection.Find
  88.    .Forward = True
  89.    .Wrap = wdFindContinue
  90.    .Format = False
  91.    .MatchCase = False
  92.    .MatchWholeWord = False
  93.    .MatchWildcards = False
  94.    .MatchSoundsLike = False
  95.    .MatchAllWordForms = False
  96.  
  97.    ' Loop form fields count.
  98.     For i = 0 To iCount
  99.  
  100.       ' Execute the find.
  101.       Do While .Execute(FindText:="<" & fFieldText(1, i) _
  102.          & "PlaceHolder>") = True
  103.  
  104.          ' Replace the placeholder with the form field.
  105.          Set fField = Selection.FormFields.Add _
  106.             (Range:=Selection.Range, Type:=wdFieldFormTextInput)
  107.  
  108.          ' Restore form field contents and bookmark name.
  109.          fField.Result = fFieldText(0, i)
  110.          fField.Name = fFieldText(1, i)
  111.       Loop
  112.  
  113.       ' Go to top of document for next find.
  114.       Selection.HomeKey Unit:=wdStory
  115.  
  116.    Next
  117. End With
  118.  
  119. End Sub
Feb 13 '07 #1
Share this Question
Share on Google+
1 Reply


100+
P: 1,646
Hi. I have no experience with this process. Having said that, a quick look through your code tells me something you might like :)
At this point:
Expand|Select|Wrap|Line Numbers
  1. ' Redim array to hold contents of text field.
  2.       ReDim Preserve fFieldText(1, iCount + 1)
  3.  
  4.       ' Place content and name of field into array.
  5.       fFieldText(0, iCount) = aField.Result
  6.       fFieldText(1, iCount) = aField.Name
you can alter the code to accept as many fields of data that you wish as long as you understand that the data is being stored as a string for the time being and you can alter that when you need to later.
Expand|Select|Wrap|Line Numbers
  1. ' Redim array to hold contents of text field.
  2.       ReDim Preserve fFieldText(2, iCount + 1)
  3.  
  4.       ' Place content and name of field into array.
  5.       fFieldText(0, iCount) = aField.Result
  6.       fFieldText(1, iCount) = aField.Name
  7.       fFieldText(2, iCount) = 'your max length here
Hope this helps
Feb 15 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.