Data encoded as RTF - need to strip (1 Viewer)

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:31
Joined
May 7, 2009
Messages
19,175
i think this one is the simplest, but need
more test RTF.
Code:
Public Function RTF2PlainText(pText As String) As String
'* arnelgp
'*
    Dim i As Integer
    
    With CreateObject("VBScript.RegExp")
    
        .Global = True
        .IgnoreCase = True
        
        For i = 1 To 2 Step 1
            .pattern = Choose(i, "{[\w\W]+}", "\\[a-z0-9]+")
            pText = .Replace(pText, "")
        Next
    
    End With
    RTF2PlainText = Trim(pText)
End Function

for a test:
Code:
Private Sub test2()

    Dim strRTF As String
'    strRTF = "{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl {\f0\fnil\fcharset0 Courier New;}}" & _
'"{\colortbl;\red8\green0\blue0;}" & _
'"{\*\generator Msftedit 5.41.15.1515;}\viewkind4\uc1\pard\cf1\highlight0\f0\fs20 Two bedrooms w/ closets, great room, steps up & loft\par"
    
    strRTF = _
"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fprq1\fcharset0 Courier New;}}" & _
"{\colortbl;\red0\green0\blue128;}" & _
"{\*\generator Riched20 10.0.17134}\viewkind4\uc1" & _
"\pard\cf1\highlight0\f0\fs20 please use $8.64 yard pricing \fs18\par"

    Dim strResult As String
    strResult = RTF2PlainText(strRTF)
    Debug.Print
    Debug.Print strResult
End Sub
 

isladogs

MVP / VIP
Local time
Today, 15:31
Joined
Jan 14, 2017
Messages
18,186
arnel
Well post 21 is certainly simpler than post 20.
However, just out of interest, can you tell me why you think your solutions in posts 20/21 are simpler than mine in post 17 or 3 ... or that of MajP in post4.

Admittedly I was only dealing with one example in each case because that's all I thought the OP wanted. However it wouldn't take much effort to 'merge' them to allow for cases where fs18 string is present and where its not
 
Last edited:

Users who are viewing this thread

Top Bottom