Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Thu Jul 13, 2017 2:40 pm

I am doing a mail merge using a dataset that is coming from a SQL database.

The data that I am retrieving is stored in the database with some HTML markups in it, example:

One of my merge fields contains this text:
<ul><li><strong>Bullet Title 1</strong>Bullet content 1.</li><li><strong>Bullet Title 2</strong>Bullet content 2.</li></ul>

Another one of my merge fields contains this text:
<strong>List of employees</strong><br />First Name< br />Second Name<br />Third Name


But when the document renders, the fields show the html tags instead of rendering the formatted text.

How can I accomplish the merge field to show the formatted text instead of the html tags?

Code: Select all
   Public Shared Sub FieldMerge(ByVal ds As DataSet, ByRef doc As Document)

            '**********************************
            'Merging
            '**********************************
            Dim fieldNames As New List(Of String)()
            Dim fieldValues As New List(Of String)()


            For Each dtcolumn As DataColumn In ds.Tables(0).Columns
                'Add the values to the list of string
                fieldNames.Add(dtcolumn.ColumnName)

                '**** THIS TEXT COULD HAVE SOME HTML TAGS - HOW TO RENDER THIS IN THE MERGE FIELD???? *****'
                fieldValues.Add(ds.Tables(0).Rows(0)(dtcolumn.ColumnName).ToString)

            Next

            Dim fieldNamesArray As String() = fieldNames.ToArray()
            Dim fieldValuesArray As String() = fieldValues.ToArray()

            'Execute the merge
            doc.MailMerge.Execute(fieldNamesArray, fieldValuesArray)


        End Sub

mtabares
 
Posts: 11
Joined: Wed May 31, 2017 7:54 pm

Fri Jul 14, 2017 3:13 am

Dear mtabares,

Thanks for your inquiry.
Here is sample code for your kind reference.
Code: Select all
Public Shared Sub MergeWithHTML()
   Dim document = New Spire.Doc.Document("F:\11093.docx")
   Dim filedNames As String() = New String() {"Htmlname", "Htmlage"}
   Dim filedValues As String() = New String() {"HtmlText1", "HtmlText2"}
   AddHandler document.MailMerge.MergeField, AddressOf MailMerge_MergeField
   document.MailMerge.Execute(filedNames, filedValues)
   document.SaveToFile("11093.docx", FileFormat.Docx)
   System.Diagnostics.Process.Start("11093.docx")
End Sub
Public Shared Sub MailMerge_MergeField(sender As Object, args As MergeFieldEventArgs)
   'All merge fields that expect HTML data should be marked with some prefix, e.g.'Html'.
   If args.FieldName.StartsWith("Html") Then
      args.CurrentMergeField.OwnerParagraph.AppendHTML(TryCast(args.FieldValue, String))
      args.Text = ""
   End If
End Sub

Morever, there will be OutOfRange exception when the html text contains the list tag <ul>, and I have posted it to our Dev team, we will inform you once there is any progress. And we firstly suggest you place placeholder into your document and then use replace method, you could refer to the code I provided in the thread.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Fri Jul 21, 2017 2:28 pm

Thank you so much, this worked perfectly.

The only change I made was to use regular expressions instead of the "html" string appended to the field.

Thank you so much,


Code: Select all
    Public Shared Sub MailMerge_MergeField(sender As Object, args As MergeFieldEventArgs)
            'Use regular expressions to find those fields with html
            Dim str As String = TryCast(args.FieldValue, String)
            Dim SearchExpression As New Regex("<.*?>", RegexOptions.IgnoreCase Or RegexOptions.Compiled)
            Dim MatchedPattern As Match
            MatchedPattern = SearchExpression.Match(Str)
            If MatchedPattern.Success Then
                'render html
                args.CurrentMergeField.OwnerParagraph.AppendHTML(TryCast(args.FieldValue, String))
                args.Text = ""
            End If
        End Sub

mtabares
 
Posts: 11
Joined: Wed May 31, 2017 7:54 pm

Mon Jul 24, 2017 1:48 am

Dear mtabares,

Thanks for your feedback.
And I am glad to tell you that the issue with list tag has been resolved. We will inform you when the hotfix of Spire.Office is available.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Fri Aug 18, 2017 1:47 am

Dear mtabares,

Thanks for waiting.
Glad to inform you that the exception issue has been in Spire.Office Platinum (Hot Fix) Version:2.16.12, welcome to have a test. Looking forward to your feedback.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.Doc