RTF to plain text conversion problem (1 Viewer)

carlton

New member
Local time
Today, 00:00
Joined
Oct 30, 2018
Messages
14
According to all I've seen across the web, to convert .RTF data to plain text data I should use the plaintext function.
On reading in a .RTF file the Rich Text formatted text displays a perfect rendition. Attempting the plaintext conversion results in a display of jibberish.
I'm using Access 2016 on Windows 10

Code:
RTFstring=ts.readall 'read the RTF string
tbDisplayRTFstring.format= acTextFormatHTMLRichText 'set text box formatting to RichText
tbDisplayRTFstring.format.value=RTFstring ' displays RTF formatted data 'Display the Rich text data
tbDisplayTXTstring.format = acTextFormatPlain 'set the plain text format
tbDisplayTXTstring.value = plaintext(RTFstring) 'displays rubbish should be plain text.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 16:00
Joined
Oct 29, 2018
Messages
21,474
Are you talking about converting an RTF file, not an RTF Formatted field in a table?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 18:00
Joined
Feb 28, 2001
Messages
27,189
IF we are talking about whole-file conversion, in theory you COULD do this from Word as an app object. You would open Word, then open the document that is RTF, then without making any other changes, do a SaveAs from Word with a different format specification. You can't do it via automation in Wordpad because that utility does not, as far as I know, expose its command structure via a COM-style .DLL file. I don't know offhand of any other utility that does that, because my Navy experience was Office-centric.

If you are talking about something else, please specify in words, not code, what you want to do.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 00:00
Joined
Feb 19, 2013
Messages
16,616
don't really understand your code

tbDisplayRTFstring.format= acTextFormatHTMLRichText refers to the format property of the control -not much use for text except to display an alternative if value is null or to set a colour - and is ignored for rich text anyway. Think you mean to use tbDisplayRTFstring.textformat

tbDisplayRTFstring.format.value=RTFstring - makes no sense, think you mean to use tbDisplayRTFstring or tbDisplayRTFstring.value.
tbDisplayTXTstring.format = acTextFormatPlain - see above, think you want the textformat property
tbDisplayTXTstring.value = plaintext(RTFstring) - does this now work?

edit- both settings of the textformat property, should be done in design view, no need to use code
 

carlton

New member
Local time
Today, 00:00
Joined
Oct 30, 2018
Messages
14
I opened a .RTF file, stored the contents into a Long Text field of my table.
Using a text box with the format set to RichText I can display the fornatted data in it's rich text format.
I need to convert the RichText formatted data to plain text for further processing, hence the use the the PlainText function. I was displaying the plain text to a textbox as a check the conversion worked correctly because the code after str=plaintext(RTFdata) was not working.
 

carlton

New member
Local time
Today, 00:00
Joined
Oct 30, 2018
Messages
14
Are you talking about converting an RTF file, not an RTF Formatted field in a table?
I need to read a .RTF file, then convert to plain text for further processing. The .RTF and plan text versions are both stored in Long Text fields.
 

ebs17

Well-known member
Local time
Today, 01:00
Joined
Feb 7, 2020
Messages
1,946
Richtext in Access is implemented via HTML. This is very different from the RTF format in RTF files, which have their own formatting control characters. Therefore, the access functions will not be able to help you.
RichText to Text conversion -- fast and free
 

Cotswold

Active member
Local time
Today, 00:00
Joined
Dec 31, 2020
Messages
528
Just thought I'd mention that an RTF file cannot contain a virus.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 00:00
Joined
Sep 12, 2006
Messages
15,658
@carlton

the syntax you are using maybe vb, not vba
Having said that, it probably wouldn't be too hard to strip out all of the html formatting from a text string, as long as there's no <> characters in the string otherwise.
 

Dimi37

New member
Local time
Today, 01:00
Joined
Feb 8, 2024
Messages
2
Hi guys,

I know I'm late :) but perhaps my experience can be useful to someone.
Reading this post, I also struggled with trying to convert RTF text to Plain Text.
Finally, I found a somewhat twisted solution, but its performance is very satisfactory.

I noticed that the Access PlainText() function doesn't deliver the benefits it claims to, except when used under the following circumstances.

This method uses a hidden form, created upon calling the function for converting RTF to plain text.

  1. Create a form that we'll call "zfrmRichTextToPlainText".
  2. Add 2 TextBox controls:
    First control: Name = txtRich, Text Format property = Rich Text
    Second control: Name = txtPlain, Text Format property = Plain Text
  3. Code the function ConvertRTFTextToPlainText as follows:

  4. Code:
    Function ConvertRTFTextToPlainText(ByVal pstrRTFText As String) As String
    'Open the form in hidden mode
    Dim frm As New Form_zfrmRichTextToPlainText
    
    
    'Assign RTF content to the Rich text control named "txtRich"
    frm.txtRich.Value = pstrRTFText
    
    
    'Now convert to plain text
    frm.txtPlain.Value = PlainText(frm.txtRich.Text)
    
    'Return the plain text
    ConvertRTFTextToPlainText = frm.txtPlain.Value
    
    'Close the form
    DoCmd.Close acForm, frm.Name, acSaveNo
    
    End function

  5. To use it:

    Code:
    strPlain = ConvertRTFTextToPlainText(strRTF)


Good luck !
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 16:00
Joined
Oct 29, 2018
Messages
21,474
Hi guys,

I know I'm late :) but perhaps my experience can be useful to someone.
Reading this post, I also struggled with trying to convert RTF text to Plain Text.
Finally, I found a somewhat twisted solution, but its performance is very satisfactory.

I noticed that the Access PlainText() function doesn't deliver the benefits it claims to, except when used under the following circumstances.

This method uses a hidden form, created upon calling the function for converting RTF to plain text.

  1. Create a form that we'll call "zfrmRichTextToPlainText".
  2. Add 2 TextBox controls:
    First control: Name = txtRich, Text Format property = Rich Text
    Second control: Name = txtPlain, Text Format property = Plain Text
  3. Code the function ConvertRTFTextToPlainText as follows:

  4. Code:
    Function ConvertRTFTextToPlainText(ByVal pstrRTFText As String) As String
    'Open the form in hidden mode
    Dim frm As New Form_zfrmRichTextToPlainText
    
    
    'Assign RTF content to the Rich text control named "txtRich"
    frm.txtRich.Value = pstrRTFText
    
    
    'Now convert to plain text
    frm.txtPlain.Value = PlainText(frm.txtRich.Text)
    
    'Return the plain text
    ConvertRTFTextToPlainText = frm.txtPlain.Value
    
    'Close the form
    DoCmd.Close acForm, frm.Name, acSaveNo
    
    End function

  5. To use it:

    Code:
    strPlain = ConvertRTFTextToPlainText(strRTF)


Good luck !
Hi. Welcome to AWF!

Thanks for sharing.
 

Users who are viewing this thread

Top Bottom