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
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