Am 23.05.2017 um 10:04 schrieb Wolfgang Wolf:
> hat jemand so was auf der Platte rumliegen? Der Weg über eine
> unsichtbare RTF-Box ist mir zu langsam und selber parsen unsicher, zumal
> mich zuerst tagelang in die Syntax einarbeiten müsste. Wisst ihr ob
> Windows was dabei hat?
Ausgehend von Dieters Routine...
Hier nochmal eine Abwandlung davon, die ohne Hilfs-Form
auskommt (in einer normalen Klasse implementiert,
vielleicht hilfreich in Services u.a.).
Anstelle von VBs Controls.Add usw. gibt es alternativ
auch das ATL-API:
Option Explicit 'cRtfConverter
Private Declare Function AtlAxWinInit Lib "atl" () As Long
Private Declare Function AtlAxGetControl Lib "atl" (ByVal hWnd As Long,
Unk As stdole.IUnknown) As Long
Private Declare Function CreateWindowExW Lib "user32" (ByVal dwExStyle
As Long, ByVal lpClassName As Long, ByVal lpWindowName As Long, ByVal
dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long,
ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long,
ByVal hInstance As Long, ByVal lpParam As Long) As Long
Private Declare Function DestroyWindow Lib "user32" (ByVal hWnd As Long)
As Long
Private hWndDoc As Long, Doc As Object, mHTML As String, mText As String
Private Sub Class_Initialize()
AtlAxWinInit
hWndDoc = CreateWindowExW(0, StrPtr("AtlAxWin"), StrPtr("mshtml:"),
0, 0, 0, 0, 0, 0, 0, 0, 0)
If hWndDoc Then AtlAxGetControl hWndDoc, Doc
If Not Doc Is Nothing Then Doc.DesignMode = "on"
End Sub
Public Sub Convert(sRtf As String)
Clipboard.Clear
Clipboard.SetText sRtf, vbCFRTF
Doc.execCommand "Paste", False, True
mHTML = Doc.body.innerHTML
mText = Doc.body.innerText
Doc.body.innerHTML = vbNullString 'reset for the next round
End Sub
Public Property Get HTML() As String
HTML = mHTML
End Property
Public Property Get Text() As String
Text = mText
End Property
Private Sub Class_Terminate()
Set Doc = Nothing
If hWndDoc Then DestroyWindow hWndDoc
End Sub
Testcode in eine Form:
Option Explicit
Const Rtf$ = "{\rtf1\ansi\deff0
{\colortbl;\red0\green0\blue0;\red255\green0\blue0;}" & _
"This line is the default color\line \cf2 This Line Is red}"
Private Converter As New cRtfConverter
Private Sub Form_Click()
Converter.Convert Rtf
Debug.Print Converter.HTML; vbLf
Debug.Print Converter.Text; vbLf
End Sub
Olaf