Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Mailmerge and Fax / April 2005

Tip: Looking for answers? Try searching our database.

Detecting Wrong MergeFields while automating MailMerge

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Christof Nordiek - 29 Apr 2005 08:08 GMT
Hi all,

another question over MailMerge automation:
Does someone know how programmatically detect, if there are MaiMerge-Fields
in a Maindocument wich don't appear in the DataSource?
When i start the MailMerge, and there are such fields in the document a
dialog is opened (but it's not visible, it's only reachable by Alt-Tab-Key)
where the user has to exchange the name of the field.
if then the user presses Cancel the MailMerge goes on anyway, and ugly
errorMessages appear in the merged document, (very bad, if you are printing
direct to the printer).

What i want, is a way to detect the errors in my app and react on it by
myself. At least the user should be able to stop the merging

Any ideas?

Thanks
Peter Jamieson - 29 Apr 2005 08:48 GMT
The following code will detect /most/ of the mergefields in a document - it
may not see some of the fields in the graphics layer.

Sub IterateMergeFields()
Dim r As Range
Dim f As Field
Dim s As Shape
For Each r In ActiveDocument.StoryRanges
 If r.StoryType <> wdTextFrameStory Then
   For Each f In r.Fields
     If f.Type = wdFieldMergeField Then
       ' do whatever you need
     End If
   Next
 End If
Next
For Each s In ActiveDocument.Shapes
 If s.Type = msoTextBox Then
   For Each f In s.TextFrame.TextRange.Fields
     If f.Type = wdFieldMergeField Then
       ' do whatever you need
     End If
   Next
 End If
Next
End Sub

If you take the name of the merge field and use it as the index to the

ActiveDocument.MailMerge.DataSource.DataFields()

collection, an error should be raised if the field is not in the collection
(I haven't checked to ensure it is trappale - if not, you would have to
iterate through the collction and try to match each fieldname in the data
source).

However, as a further complication, Word may alter the data source field
names when it inserts them as MERGEFIELD fields, if
a. they contain certain characters
b. they are over a certain length (over around 31 characters I think)

I don't have a list of the relevant rules or code that deals with them. I
suspect merge documents where renaming occurs are relatively rare.

Peter Jamieson
> Hi all,
>
[quoted text clipped - 17 lines]
>
> Thanks

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.