Thanks for the sample code… My code is in VB so I had to convert it. I get no errors, however no data at all is saved into the template… Does the conversion look correct to you? I feel something in the handler may have gotten converted wrong.
Sub
Dim doc As New Document("d:\hart11\templates\canvassing2.doc")
Dim adapterTemplate1 As New SQLDataAdapter("SELECT ‘’ AS SalesAmount, CompanyTxt AS Company, ‘’ AS EmploymentThisSite, FaxDirectTxt AS Fax, ‘’ AS Ownership, PhoneDirectTxt AS Phone, ‘’ AS [Plant/FacilitySize], PhoneGeneralTxt AS Phone2, WebURL AS WebAddress, ‘’ AS YearEstablished, ‘’ AS OwnsRents, Address1Txt AS Address1, Address2Txt AS Address2, CityTxt As City, StateCode AS State, PostalCodeTxt As Zip, ShippingAddress1Txt AS AltAddress, ShippingCityTxt As AltCity, ShippingStateCode AS AltState, ShippingPostalCodeTxt AS AltZip, CountyTxt AS County, ContactSIC1Code AS SIC1, ‘’ AS SIC1Description, ContactSIC2Code AS SIC2, ‘’ AS Sic2Description, ContactSIC3Code AS SIC3, ‘’ AS Sic3Description, ContactParentCompanyTxt AS Parent, PrefixTxt AS Prefix, FirstTxt AS First, LastTxt As Last, SuffixTxt AS Suffix, TitleTxt AS Title, DivisionTxt AS Division, ExportNameTxt AS Canvassed, ExportPreparedFor AS Salesperson FROM ViewContact INNER JOIN ExportDetail ON ExportDetail.ExportPrimaryRecordID = ViewContact.ContactID INNER JOIN ExportMaster ON ExportMaster.ExportID = ExportDetail.ExportID WHERE ExportDetail.ExportID=’" & Request.QueryString("ID") & "’ UNION SELECT [Sales Amount] AS SalesAmount, Company, EmploymentThisSite, REPLACE(Fax, ‘-’, ‘’) AS Fax, Ownership, REPLACE(Phone,’-’,’’) AS Phone, [Plant/FacilitySize], TollFree AS Phone2, WebAddress, YearEstablished, OwnsRents, MailAddress AS Address1, ‘’ AS Address2, MailCity AS City, MailState AS State, MailZipPlusExtension AS Zip, StreetAddress AS AltAddress, StreetCity AS AltCity, StreetState AS AltState, StreetZip AS AltZip, StreetCounty AS County, PrimarySic AS SIC1, PrimarySicDescription AS SIC1Description, Sic2 AS SIC2, Sic2Description, Sic3 AS SIC3, Sic3Description, ParentName AS Parent, RankedExecutive1Prefix AS Prefix, RankedExecutive1FirstName AS First, RankedExecutive1LastName AS Last, RankedExecutive1Suffix AS Suffix, RankedExecutive1TitleDescription AS Title, ‘’ AS Division, ExportNameTxt AS Canvassed, ExportPreparedFor AS Salesperson FROM ContactHarris INNER JOIN ExportMaster ON ExportMaster.ExportID = ContactHarris.ExportID WHERE ContactHarris.ExportID=’" & Request.QueryString("ID") & "’ AND ContactHarris.Duplicate=’’ ORDER BY City ASC", myConnection)
Dim dtTemplate1 As New DataTable()
adapterTemplate1.Fill(dtTemplate1)
'doc.MailMerge.Execute(dtTemplate1)
'doc.MailMerge.DeleteFields()
doc.MailMerge.FieldMergingCallback = New HandleMergeField()
doc.MailMerge.ExecuteWithRegions(dtTemplate1)
doc.Save(SavePath)
End Sub
Private Class HandleMergeField
Implements IFieldMergingCallback
Private Sub FieldMerging(args As FieldMergingArgs) Implements IFieldMergingCallback.FieldMerging
If args.FieldName = "City" Then
mRecordIndex += 1
Dim currentCity As String = args.FieldValue.ToString()
If (Not String.IsNullOrEmpty(mCity) AndAlso mCity <> currentCity) OrElse mRecordIndex > 2 Then
mRecordIndex = 0
' Move to the paragraph where the mergefied is located and set PageBreakBefore property.
Dim builder As New DocumentBuilder(args.Document)
builder.MoveToField(args.Field, False)
builder.CurrentParagraph.ParagraphFormat.PageBreakBefore = True
End If
mCity = currentCity
End If
End Sub
Private Sub ImageFieldMerging(e As ImageFieldMergingArgs) Implements IFieldMergingCallback.ImageFieldMerging
' Do nothing.
End Sub
Private mCity As String Private mRecordIndex As Integer
End Class