将HTML或RTF格式的文本插入Word书签

时间:2011-07-05 07:53:14

标签: vb.net ms-word bookmarks

我尝试将格式化文本插入Word书签。该文本来自几个富文本控件(我们使用TX文本控件)并附加到书签中。 问题是标签是按原样编写的,不会被解释。

oWord = New Word.Application
Dim strFileName As String = "\\...\Template.dot"
oDoc = oWord.Documents.Add(strFileName)
Dim strText As String = ""
Dim strOut As String = ""
txtPart1.Save(strOut, TXTextControl.StringStreamType.RichTextFormat)
strText += strOut
strText += ControlChars.CrLf & ControlChars.CrLf & ControlChars.CrLf
strText += txtPart2.Text
oDoc.Bookmarks.Item("Conditions").Range.Text = strText
oWord.Visible = True

我尝试使用RTF或HTML格式的字符串,但它的行为是相同的。

1 个答案:

答案 0 :(得分:2)

我完成了一个“不太好”的解决方案:

oWord = New Word.Application
Dim strFileName As String = "\\...\Template.dot"
oDoc = oWord.Documents.Add(strFileName)
Dim strText As String = ""
txtPart1.Save(strText, TXTextControl.StringStreamType.RichTextFormat)
Clipboard.SetData(DataFormats.Rtf, strText)
oDoc.Bookmarks.Item("Conditions").Range.PasteSpecial(DataType:=Word.WdPasteDataType.wdPasteRTF)

我讨厌使用剪贴板插入格式化文本的想法(以RTF格式,似乎不能使用HTML格式),所以我会在接受这个答案之前等待......