Free Support Forum - aspose.com

Region merge with HTML

I'm doing some "mail merging" with regions ('MERGEFIELD TableStart:MyTableName and TableEnd:MyTableName).

Some of the data columns from the database have HTML codes and are simply inserted into the mergefields as text when I perform the merge ( doc.MailMerge.ExecuteWithRegions(dt) ).

Is it possible somehow to have the HTML data from selected columns (in the datatable dt) inserted into the mergefields as formatted HTML (so that the database column content "heading" will appear as heading and and as heading in the final word document?

...or will I need to do the mail merging differently?

/Morten

Hi

Thanks for your request. Please try to use the following code.

Document doc = new Document(@"065_89266_mulmad\in.doc");

doc.MailMerge.MergeField += new MergeFieldEventHandler(MailMerge_MergeField);

doc.MailMerge.ExecuteWithRegions(table1);

doc.Save(@"065_89266_mulmad\out.doc");

}

void MailMerge_MergeField(object sender, MergeFieldEventArgs e)

{

DocumentBuilder builder = new DocumentBuilder(e.Document);

//detect if text is html code

if (e.FieldValue.ToString().Contains("<"))

{

e.Text = string.Empty;

builder.MoveToField(e.Field, false);

builder.InsertHtml(e.FieldValue.ToString());

}

}

I hope that it will help you.

thanks.

here is the dotnet 1.1 vb version:

Imports Aspose.Words.Reporting
Private Sub MailMerge_CheckMergeFieldForHtml(ByVal sender As Object, ByVal e As MergeFieldEventArgs)
Dim builder As New DocumentBuilder(e.Document)
'detect if text is html code
If e.FieldValue.ToString().IndexOf("<") >= 0 Then
e.Text = String.Empty
builder.MoveToField(e.Field, False)
builder.InsertHtml(e.FieldValue.ToString())
End If
End Sub

Hi

For additional information about Mail Merge Events please see API reference.
http://www.aspose.com/Products/Aspose.Words/Api/TheMergeFieldEvent.html

Best regards.

Thanks for the reply I'm using VB (1.1)

But this function is throwing the following error. I can see toString() property with e.FieldValue.ToString().

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Dim doc As Document = New Document(System.IO.Path.Combine(DocPath, "AlternatingRowsDemo.doc"))

AddHandler doc.MailMerge.MergeField, AddressOf MailMerge_CheckMergeFieldForHtml

Private Sub MailMerge_CheckMergeFieldForHtml(ByVal sender As Object, ByVal e As MergeFieldEventArgs)

Line 113:            Dim builder As New DocumentBuilder(e.Document)
Line 114:            'detect if text is html code
Line 115:            If e.FieldValue.ToString().IndexOf("<") >= 0 Then
Line 116:                e.Text = String.Empty
Line 117:                builder.MoveToField(e.Field, False

builder.InsertHtml(e.FieldValue.ToString())

End If

End Sub

Hi

It is strange. This code works fine on my side.

Also see the attachment. There is the simple application.

Best regards.

Thanks for the reply. It is working now. Some how, my template document was crashing. Hence I have used the existing one and modified the columns.

One more question..

We want to print the the table header on the top of each page. (

for AlternatingRowsDemo )

What do I need to do for achieving this? ( in aspose.pdf , there was detailTable.IsFirstRowRepeated = True)

thanks in advance

Hi

Thanks for your request. If you use some template then you should set “Table properties”/”Row”/”Repeat as header row at the top of each page” = true to repeat your header at each page.

Also you can do this programmatically. Set HeadingFormat = true for your heading row. See the following example.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.RowFormat.HeadingFormat = true;

builder.InsertCell();

builder.Write("header");

builder.InsertCell();

builder.Write("header");

builder.EndRow();

builder.RowFormat.HeadingFormat = false;

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

{

builder.InsertCell();

builder.InsertCell();

builder.EndRow();

}

builder.EndTable();

doc.Save(@"208_95405_GauravG\out.doc");

I hope that it will help you.

Best regards.