在 VBA 中的 Access 中将嵌入的 RTF OLE 转换为 HTML



我有一个表,其中包含包含富文本格式数据的嵌入式 OLE 字段。我需要将此数据传输到 MySQL 数据库并将其转换为 HTML。我使用访问。有没有办法在 VBA 中快速完成? 我搜索了网络,大多数人使用富文本控件(richtx32.ocx(来获取纯文本,但我需要它保持格式,我也没有这个控件。

以下是我解决问题的方法:

Option Explicit
Public wrd As Word.Application
Public doc As Word.Document
Function RTF2HTMLviaWord(rtf As String) As String
'Open Tools --> References --> and check Microsoft Scripting Runtime
Dim fso As New FileSystemObject
Dim text As TextStream
Dim temp As String
temp = Environ("TEMP")
If Len(rtf) > 1 Then
Set text = fso.CreateTextFile(temp & "RTF2HTML.rtf", True)
text.Write rtf
text.Close
If wrd Is Nothing Then
Set wrd = New Word.Application
End If
Set doc = wrd.Documents.Open(temp & "RTF2HTML.rtf", False)
doc.SaveAs temp & "RTF2HTML.htm", wdFormatHTML
doc.Close
fso.DeleteFile temp & "RTF2HTML.rtf"
Set text = fso.OpenTextFile(temp & "RTF2HTML.htm", ForReading, False)
RTF2HTMLviaWord = text.ReadAll
text.Close
fso.DeleteFile temp & "RTF2HTML.htm"
Else
RTF2HTMLviaWord = ""
End If
End Function

唯一的缺点是Word会产生太多垃圾HTML标签。我希望它可以保存最少的 HTML 标签而不格式化。

最新更新