PDA

View Full Version : Word Merge with txt, graphic, and forms



mcsweeneyp
11-02-2006, 03:26 PM
The Word Merge Doc I am working with does a standard txt merge, and then locks the resultant formletter to make the form fields usable. (I found some code that fixes the problem where textform fields don't move to the formletter.) It all works great, but now I need to add an InsertPicture merge field.

After the merge, if I manually turn off the protection on the formletter, I can <F9> to get the picture to display, so I know I have the syntax working. But I do NOT want to have to manually turn off protection, the <F9>, and turn on the protection.

Can anyone help me with this?

I have pasted in my current macro (ie, where I am forced to manually unlock/lock) below.

Oh, yes, I have tried running an update macro I found on the web (see the bottom) but it does not seem to help. Also tried inserting a Selection.Fields.Update before my final lock. It also did not help.

To top it all off, I am not a programmer so I am totally at a loss now. :bug:

Any help is greatly appreciated!!
Patty

-----------------------------------------------------------
Private Sub Document_Open()
'InsertBoth Macro
Application.Visible = True
Application.WindowState = wdWindowStateMinimize
Application.DisplayAlerts = wdAlertsNone
Dim fFieldText() As String
Dim iCount As Integer
Dim fField As FormField

docname = ActiveDocument.Name
datapath = "c:\xxxx\"

' Because the document contains form fields,
' it should be protected, so unprotect document.
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
End If

ActiveDocument.MailMerge.OpenDataSource Name:=datapath & _
Left(docname, Len(docname) - 4) & ".txt", _
ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _
AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
Format:=wdOpenFormatAuto, Connection:="", SQLStatement:="SELECT * FROM 'Data'", SQLStatement1:=""
' 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

With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.MailAsAttachment = False
.MailAddressFieldName = ""
.MailSubject = ""
.SuppressBlankLines = True
.DataSource.FirstRecord = wdDefaultFirstRecord
.DataSource.LastRecord = wdDefaultLastRecord
.Execute (True)

' Find and Replace placeholders with form fields.
doFindReplace iCount, fField, fFieldText()
' Protect the merged document
ActiveDocument.Protect Password:="", NoReset:=True, Type:=wdAllowOnlyFormFields

End With
Application.Documents("InsertBoth.doc").Close (Word.WdSaveOptions.wdDoNotSaveChanges)

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
------------------------
Sub UpdateAll()
' UpdateAll Macro
'
Dim oStory As Range
For Each oStory In ActiveDocument.StoryRanges
oStory.Fields.Update
If oStory.StoryType <> wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Fields.Update
Wend
End If
Next oStory
Set oStory = Nothing
End Sub

mcsweeneyp
11-06-2006, 08:25 AM
Issue was a bug with IncludePicture (see support.microsoft.com article 112324: IncludePicture and Improt Fields do not Merge Correctly)
The merge was removing the \\. Modified IncludePicture with Merge field to be {INCLUDEPICTURE "c:\\\\XXXX\\\\{MERGEFILED Picture}"}

The referenced Picture field in my .txt import file was IncludeBoth.jpg
(Note: alternatively use "c:\\\\XXXX\\\\IncludeBoth.jpg" in the .txt file and {INCLUDEPICTURE {MERGEFILED Picture}} in the .doc file)

In the Mail Merge Document ThisDocument Macro prior to
' Protect the merged document

Added:
' Update the merged document prior to locking
Selection.Fields.Update


Another useful article was 211308: Text Form Field are Not Retained During Mail Merge. Used the word around code for looping and replacing form fields with placeholders, and then the place holders with form fields in my merged formletter.

Patty