Problem to generate MS Word file from RichTextBox with format

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

We store the RichTextBox text with format in SQL DB. It looks like
"<p><b>123</b>ABC</p>". When I use this string to generate a Word doc file,
the doc only includes exactly the raw text with tags, no format.

What’s wrong with my Word automation code? Thanks ahead.
Shaw
----- Code Sample -----
Sub Main()
objWordApplication = CreateObject("Word.Application")
objWordApplication.Visible = False
Dim wRng As Word.Range
objDocument = objWordApplication.Documents.Add
wRng = objDocument.Paragraphs(objDocument.Paragraphs.Count).Range

Dim htmlDataObject As New DataObject
htmlDataObject.SetData(DataFormats.Rtf, True, "<p><b>123</b>ABC</p>")
Clipboard.SetDataObject(htmlDataObject)

wRng.Paste()
objDocument.SaveAs("D:\\Docs\MyTestDoc.doc")

objWordApplication.Quit()
End Sub
 
I spent time and figured out the problem.

(1) It should be an html data format.
(2) The partial html tagged string should be wrapped by other tags, such as
HtmlSatrt, HtmlEnd… to generate so called html fragment string. And then the
html fragment string is passed into DataObject which is going to Clipboard
later.

Thanks for reading my post.
Shaw
 
Back
Top