Free Support Forum - aspose.com

Mail Merge and Merge regions

Please see attached document. I am trying to do a mail merge and a merge with regions on the same document using the code below. The merge works fine, but the merge region bit shows «TableStart:dtAnimal»Fred«TableEnd:dtAnimal». It is not showing any of the additional records. Any ideas?

Dim NewDoc As New Aspose.Word.Document(Server.MapPath("STCCert.DOC"))

NewDoc.MailMerge.RemoveEmptyParagraphs = True

SQLString = "Select * from staapplication where appno = '" & NewAppNo & "'"

cmdApplication.CommandText = SQLString

cmdApplication.Connection = conSTC

daApplication.SelectCommand = cmdApplication

daApplication.Fill(dtApplication)

SQLString = "Select * from staapplicant where appno = '" & NewAppNo & "'"

cmdApplicant.CommandText = SQLString

cmdApplicant.Connection = conSTC

daApplicant.SelectCommand = cmdApplicant

daApplicant.Fill(dtApplicant)

SQLString = "Select * from staanimal where appno = '" & NewAppNo & "'"

cmdAnimal.CommandText = SQLString

cmdAnimal.Connection = conSTC

daAnimal.SelectCommand = cmdAnimal

daAnimal.Fill(dtAnimal)

SQLString = "Select * from staallergen where appno = '" & NewAppNo & "'"

cmdAllergen.CommandText = SQLString

cmdAllergen.Connection = conSTC

daAllergen.SelectCommand = cmdAllergen

daAllergen.Fill(dtAllergen)

IssueDate = Today

ExpiryDate = Today.AddDays(364)

'Fill in details from STAApplication

If dtAnimal.Rows.Count > 0 Then

NewDoc.MailMerge.ExecuteWithRegions(dtAnimal)

End If

'Fill in allergen details

If dtAllergen.Rows.Count > 0 Then

Dim row As DataRow

Dim i As Integer

Dim x As Integer

Dim BookMarkName As String

i = 1

x = 0

For Each row In dtAllergen.Rows

i = i + 1

x = x + 1

Next

End If

' Create an array of the field names

'NB Field Names are case sensitive - have to be exactly the same as in word doc

Dim fieldNames() As String = {"AuthNo", "VetName", "Condition", "Practice", "Address1", "Address2", "City", "County", "Postcode", "VMDVetNo", "ExpiryDate", "IssueDate", "RCVSNo", "SVetName", "SVetAddress1", "SVetAddress2", "SVetCity", "SVetCounty", "SVetPostcode", "ImporterName", "ImporterAddress1", "ImporterAddress2", "ImporterCity", "ImporterCounty", "ImporterPostcode", "CountryOrigin", "PrevSTC", "OwnerName", "OwnerAddress", "AnimalName", "Species", "Breed", "Weight", "NoAnimals", "Dosage", "HoldingAddress1", "HoldingAddress2", "HoldingCity", "HoldingCounty", "HoldingPostcode", "FoodProducing", "Justification", "ProductName", "MaNo", "Manufacturer", "Active1", "Active2", "Active3", "PharmForm", "Indications", "Strength", "StrengthUnits", "TotalAmount", "AmountUnits"}

' Create an array of the field values

Dim fieldValues() As Object = {dtApplication.Rows(0).Item("Appno"), dtApplicant.Rows(0).Item("VetName"), dtApplication.Rows(0).Item("stacondition"), dtApplicant.Rows(0).Item("Practice"), dtApplicant.Rows(0).Item("Address1"), dtApplicant.Rows(0).Item("Address2"), dtApplicant.Rows(0).Item("City"), dtApplicant.Rows(0).Item("County"), dtApplicant.Rows(0).Item("Postcode"), dtApplicant.Rows(0).Item("VetNo"), ExpiryDate, IssueDate, dtApplicant.Rows(0).Item("RCVSNo"), dtApplicant.Rows(0).Item("SVetName"), dtApplicant.Rows(0).Item("SVetAddress1"), dtApplicant.Rows(0).Item("SVetAddress2"), dtApplicant.Rows(0).Item("SVetCity"), dtApplicant.Rows(0).Item("SVetCounty"), dtApplicant.Rows(0).Item("SVetPostcode"), dtApplicant.Rows(0).Item("ImporterName"), dtApplicant.Rows(0).Item("ImporterAddress1"), dtApplicant.Rows(0).Item("ImporterAddress2"), dtApplicant.Rows(0).Item("ImporterCity"), dtApplicant.Rows(0).Item("ImporterCounty"), dtApplicant.Rows(0).Item("ImporterPostcode"), dtApplication.Rows(0).Item("staCountryOrigin"), dtApplication.Rows(0).Item("PrevSTANo"), dtAnimal.Rows(0).Item("animalOwner"), dtAnimal.Rows(0).Item("animalAddress"), dtAnimal.Rows(0).Item("AnimalName"), dtApplication.Rows(0).Item("staSpecies"), dtAnimal.Rows(0).Item("AnimalBreed"), dtAnimal.Rows(0).Item("animalWeight"), dtApplication.Rows(0).Item("staNoAnimals"), dtAnimal.Rows(0).Item("animalDosage"), dtApplicant.Rows(0).Item("HoldingAddress1"), dtApplicant.Rows(0).Item("HoldingAddress2"), dtApplicant.Rows(0).Item("HoldingCity"), dtApplicant.Rows(0).Item("HoldingCounty"), dtApplicant.Rows(0).Item("HoldingPostcode"), dtApplication.Rows(0).Item("staFoodProducing"), dtApplication.Rows(0).Item("staJustification"), dtApplication.Rows(0).Item("staProduct"), dtApplication.Rows(0).Item("staMAorigin"), dtApplication.Rows(0).Item("staManufacturer"), dtApplication.Rows(0).Item("staActive1"), dtApplication.Rows(0).Item("staActive2"), dtApplication.Rows(0).Item("staActive3"), dtApplication.Rows(0).Item("staPharmForm"), dtApplication.Rows(0).Item("staIndications"), dtApplication.Rows(0).Item("staStrength"), dtApplication.Rows(0).Item("staStrengthUnits"), dtApplication.Rows(0).Item("staTotalAmount"), dtApplication.Rows(0).Item("staAmountUnits")}

'Fill the fields in the document with user data.

NewDoc.MailMerge.RemoveEmptyParagraphs = True

NewDoc.MailMerge.Execute(fieldNames, fieldValues)

'Save the document

DocName = Right(NewAppNo, 5) & ".doc"

NewDoc.Save(DocName, Aspose.Word.SaveFormat.FormatDocument, Aspose.Word.SaveType.OpenInWord, Response)

End Sub

Many thanks

Shellie

I think you need to add this line of code before MailMerge.ExecuteWithRegions:

dtAnimal.Name = "dtAnimal";

It looks like the mail merge engine cannot match the table you passing to it with the merge region in the document it needs to fill. The table name property is the way to tie the two.