How to disable 'Check' popup when merge fields are incorrect

B

Brent

I really hope someone has an answer to this.

I'm writing an auto-faxing program which scans our registration
database every minute and uses Word to perform merges which print to a
third party fax program.

Co-workers of mine will create merge templates whose merge fields will
inevitably be old or just incorrect (as they have been with email
templates in the past) and cause the Check dialog box to display to
prompt a user to remove bad merge fields.

Having a dialog box display when you're trying to automate presents a
problem as the code halts. I can't have this. One work around that
I've tried is to parse the 'code' property of the mailmerge field and
compare a substring of it to the data fields. If there isn't a match
then I throw an error. However, this method is not full proof (user
edits in the merge fields could break the parsing) and I would rather
have Word just throw an error if the merge fields are incorrect.

I just need to report the error to a log and not have the dialog box
display.

My code in VB.NET follows:

Public Function OpenMailMerge(ByVal DataFileName As String, ByVal
strTemplatePathName As String, ByRef MergedDoc As Word.Document, ByVal
PrintToFaxFacts As Boolean) As Boolean

Dim doc As Word.Document
Dim i, j As Integer
Dim mMergeDataSource As Word.MailMergeDataSource
Dim wb As Object

Try
If IO.File.Exists(strTemplatePathName) = False Then
Throw New Exception("MS Word Template path does not
exist!")
End If
If strTemplatePathName.EndsWith("dot") = False Then
Throw New Exception("Template file is not a valid MS
Word Template!")
End If

'NOTE: comma delimited file already generated
If IO.File.Exists(DataFileName) = False Then
Throw New Exception("Data file path does not exist!")
End If

doc = wrd.Documents.Add(strTemplatePathName)
mMergeDataSource = doc.MailMerge.DataSource
doc.MailMerge.OpenDataSource(DataFileName, , False, True,
True, False)

'visible only for diagnosing purposes
wrd.Visible = True

doc.MailMerge.SuppressBlankLines = True
wrd.Options.PrintBackground = False
doc.MailMerge.Destination =
Word.WdMailMergeDestination.wdSendToNewDocument

If PrintToFaxFacts = True Then
wb = wrd.WordBasic
wb.FilePrintSetup(Printer:="Copia FFMERGE Driver",
DoNotSetAsSysDefault:=1)
End If

'Parse through mailmerge fields and compare to data fields
Dim DataFieldExists As Boolean = False
For i = 1 To doc.MailMerge.Fields.Count
For j = 1 To doc.MailMerge.DataSource.DataFields.Count
If
LCase(doc.MailMerge.Fields.Item(i).Code.Text).IndexOf(" " &
LCase(doc.MailMerge.DataSource.DataFields.Item(j).Name) & " ") > -1
Then
DataFieldExists = True
Exit For
End If
Next

If DataFieldExists = False Then
Throw New Exception("Data field on MS Word
template not found!!!")
End If
DataFieldExists = False
Next

'Try-Catch block doesn't really help as the dialog box
displays
Try
doc.MailMerge.Execute()
Catch ex As Exception
Throw ex
End Try


MergedDoc = wrd.ActiveDocument


Catch exc As Exception
Throw exc
End Try
End Function
 
P

Peter Jamieson

I don't think setting Application.DisplayAlerts helps so AFAIK the best you
could do from VBA (and I can't say I like it) would be to test the merge
using something like:
Sub TestTheMerge()
'Application.DisplayAlerts = wdAlertsNone
With ActiveDocument.MailMerge
.DataSource.FirstRecord = 1
.DataSource.LastRecord = 1
SendKeys "{ESC}"
.Execute pause:=False
End With
' Then you will be left with an output document if there were no problems
with record 1
' but an output document and an error document if there was a problem.
End Sub

However, quite apart from anything else, this does not /guarantee/ success
if, for example, the users are doing tricky things like creating mergefield
field names on the fly by concatenating other fields.

I'd also wonder how you are guaranteeing that the .dot files are not
attached to a data source when you open them - if they are, you will receive
another non-suppressible error.

..
 
B

Brent

Thank you for your response. By the way, I'm using Office 2000 for the
automation, but are there any mailmerge improvements in later
versions? I'm more than a little miffed that I don't have good
programmatic control over mailmerge automation. Of course, I could
just do manual merge by scanning bookmarks and dropping the data in.
Not exactly efficient but at least I could get it to work.
 
P

Peter Jamieson

Well, a lot of things certainly changed between Word 2000 and Word 2002, but
you will have to judge for yourself which of the changes are improvements.

As far as the programmability side is concerned, the main changes are
a. there are now Mailmerge events which fire pre-merge, pre-record,
post-merge etc. and let you manipulate the mail merge main document before
processing each data source record
b. because 2002 has a lot of new stuff to do with connecting to OLEDB data
sources there is now an extra (rather poorly documented)parameter in
OpenDataSource that specifies a connection subtype
c. users can review the list of records to be merged and include/exclude
them individually

However, as far as I know there are no changes to the way Word handles
missing mail merge data sources when the document is opened, names that
aren't in the data source, etc.
Of course, I could
just do manual merge by scanning bookmarks and dropping the data in.
Not exactly efficient but at least I could get it to work

If you want to maintain control that's probably the best way to go.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top