Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.mailmerge.fields
[email protected] dennistiu@yahoo.com is offline
external usenet poster
 
Posts: 1
Default 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   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Peter Jamieson Peter Jamieson is offline
external usenet poster
 
Posts: 4,582
Default 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   Report Post  
Posted to microsoft.public.word.mailmerge.fields
[email protected] dennis@4miles.com is offline
external usenet poster
 
Posts: 1
Default 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

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
MS Word form fields versus mail merge fields [email protected] Mailmerge 4 March 4th 06 05:31 PM
How can I validate a Word document to find out if it is corrupt? John Liungman Microsoft Word Help 4 February 24th 06 09:04 AM
Validate form field Amy Microsoft Word Help 2 October 10th 05 06:26 PM
Validate table contents Peter Hattam Microsoft Word Help 1 July 11th 05 01:21 PM
Word dont have the ability to validate internal link leoman730 Microsoft Word Help 1 March 16th 05 10:47 PM


All times are GMT +1. The time now is 06:39 PM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"