We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Need help with pasting HTML to Document

i'm trying to put some HTML part from a rte editor to my generated word document.

Most of the document ist pasted using MailMerge. At one Point i want to add html data so i got a placeholder there: <<TempPlaceHolderForHTML>>

this is getting my placeholdertext:

Dim selection As TextSelection = doc.Find(New System.Text.RegularExpressions.Regex("<<TempPlaceHolderForHTML>>"))

with this im pasting my html to another document. i thought this would be the best to add the parts to the document:

Private Function HTMLtoRTF(htmltext As String) As EntityCollection
            Dim document As WordDocument = New WordDocument()
            Dim section As IWSection = document.AddSection()
            Dim para As IWParagraph = section.AddParagraph()

            document.XHTMLValidateOption = XHTMLValidationType.Transitional
            Return section.ChildEntities
End Function

unfortunately this is not working: 

Dim foo2 As New TextBodyPart(selection)
                Dim blubb = HTMLtoRTF(currentUser.ProfilInteressen.Text)

                For Each fob In blubb

Does anyone know hot to continue at this point or is there maybe an easy way im overseeing?

Kind regards,

4 Replies

LC lc August 27, 2015 11:04 AM UTC

i found a solution, but there should be some easier approach to this problem:

Private Function FindCell(doc As WordDocument, searchString As String) As WTableCell
            For Each it As IWSection In doc.Sections
                For Each blu As WTable In it.Tables
                    For Each blu2 As WTableRow In blu.Rows
                        For Each cl As WTableCell In blu2.Cells
                            Dim temp = blu2.Cells.Count
                            For Each pg As WParagraph In cl.Paragraphs
                                If pg.Text.Contains(searchString) Then
                                    Return cl
                                End If
            Return Nothing
        End Function

Dim cell = FindCell(doc, "<<TempPlaceHolderForHTML>>")
                If Not IsNothing(cell) Then
                    Dim pgCount = cell.Paragraphs.Count()
                    For i As Integer = pgCount - 1 To 0 Step -1
                    Dim tempParagraph = cell.AddParagraph()
                End If

DV Divyalakshmi Vasudevan Syncfusion Team August 28, 2015 07:34 AM UTC

Hi IC,

Thank you for your interest in Syncfusion products.

Kindly make use of below code snippet to meet your requirement by using Find and Replace functionality of DocIO .


'Loads the Word Document

            Dim document As New WordDocument(filename)

            'Find the <<TempPlaceHolderForHTML>> text

            Dim selections As TextSelection() = document.FindAll(New System.Text.RegularExpressions.Regex("<<TempPlaceHolderForHTML>>"))

            For selectionIndex As Integer = 0 To selections.Length - 1

                Dim selection As TextSelection = selections(selectionIndex)

                Dim OwnerParagraph As WParagraph = selection.GetAsOneRange().OwnerParagraph

                'Retrieve the Owner cell

                If TypeOf OwnerParagraph.OwnerTextBody Is WTableCell Then

                    Dim cell As WTableCell = TryCast(OwnerParagraph.OwnerTextBody, WTableCell)

                    'Clear the paragraphs inside the cell


                    Dim paragraph As IWParagraph = cell.AddParagraph()

                    'Append the HTML string to the paragraph


                End If


            'Saves the Word document to disk


Kindly refer the below UG documentation link to get more information about Find and Replace:



Please let us know if you have any other questions.

Divyalakshmi V

LC lc August 28, 2015 11:49 AM UTC

Hi Divyalakshmi,
thanks this looks much easier than my code. selection.GetAsOneRange().OwnerParagraph was the connection i could not find in the samples.


SV Sivasubramani V Syncfusion Team August 31, 2015 07:22 AM UTC

Hi IC,

Thank you for your update.

We have prepared a sample to find and replace the particular content with html string using find and replace functionality of DocIO. Please find the sample from below link

Sample link:


Please let us know if you have any other questions.


Live Chat Icon For mobile
Up arrow icon