473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
1 4927
willakawill
1,646 1GB
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

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

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
9
by: Tom | last post by:
A question for gui application programmers. . . I 've got some GUI programs, written in Python/wxPython, and I've got a help button and a help menu item. Also, I've got a compiled file made with...
11
by: Helmut Jarausch | last post by:
Hi, entering help('rstrip') or help('ljust') into IDLE's shell window I only get no Python documentation found ...
4
by: Sarir Khamsi | last post by:
Is there a way to get help the way you get it from the Python interpreter (eg, 'help(dir)' gives help on the 'dir' command) in the module cmd.Cmd? I know how to add commands and help text to...
7
by: tcarvin | last post by:
The .NET studio help system seems to use these odd URLs to locate (html) help files to display. But on my system, I get "Server not found" web errors whenever I access Help. What do I have to do...
3
by: Woody Splawn | last post by:
For reasons I have not yet identified I am having problems with the help system in VS.net 2003. It seems as though it was working all right in the past but it's not now. First of all I can't seem...
6
by: d.warnermurray | last post by:
I am doing a project for school that involves creating help files for a html authoring tool. If you could help me with answers to some questions it would really help. 1. What tasks do you expect...
3
by: Mark Lees | last post by:
Just installed 2003 and the help file sucks because it tries to connect to Microsoft Online Help which takes too long. When I run Access and I'm not connected to the internet, it seems to connect...
3
by: stuart_white_ | last post by:
I've just upgraded from Python 2.3.3 to Python 2.4.2, and, although the new version of Python seems to be running correctly, I can't seem access the help from the interpreter. On Python 2.3.3...
1
by: gordon | last post by:
Hi I have an app that i have written a chm help file for. What is the best way to associate that with my help menu and the F1 key. I have added the file to the project already, just not sure...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.