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 / March 2007

Tip: Looking for answers? Try searching our database.

Validate Merge Fields

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dennistiu@yahoo.com - 26 Feb 2007 20:44 GMT
Basically I am using VBA to merge fields. I want to know if there is a
way to validate the merge fields specified in the document and the
available merge fields specified in the datasource:

           Dim wrdApp As New
Microsoft.Office.Interop.Word.Application
           wrdApp.ActiveDocument.MailMerge.OpenDataSource( *** My
data source information *** )
           wrdApp.ActiveDocument.MailMerge.Destination =
wdSendToNewDocument
           wrdApp.ActiveDocument.MailMerge.Execute()

I want to validate all the merge fields first before callign execute.
Is this possible?
The problem is if I have a specified merge field in the document that
is not in the datasource. Word displays a popup to warn me about the
invalid field. Since I am doing everything in the background, I have
no way of knowing the popup.

I see a check method but what does this do? It checks and then what?
It doesnt return anything or any boolean value to me

wrdApp.ActiveDocument.MailMerge.Check() - cant seem to know what this
will do?

Anyone have any ideas or suggestions?
Peter Jamieson - 27 Feb 2007 18:28 GMT
Looks more like VB.NET to me, but anyway...

I think you will probably have to iterate through all the merge fields in
the ocument and ensure that each MERGEFIELD name is in the
ActiveDocument.MailMerge.DataSource.DataFields collection.

Iterating through all the fields is in itself non-trivial but the following
VBA code should get most of them:

Dim objStory As Range
Dim objField As Field

For Each objStory In ActiveDocument.StoryRanges
 For Each objField In objStory.Fields
   ' do what you want to the field here. You can test objField.Type
   ' to process specific field types
 Next
' The header/footer ranges can be linked in a way
 ' that is not revealed by the outer For Each
 ' so we have to do the following
 While Not (objStory.NextStoryRange Is Nothing)
   Set objStory = objStory.NextStoryRange
   For Each objField In objStory.Fields
     ' do what you want to the field here
   Next
 Wend
Next objStory

Set objStory = Nothing
Set objField = Nothing

To test the field against the Datafields collection you would probably need
something like:

Dim strFieldName As String
Dim bFieldFound As Boolean
For Each objField In objStory.Fields
   If objField.Type = wdFieldMergeField Then
     strFieldName = Trim(Mid(Trim(objField.Code), 12))
     If Left(strFieldName, 1) = Chr(34) Then
       strFieldName = Mid(strFieldName, 2, InStr(2, strFieldName & Chr(34),
Chr(34)) - 2)
     Else
       strFieldName = Left(strFieldName, InStr(1, strFieldName & " ", "
") - 1)
     End If
     With ActiveDocument.MailMerge.DataSource
       bFieldFound = False
       For i = 1 To .DataFields.Count
         If strFieldName = .DataFields(i).Name Then
           bFieldFound = True
           Exit For
         End If
       Next
     End With
     ' deal with a mismatch. Up to you whether you try
     ' to deal with all the mismatches etc.
     If Not bFieldFound Then
       MsgBox "field not found"
       Exit Sub
     End If
   End If

Peter Jamieson

> Basically I am using VBA to merge fields. I want to know if there is a
> way to validate the merge fields specified in the document and the
[quoted text clipped - 22 lines]
>
> Anyone have any ideas or suggestions?
dennis@4miles.com - 01 Mar 2007 18:23 GMT
Peter

Thank you for the response. Yes it is in vb.net. Sorry about the
confusion.
I will try what you have suggested and let you know if it works.

Dennis

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.