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... - Sub PreserveMailMergeFormFieldsNewDoc()
-
-
Dim fFieldText() As String
-
Dim iCount As Integer
-
Dim fField As FormField
-
Dim sWindowMain, sWindowMerge As String
-
-
On Error GoTo ErrHandler
-
-
' Store Main merge document window name.
-
sWindowMain = ActiveWindow.Caption
-
-
' Because the document contains form fields,
-
' it should be protected, so unprotect document.
-
If ActiveDocument.ProtectionType <> wdNoProtection Then
-
ActiveDocument.Unprotect
-
End If
-
-
' Loop through all text form fields
-
' in the main mail merge document.
-
For Each aField In ActiveDocument.FormFields
-
-
' If the form field is a text form field...
-
If aField.Type = wdFieldFormTextInput Then
-
-
' Redim array to hold contents of text field.
-
ReDim Preserve fFieldText(1, iCount + 1)
-
-
' Place content and name of field into array.
-
fFieldText(0, iCount) = aField.Result
-
fFieldText(1, iCount) = aField.Name
-
-
' Select the form field.
-
aField.Select
-
-
' Replace it with placeholder text.
-
Selection.TypeText "<" & fFieldText(1, iCount) & "PlaceHolder>"
-
-
' Increment icount
-
iCount = iCount + 1
-
-
End If
-
-
Next aField
-
-
' Perform mail merge to new document.
-
ActiveDocument.MailMerge.Destination = wdSendToNewDocument
-
ActiveDocument.MailMerge.Execute
-
-
' Find and Replace placeholders with form fields.
-
doFindReplace iCount, fField, fFieldText()
-
-
' Protect the merged document.
-
ActiveDocument.Protect Password:="", NoReset:=True, _
-
Type:=wdAllowOnlyFormFields
-
-
' Get name of final merged document.
-
sWindowMerge = ActiveWindow.Caption
-
-
' Reactivate the main merge document.
-
Windows(sWindowMain).Activate
-
-
' Find and replace placeholders with form fields.
-
doFindReplace iCount, fField, fFieldText()
-
-
' Reprotect the main mail merge document.
-
ActiveDocument.Protect Password:="", NoReset:=True, _
-
Type:=wdAllowOnlyFormFields
-
-
' Switch back to the merged document.
-
Windows(sWindowMerge).Activate
-
-
ErrHandler:
-
-
End Sub
-
-
-
Sub doFindReplace(iCount As Integer, fField As FormField, _
-
fFieldText() As String)
-
-
' Go to top of document.
-
Selection.HomeKey Unit:=wdStory
-
-
' Initialize Find.
-
Selection.Find.ClearFormatting
-
-
With Selection.Find
-
.Forward = True
-
.Wrap = wdFindContinue
-
.Format = False
-
.MatchCase = False
-
.MatchWholeWord = False
-
.MatchWildcards = False
-
.MatchSoundsLike = False
-
.MatchAllWordForms = False
-
-
' Loop form fields count.
-
For i = 0 To iCount
-
-
' Execute the find.
-
Do While .Execute(FindText:="<" & fFieldText(1, i) _
-
& "PlaceHolder>") = True
-
-
' Replace the placeholder with the form field.
-
Set fField = Selection.FormFields.Add _
-
(Range:=Selection.Range, Type:=wdFieldFormTextInput)
-
-
' Restore form field contents and bookmark name.
-
fField.Result = fFieldText(0, i)
-
fField.Name = fFieldText(1, i)
-
Loop
-
-
' Go to top of document for next find.
-
Selection.HomeKey Unit:=wdStory
-
-
Next
-
End With
-
-
End Sub
1 4927
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: - ' Redim array to hold contents of text field.
-
ReDim Preserve fFieldText(1, iCount + 1)
-
-
' Place content and name of field into array.
-
fFieldText(0, iCount) = aField.Result
-
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. - ' Redim array to hold contents of text field.
-
ReDim Preserve fFieldText(2, iCount + 1)
-
-
' Place content and name of field into array.
-
fFieldText(0, iCount) = aField.Result
-
fFieldText(1, iCount) = aField.Name
-
fFieldText(2, iCount) = 'your max length here
Hope this helps
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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 ...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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$) {
}
...
|
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...
|
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
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
| |