Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.word.mailmerge.fields
|
|||
|
|||
Validate Merge Fields
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? |
#2
Posted to microsoft.public.word.mailmerge.fields
|
|||
|
|||
Validate Merge Fields
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 wrote in message ups.com... 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? |
#3
Posted to microsoft.public.word.mailmerge.fields
|
|||
|
|||
Validate Merge Fields
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 |
Reply |
Thread Tools | |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
MS Word form fields versus mail merge fields | Mailmerge | |||
How can I validate a Word document to find out if it is corrupt? | Microsoft Word Help | |||
Validate form field | Microsoft Word Help | |||
Validate table contents | Microsoft Word Help | |||
Word dont have the ability to validate internal link | Microsoft Word Help |