Solved

how to insert html into a wordDoc Openxml

Posted on 2011-02-22
5
1,115 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:xav056
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 12

Expert Comment

by:Hairbrush
ID: 34961381
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.
0
 
LVL 9

Author Comment

by:xav056
ID: 34963479
this uss vba or com,
I am looking for ways to do this using the openxml sdk, without a word com object
Thank you
0
 
LVL 9

Accepted Solution

by:
xav056 earned 0 total points
ID: 34971708
using ltchunck seems to be the solution
0
 
LVL 12

Expert Comment

by:Hairbrush
ID: 34977777
Thanks for sharing the solution altChunk - I've learned something new!
0
 
LVL 9

Author Closing Comment

by:xav056
ID: 35005244
this seems to be the solution I am looking for, found it myself
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question