Solved

RTF convert to plain text (BY FUNCTION!)

Posted on 2001-06-20
9
1,039 Views
Last Modified: 2007-11-27
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}
0
Comment
Question by:mdonley
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6212368
hearing...
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6212373
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
0
 
LVL 6

Expert Comment

by:JonFish85
ID: 6212685
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)
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6212735
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
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 3

Expert Comment

by:rmichels
ID: 6212983
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?
0
 
LVL 3

Expert Comment

by:rmichels
ID: 6212989
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
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 200 total points
ID: 6215008
Here is a quick and dirty way of splitting up the RTF blocks. This code just cycles through the available blocks of RTF text and displays them in an RTF box. (I am using the sample you provided to demo but I imagine you would be reading this from elsewhere into a string rather than assigning it explicitly.)

Dim rtf As String
Dim aryRTF() As String

Private Sub Command1_Click()
    Static intElement As Integer
    intElement = intElement + 1
    If intElement > UBound(aryRTF) Then
        intElement = 0
        MsgBox "No More RTF Sections To Display"
        Exit Sub
    End If
    RichTextBox1.TextRTF = "{\rtf1" & aryRTF(intElement)
End Sub

Private Sub Form_Load()
    rtf = "{\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}"
    aryRTF = Split(rtf, "{\rtf1")
End Sub
0
 

Author Comment

by:mdonley
ID: 6216532
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.
0
 

Author Comment

by:mdonley
ID: 6216565
Answer was just in time and worked like a charm.  Very well thought out and well presented.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now