Free Support Forum - aspose.com

Merge fields with multiple regions

Dear support,

I have been using Aspose Word to convert into PDF.

Now I need to 2 tables in the same doc. I assumed that I can create the Merge fields one more time by giving the TableStart and TableEnd fields again. In the code I filled another Dataset and put another ExecuteWithRegions again. But I get an error saying that the table doesnot exist in the Dataset. The following is the code and the commented portion is the second Region. (removed SQL statement to reduce the lines. rgds/

Dim strSQL

Dim myCommand As OracleDataAdapter

Dim DS As DataSet

Dim doc As New Document(Request.MapPath(Request.ApplicationPath) & "\QoutationSamples\LCL_Pathfinder_Word_Template_1.dot")

strSQL = " ..."

myCommand = New OracleDataAdapter(strSQL, myConnection)

DS = New DataSet()

myCommand.Fill(DS, "LCLGUIDE")

If DS.Tables("LCLGUIDE").Rows.Count > 0 Then

doc.MailMerge.ExecuteWithRegions(DS)

End If

DS.Clear()

'myCommand.Dispose()

' ---- second region

'DS.Clear()

'strSQL = "...."

'myCommand.Fill(DS, "LCLGUIDEPOINTS")

'If DS.Tables("LCLGUIDEPOINTS").Rows.Count > 0 Then

' doc.MailMerge.ExecuteWithRegions(DS)

'End If

'myCommand.Dispose()

' ---- second region

Please attach your template document. I will check what the problem is.

Dear Vladmir,

Pl find attached. In principle is it possible to use multiple Regions?

rgds

Pavan

attached again…

It seems that when you call ExecuteWithRegions second time - it searches for "LCLGUIDE" table first as the mergefields for this table still exist in the document. When it is not found in the dataset Aspose.Words breaks.

Try to fill dataset with both tables and call ExecuteWithRegions only once.

Dear Vladmir,

Thanx for the help. While I check this I have another issue for you. In the same Word file I sent you if an image is placed in the Header, it is ignored in the PDF. Could pl check on this. I am enclosing the file again with the image in the header.

rgds

Pavan

From what I see the image is not ignored but rather misalligned. It is positioned inline in your template but it is better to set it floating with explicit positioning to avoid positioning problems.

I am sending the corrected template along with PDF made from it.

Also please make sure that you are using the latest versions of Aspose libraries.

I am using Aspose Word 2.4.0.0 Aspose PDF 2.2.0.0

I saw that there is an update for Aspose Word to Aspose Words. Do I need to do it or any other update?

rgds

Pavan

Yes, it is always a good idea to update to the latest version. Please contact Aspose.Purchase to find out how to do it.

Dear Vladimir,

I tried but now none of the two tables are shown. No error but the placeholders are only shown. I am using 2 Dataadapters to fill into the same dataset. Here's my code and attched is the word and pdf. Thanx in advance

Dim strSQL1

Dim strSQl2

Dim myCommand1 As OracleDataAdapter

Dim myCommand2 As OracleDataAdapter

Dim DS As DataSet

Dim doc As New Document(Request.MapPath(Request.ApplicationPath) & "\QoutationSamples\LCL_Pathfinder_Word_Template_1.dot")

strSQL1 = "..."

'----

strSQl2 += " .."

myCommand2 = New OracleDataAdapter(strSQl2, myConnection)

DS = New DataSet()

myCommand2.Fill(DS, "LCLGUIDEPOINTS")

'---

myCommand1 = New OracleDataAdapter(strSQL1, myConnection)

myCommand1.Fill(DS, "LCLGUIDE")

If DS.Tables("LCLGUIDE").Rows.Count > 0 Then

doc.MailMerge.ExecuteWithRegions(DS)

End If

I have tested MailMerge of your template document with the following code:

string filename = Application.StartupPath + @"\Brief\LCL_Pathfinder_Word_Template_1.dot";

Document doc = new Document(Application.StartupPath + @"\Brief\LCL_Pathfinder_Word_Template_1.dot");

string pol = "POL";

string direct = "DIRECT";

string pod = "POD";

DataTable table1 = new DataTable("LCLGUIDE");

table1.Columns.Add(pol);

table1.Columns.Add(direct);

table1.Columns.Add(pod);

for(int i = 0; i<10; i++)

{

DataRow row1 = table1.NewRow();

table1.Rows.Add(row1);

row1[0] = pol + i;

row1[1] = direct + i;

row1[2] = pod + i;

}

DataTable table2 = new DataTable("LCLGUIDEPOINTS");

table2.Columns.Add(pol);

table2.Columns.Add(pod);

for(int i = 0; i<10; i++)

{

DataRow row2 = table2.NewRow();

table2.Rows.Add(row2);

row2[0] = pol + i;

row2[1] = pod + i;

}

DataSet ds = new DataSet();

ds.Tables.Add(table1);

ds.Tables.Add(table2);

doc.MailMerge.ExecuteWithRegions(ds);

doc.Save(Application.StartupPath + @"\Brief\LCL_aftermerge.doc");

And it works ok.

Please check the correctness of the data in the dataset after filling it. You can use debug mode and quick watch command for that purpose.