Link to home
Start Free TrialLog in
Avatar of xav056
xav056

asked on

how to insert html into a wordDoc Openxml

Hello
I have a word document with rich text controls with specific Title/Tags.
I would like to insert text into those controls using the openxml sdk and vb.net, however my text is html formatted lets say <b>some text </b>
If i do the following
 sdtElement.Descendants(Of Text)().FirstOrDefault().Text = "<b> some text </b>"
I see the <b></b> as text, what I would like to happen is have this text in bold.
What control should I use and How would I achieve what I want
Here's the code I am using

Using doc As WordprocessingDocument = WordprocessingDocument.Open("C:\Test.docx", True)
            Dim mainPart As MainDocumentPart = doc.MainDocumentPart()
            Dim sdtElement As SdtElement = mainPart.Document.Descendants(Of SdtElement)().Where(Function(element) element.SdtProperties.GetFirstChild(Of SdtAlias)() IsNot Nothing _
                                 AndAlso element.SdtProperties.GetFirstChild(Of SdtAlias).Val = "test").SingleOrDefault()
            If sdtElement IsNot Nothing Then
                sdtElement.Descendants(Of Text)().FirstOrDefault().Text = "<b> some text </b>"
            End If
        End Using

Open in new window

Avatar of Hairbrush
Hairbrush
Flag of Jersey image

Hi Xav

This would be one way to do it: http://www.xtremevbtalk.com/showthread.php?t=120862 but it only deals with bold and italics!  Beyond that, you'd need an HTML to RTF library.  There are commercial products like http://www.sautinsoft.com/products/html-to-rtf/index.php or if you Google you might find an open source library on somewhere like CodePlex.
Avatar of xav056
xav056

ASKER

this uss vba or com,
I am looking for ways to do this using the openxml sdk, without a word com object
Thank you
ASKER CERTIFIED SOLUTION
Avatar of xav056
xav056

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for sharing the solution altChunk - I've learned something new!
Avatar of xav056

ASKER

this seems to be the solution I am looking for, found it myself