In VSTO project, we can use WinForm dialog with RichTextBox to allow user to input rich formatted text. The Rtf property of RichTextBox returns the string representing the content in Rtf format.
However, there is no direct way to insert the Rtf content into Word Range object. The Text property of Range object only accept plain text and there is no Rtf property in Range object.
After the long search and experiment, I found out that the only way to communicate between RichTextBox and Range object is via Clipboard.
From RichTextBox to Range:
However, there is no direct way to insert the Rtf content into Word Range object. The Text property of Range object only accept plain text and there is no Rtf property in Range object.
After the long search and experiment, I found out that the only way to communicate between RichTextBox and Range object is via Clipboard.
From RichTextBox to Range:
From Range to RichTextBox:Private Sub InsertRtf(ByRef strRtfMessage As String, ByRef oRange As Word.Range)
With oRange.FormattedText
.Style = "Body Single"
Clipboard.Clear()
Clipboard.SetData(DataFormats.Rtf, strRtfMessage)
.Paste()
End With
End Sub
Private Function TransformRangeToRtf(ByRef oRange As Word.Range, ByRef rtfMsg As RichTextBox) As String
oRange.Copy()
rtbMsg.Paste()
Return rtbMsg.Rtf
End Function
Comments