Link to home
Start Free TrialLog in
Avatar of mdonley
mdonley

asked on

RTF convert to plain text (BY FUNCTION!)

I need help in a hurry!  I have a field in a database that can contain a concatenation of one or many RTF fields.  When I want to read the straight text of one I put it in a standard RTF textbox and then read the text out of it.  I have found that the standard microsoft RTF textbox only reads in the first of the concatenated set and drops the rest of them.  I need a function of some sort to strip out the RTF formatting and present me with just the plain text.

Below is an example of one of the database field values.  each time a reviewer adds comments, there is a new rtf chunk appended on the bottom:

{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Times New Roman;}{\f1\froman Times New Roman;}}
{\colortbl ;\red255\green0\blue0;}
\viewkind4\uc1\f1 Based on observation, ...  Findings include:
\par
\par Review of policy indicated ...
\par
\par Staff were unable to ...
\par
\par Nursing staff were unable to ...
\par
\par Review of the clinical record ...
\par
\par Review of clinical records for ...
\par
\par Interview with ...
\par
\par  
\par \f0
\par }
{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Times New Roman;}{\f1\froman Times New Roman;}}
{\colortbl ;\red255\green0\blue0;}
\viewkind4\uc1\f1\fs28
\par Observation on 5/9/01 at 2:00 PM ...  
\par
\par Interview with nursing staff on 5/9/01 at 2:10 PM confirmed ...  
\par
\par The item on the 3rd floor was left unlocked at 4:45 PM on 5/8/01 when ...
\par
\par Nursing staff failed to ...
\par
\par
\par Submitted to the facility on 3/8/01 ...
\par
\par Review of facility policy ...
\par
\par Review of the activity ...
\par
\par Cross refer to F-429 and F-430
\par
\par Code 211.12(d)(1)(2)(5)
\par Code 211.10(c)\fs20
\par \cf1}
Avatar of Richie_Simonetti
Richie_Simonetti
Flag of Argentina image

hearing...
You could paste the contents of concatenated fileds to clipboard and, suposse your Rich text box is called RTB1:

Private Sub Form_Click()
' this converts richtext to plain text
rtb1.Text = Clipboard.GetText
End Sub
Avatar of JonFish85
JonFish85

you could try this

Public Function StripRTF(RT As String) As String
Dim txt As TextBox
Dim RTBox As RichTextBox

  RTBox.RTFText = RT
  txt.Text = RTBox.Text
  StripRTF = txt.Text
End Function


that might work. I can test it at this moment, so you might need to change a thing or two (such as RTBox.RTFText might be RTBox.TextRTF)
Hi, Agree with JonFish85,

Show your RTF format file in a Rich Text Box (r1.LoadFile FileName, see more in MSDN) , then save it in TXT Format:

r1.SaveFile FileName, rtfText '1
______________________________________________________

Cuts from MSDN:

Saves the contents of a RichTextBox control to a file.

Syntax

object.SaveFile(pathname, filetype)

The SaveFile method syntax has these parts:

Part
object - Required. Anobject expression that evaluates to an object in the Applies To list.

pathname - Required. Astring expression defining the path and filename of the file to receive the contents of the control.

filetype - Optional. An integer or constant that specifies the type of file loaded, as described in Settings.


Settings

The settings for filetype are:

Constant
rtfRTF - 0 (Default) RTF. The RichTextBox control saves its contents as an .rtf file.

rtfText - 1 Text. The RichTextBox control saves its contents as a text file.


Remarks

You can also use the Write function in Visual Basic and the TextRTF and SelRTF properties of the RichTextBox control to write .rtf files. For example, you can save the highlighted contents of a RichTextBox control to an .rtf file as follows:

Open "mytext.rtf" For Output As 1

Print #1, RichTextBox1.Sel

'Hope will help
The problem (I think) is that you have multiple rtf logical blocks of data.  Each section that starts with {\rtf1\ansi\deff0{\fonttbl... is really defining an independent rtf block of data.

I saved your rtf example to a text file with an rtf extention.  I then opened up the file in word and only got the first set of text.

So, it's not an issue with the RTF control.

You could try parsing the strings and putting them back to gether, but if the rtf headers vary due to font names, sizes, etc..you will have to deal with putting the font definitions back together.  Not a pretty picture I think.

Why is the data broken up as you have it?
Actually another thought.

You can parse out each individual block (using the \rtf.. as a starting point), and load each one in a single rtf field.  Then extract the text for it, save it/concat it with previously saved straigth text.

A process like:

1) Get concatenated RTF text

while text to part
   get next block
   load into rtf control
   textonly = textonly & rtf.text
wend
ASKER CERTIFIED SOLUTION
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mdonley

ASKER

I think you guys are on the right track with the idea of splitting the blob into the smaller RTF chunks.  I am going to try and test it now.  I am doing all of this simply to display all the different comments from different investigators on one Crystal Report.  Crystal 8.5 does have support for RTF format in fact it can load the raw RTF and interpret it nicely, but since it is a report by nature, I want all of the text to be unifor and crystal just renders it as however it is stored and therefore it is not satisfactory.  If the fields were smaller, I would be able to execute a formula on the field in Crystal, but since the field is defined as a "TEXT" field, Crystal will not allow me to do that.

Anyway, I will try the above suggestion and then award points.  I might have to split the points between rmichels and TimCottee since you both were on the same basic track.  I will worry about that later, right now I just want to see if it will work.
Avatar of mdonley

ASKER

Answer was just in time and worked like a charm.  Very well thought out and well presented.