madpsychot
05-28-2008, 03:28 PM
I am looking for some real help here!
The scenario:
I am making report for my school, by mail merging a master report layout with data from an Excel spreadsheet. That merges the student name, and puts their correct class, teacher etc on every report. I also have Form Text Fields, for teachers to enter their comments.
I merge this master into a single document, so I end up with a document containing 360 students. This is done using the macros provided by Microsoft Support. This single document has therefore retained all the form text fields I put in, which would ordinarily have been lost.
After I have done this, I use the excellent split document macro (from this site!) to separate all my reports into one report - per student. Everything works well! So what is my problem?
Well, in my master report I have set a maximum character limit for each of the form text fields. In the mail merge this is lost, and each text field now becomes unlimited text.
In the macros provided by Microsoft Support, the macro first copies the contents of the text field, then the name of the field, and then replaces it with a placeholder. As my reports are empty to start with I have no need for the contents to be copied. Is there a way that the attribute (maximum character length) of the field can be copied and then put back in?
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
This is the code that copies the contents and the form field, and replaces it with a placeholder.
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
And this is the find and replace to put the Text Fields back in their places.
Thanks in advance, I do hope there is someone out there who knows how to help me!
The scenario:
I am making report for my school, by mail merging a master report layout with data from an Excel spreadsheet. That merges the student name, and puts their correct class, teacher etc on every report. I also have Form Text Fields, for teachers to enter their comments.
I merge this master into a single document, so I end up with a document containing 360 students. This is done using the macros provided by Microsoft Support. This single document has therefore retained all the form text fields I put in, which would ordinarily have been lost.
After I have done this, I use the excellent split document macro (from this site!) to separate all my reports into one report - per student. Everything works well! So what is my problem?
Well, in my master report I have set a maximum character limit for each of the form text fields. In the mail merge this is lost, and each text field now becomes unlimited text.
In the macros provided by Microsoft Support, the macro first copies the contents of the text field, then the name of the field, and then replaces it with a placeholder. As my reports are empty to start with I have no need for the contents to be copied. Is there a way that the attribute (maximum character length) of the field can be copied and then put back in?
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
This is the code that copies the contents and the form field, and replaces it with a placeholder.
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
And this is the find and replace to put the Text Fields back in their places.
Thanks in advance, I do hope there is someone out there who knows how to help me!