[VB]RichTextBox內文(字型/格式)轉Html

透過Office.Interop以及剪貼簿來達成RichTextBox的值轉為HTML格式

https://www.codeproject.com/articles/51879/converting-rtf-to-html-in-vb-net-the-easy-way

原文為  於2010/1/14撰寫

此方法不是直接對RichTextBox進行轉換,而是將其內容利用剪貼簿Hold住,貼上Word後

再利用剪貼簿將Word上的值整個擷取起來,再使用System.Windows.Forms.DataFormats.Html 整個Format掉

 

Public Class Form1
    Dim sConvertedString As String = ""

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        sRTF_To_HTML(RichTextBox1.Rtf)
        TextBox1.Text = sConvertedString
    End Sub

    Public Function sRTF_To_HTML(ByVal sRTF As String) As String
        'Declare a Word Application Object and a Word WdSaveOptions object


        Dim MyWord As Microsoft.Office.Interop.Word.Application
        Dim oDoNotSaveChanges As Object =
             Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges
        'Declare two strings to handle the data
        Dim sReturnString As String = ""

        Try
            'Instantiate the Word application,
            'Set visible To False And create a document

            MyWord = CreateObject("Word.application")
            MyWord.Visible = False
            MyWord.Documents.Add()
            'Create a DataObject to hold the Rich Text
            'and copy it to the clipboard
            
            Dim doRTF As New System.Windows.Forms.DataObject
            doRTF.SetData("Rich Text Format", sRTF)
            Clipboard.SetDataObject(doRTF)
            'Paste the contents of the clipboard to the empty,
            'hidden Word Document
            MyWord.Windows(1).Selection.Paste()
            '…then, select the entire contents of the document
            'and copy back to the clipboard
            MyWord.Windows(1).Selection.WholeStory()
            MyWord.Windows(1).Selection.Copy()
            'Now retrieve the HTML property of the DataObject
            'stored on the clipboard
            sConvertedString =
                 Clipboard.GetData(System.Windows.Forms.DataFormats.Html)
            'Remove some leading text that shows up in some instances
            '(like when you insert it into an email in Outlook
            '把轉換好的Html擷取自己想要的片段
            sConvertedString =
                sConvertedString.Substring(sConvertedString.IndexOf("t-->"))

            'Also remove multiple  characters that somehow end up in there
            sConvertedString = sConvertedString.Replace("Â", "")
            '…and you're done.
            sReturnString = sConvertedString.Remove(0, 4)
            If Not MyWord Is Nothing Then
                MyWord.Quit(oDoNotSaveChanges)
                MyWord = Nothing
            End If
        Catch ex As Exception
            If Not MyWord Is Nothing Then
                MyWord.Quit(oDoNotSaveChanges)
                MyWord = Nothing
            End If
            MsgBox("Error converting Rich Text to HTML")
        End Try
        Return sReturnString
    End Function
End Class

簡單拉幾個RichTextBox/Button/Textbox,輸出大概就像下圖…

找個線上預覽HTML的網頁,轉出來大概就這樣

 

單純筆記,皆為非正規作法,旁門左道,胡搞瞎搞。