Issue with MailMerge - Returns Blank Document


#1

I am currently working on a MailMerge function and it appears that after I try to do a MailMerge.Execute(datatable) that the document that is returned is empty.

If I comment out the MailMerge.Execute(datatable) line then the template is returned and I can see the fields.

I think that if I happen to pass in an empty datatable then it returns a blank document for some reason. If I pass a datatable with multiple rows, it repeats the contents of the document for each row.

For example, if I have a 1 page doc and 3 rows in my datatable, the doc I receive will have 3 pages.

I'm new to using the datatable and could use some guidance.

Thanks!

Josh


#2

I just tried this by using the MailMerge.Execute(fieldnames,fieldvalues) and that worked fine.

I'm trying to use a datatable so I can change which fields to merge based on which letter was picked from a list.

Thanks,

Josh


#3

Hard to say what the problem is. MailMerge should never produce the effects like you have described. It will be fine if you attach your project with templates here, then I can check what is going on.


#4

Here is the code that I use to perform the mailmerge:

Public Function GenerateLetter(ByVal ARIData As String, ByVal LetterData As System.Data.DataTable) As Document

Dim ARI() As String = ARIData.Split(";")

Dim doc As Document = New Document(ARI(0).ToString.Trim)

' Create an array of the field names

Dim fieldNames() As String = {"Name", _

"CareOf", _

"Address", _

"CityStateZip", _

"APN", _

"DocNumber", _

"RecordingDate", _

"SitusInfo", _

"UserName", _

"UserTitle"}

' Create an array of the field values

Dim fieldValues() As Object = { _

"name", _

"careof", _

"address", _

"citystatezip", _

"apn", _

"transferid", _

"transferdate", _

"situsinfo", _

"username", _

"jobtitle" _

}

'Set up the event handler for image fields and perform mail merge.

AddHandler doc.MailMerge.MergeImageField, AddressOf HandleMergeImage

'Fill the fields in the document with user data.

doc.MailMerge.Execute(fieldNames, fieldValues)

'doc.MailMerge.Execute(LetterData)

doc.MailMerge.DeleteFields()

Return doc

End Function

Here is the code I use for sending the document back to the user:

Dim aridata As String

aridata = filepath & ";" & filename & ".doc"

Dim dt As New DataTable

dt = Exemptions.PopulateLetterFields(CInt(lblSelectedExemptionVal.Text), CInt(CType(e.Item.FindControl("lblLetterID"), Label).Text), "Preview")

Dim PreviewDoc As Document

PreviewDoc = Exemptions.GenerateLetter(aridata, dt)

PreviewDoc.Save(filename & ".doc", SaveFormat.FormatDocument, SaveType.OpenInWord, Me.Response)

To generate my DataTable I use the following code based on data in a database:

Public Function PopulateLetterFields(ByVal ExemptionID As Integer, ByVal LetterID As Integer, ByVal Mode As String) As DataTable

Dim tblLetterData As DataTable

tblLetterData = New DataTable("LetterData")

Dim FieldName As DataColumn = New DataColumn("FieldName")

FieldName.DataType = System.Type.GetType("System.String")

tblLetterData.Columns.Add(FieldName)

Dim FieldData As DataColumn = New DataColumn("FieldData")

FieldData.DataType = System.Type.GetType("System.String")

tblLetterData.Columns.Add(FieldData)

Dim dsTemp As New DsExemptions

PopulateDS(dsTemp, "ExemptionLettersData", ExemptionID, 0, 0, LetterID)

PopulateDS(dsTemp, "ExemptionLetterFields", 0, 0, 0, 0)

Dim DataRow As DsExemptions.ExemptionLettersDataRow

For Each DataRow In dsTemp.ExemptionLettersData

Dim fieldRow As ExemptionFramework.DsExemptions.ExemptionLetterFieldsRow

fieldRow = dsTemp.ExemptionLetterFields.FindByFieldID(DataRow.FieldID)

Dim tmpRow As DataRow = tblLetterData.NewRow

tmpRow.Item("FieldName") = fieldRow.FieldName

Select Case Mode

Case "Preview"

Select Case fieldRow.FieldName

Case "Name"

tmpRow.Item("FieldData") = "Name"

Case "CareOf"

tmpRow.Item("FieldData") = "CareOf"

Case "Address"

tmpRow.Item("FieldData") = "Address"

Case "CityStateZip"

tmpRow.Item("FieldData") = "CityStateZip"

Case "APN"

tmpRow.Item("FieldData") = "APN"

Case "Situs"

tmpRow.Item("FieldData") = "Situs"

Case "DocNumber"

tmpRow.Item("FieldData") = "DocNumber"

Case "FinalText"

tmpRow.Item("FieldData") = "FinalText"

Case "RespondNote"

tmpRow.Item("FieldData") = "RespondNote"

Case "NoteLine1Text"

tmpRow.Item("FieldData") = "NoteLine1Text"

Case "NoteLine2Text"

tmpRow.Item("FieldData") = "NoteLIne2Text"

Case "UserName"

tmpRow.Item("FieldData") = "UserName"

Case "UserInitials"

tmpRow.Item("FieldData") = "UI"

Case Else

tmpRow.Item("FieldData") = ""

End Select

Case "Data"

End Select

tblLetterData.Rows.Add(tmpRow)

Next

Return tblLetterData

End Function

I have attached the template file that I am using.

Please let me know if you need more info.


#5

Miklovan,

Have you had a chance to try the code I posted? Is there anything unusual about it?

Please let me know.

Thanks,

Josh


#6

The PopulateLetterFields function is rather obscure. Try to preview tblLetterData contents before doing MailMerge.Execute in debug mode. Try to check the value of PreviewDoc.GetText() before saving document to the response. Also save the PreviewDoc locally on test server. It should not be empty.

If nothing helps please send the complete project here. I will check what could be done.


#7

I have checked the data that is in the tblLetterData table and it contains the correct data. Is there a specific formatting that I need to use for the column headers to specify which column is the fieldname and which is the data?

I also tried displaying the contents of the .GetText() in an alert box and it returned an error. When placing the data in a label it shows me the contents from 3 repititions of the template.

What exactly do you need me to send you? I can try to remove what you won't need.

Thanks,


#8

If you are using the DataTable source for MailMerge then it should be organized so that column names were similar to mergefield names and records in the data row were data for mergefield fill.

Two-column datatable with mergefiled names in one column and data in another won't do. You should use string arrays instead.

The following behavior is normal and documented in API Reference:

When MailMerge object reaches end of the main document and there are still more rows in the DataTable, it copies entire content of the main document and appends it to the end of the destination document using a section break as a separator.


#9

I had read through the whole tutorial on how to do a MailMerge and I guess I missed the documentation on the method for DataTables (or at least I didn't remember it).

I changed to using a single row and columns for all the data and that resolved the issue.

Thanks for your assistance and sorry to take up your time.

Thanks,

Josh