Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

RTF preview and RTF save of generated document.

Posted on 2000-04-10
Last Modified: 2012-08-13
I have defined simple language to allow users to create simple letters and then print them out.
A sample program is shown below.
PRINT "Dear Mr. "
PRINT "You owe us $"

During Interpretation i simplly use appropriate printer/word/SQL SELECT commands to format the text ....
This produces
Dear Mr. John
You owe us $100
This program is stored in SQL SERVER and whenever a letter is to be printed the program is retrieved and interpreted.
I am providing options to print this output to Printer or Word/RTF File.
I guess I can use the printer and word controls to do this.
What I want is a way to preview the generated document before opening it in word or sending it to printer.
I want to be able to preview the document as it is generated in a rich text box. How do I do this ?
Also I want to save the output of the interpreted program as an RTF file.... How do I do this ?

Note that only simple formatting commands (supported by RTF is implemented)
Question by:tellis070898
  • 4
  • 3
  • 2
  • +1

Author Comment

ID: 2699777
I feel it all boils down to how to dynamically add pieces of differently formatted text to a Rich Text Box during runtime.
Ex. I want to add "Dear John" in bold followed by "You Owe Us" in italics to the rich text box.Is this possible ?
If not can i write these pieces of text to the rich text box and then programatically select the desired portions and apply formatting to them ?
If the above is not possible then, Is it possible to genetate a temp RTF file with the above text and then load this into the rich textbox ?
If none of this is possible how do I go about it ?
LVL 14

Expert Comment

ID: 2699853
First check out the following microsoft KB  articles:

HOWTO: Set Up the RichTextBox Control for WYSIWYG Printing

HOWTO: Use a PictureBox to Control Orientation Printing a Form

As for formatting text in a richtextbox, set the "SelStart" property to be the position where you want to start formatting and the "SelLength" property to be the length of the text you want to format.

Then you can use:

   RichTextBox1.SelBold = True

   RichTextBox1.SelItalic = True

Or any of the other formatting properties.

You can also use the RichTextBox.Find method to highlight text in the RichTextBox and then use the formatting properties on the highlighted text.

Hope this help!

LVL 28

Expert Comment

ID: 2700783
richtextbox has a save command built in:
richtextbox.SaveFile(pathname, filetype)
where filetype can be either rtfRTF or rtfText.  It saves as rtfRTF by default.  If you are talking about print preview check out this project example

also more general info about the richtextbox:

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Expert Comment

ID: 2701021

I have worked on a Project where I did exactly this.

We saved the Text in a letters table in SQL Server as RTFText with <<Amount Owed>> in the text. These are prompts or fillers - like old fashioned report generators.

For Instance you put in //LastName// to insert a field and <<Amount Owed>> for a user prompt.

Get the Text from your database and set MyRTFbox.textrtf= rs("Text") - as an example.

Then scan the text and fill the fields //LastName// etc.

Now when the user clicks 'Print' scan for prompts and ask the user to fill in <<Amount Owed>>.

You can interpret the <<Amount Owed>> as compulsary and <<<Amount Owe>>> as non compulsary as an example. (Extra braces).

I then made a letter generator where a user could create letters and embed them in the database by setting rs("Text")=myRTF.RTFText so giving access to 300 users as soon as a new letter was available.

Good Luck


Author Comment

ID: 2703281
Adjusted points from 100 to 150

Author Comment

ID: 2703282
Hi the responses were great ...
Mcrider has given me just the solution I need and the links provided by AzraSound were helpful.
I have just a few questions to help me wind up this question ...
# How do I append text into the rich text box ?
# Programaically how do I insert an Image into the rich text box.
# How do I programatically insert newlines and NEWPAGE into the rich text box.

Ex: I have 3 string variables A B and C and an image x.bmp
I want to write A into the rich text box.
Then i want to add B to the box in a new line.
Then I want to add string C in a new logical page (such that the rtf document will have a new page)
Finally I want to insert the image into the text box ...
How do i go about this ?
LVL 14

Accepted Solution

mcrider earned 150 total points
ID: 2703796
# How do I append text into the rich text box ?

If you do this, you will append text to a richtextbox:

   Dim MyBuffer As String
   MyBuffer = "Here's some more text"
   RichTextBox1.Text = RichTextBox1.Text & MyBuffer

# Programaically how do I insert an Image into the rich text box.

You have to put the image onto the clipboard and then paste it into the richtextbox... Like this:

    Clipboard.SetData Picture1.Image, vbCFBitmap
    SendKeys "^v", True

# How do I programatically insert newlines and NEWPAGE into the rich text box.

You can insert new lines by using the "VbCrLf" variable... For example, this will insert a new line at the beginning of the richtextbox:

   RichTextBox1.SelStart = 0
   RichTextBox1.SelLength = 0
   RichTextBox1.SelText = vbCrLf

Unfortunately, the RichTextBox control does NOT support pagebreaks, so if you are printing using the control, you're out of luck...  However, if you are going to be printing from WORD, once you have saved the richtext file using:

   richtextbox1.SaveFile "c:\windows\desktop\testdoc.rtf"

You can open the file as a simple text file using the Open statement and insert the Rich Text Tag "\page " wherever you want a page break (dont use the quotes...)

For example, here is a richtext file that has a page break:

{\rtf1\ansi\deff0\deftab720{\fonttbl{\f0\fswiss MS Sans Serif;}{\f1\froman\fcharset2 Symbol;}{\f2\froman\fprq2 Times New Roman;}}
\deflang1033\pard\plain\f2\fs20 This is a test
\par testing page 1
\par \page this is only a test page 2
\par }

This should deserve an "A"... ;-)


LVL 28

Expert Comment

ID: 2704289
if you wanted to pass a new line before appending the new string:

Richtextbox1 = Richtextbox1 & vbCrLf & "My String to Add"

Obviously you can use as many vbCrLf's as you need to put in as many lines as you desire

Author Comment

ID: 2706763
Hi, Mcriders excellent comments answers solve all my problems so an 'A' it will be :-)
AzraSound and Voodooman thanks for the helpful comments and links...
It was a great discussion - Thanks Guys

LVL 14

Expert Comment

ID: 2710446
Thanks for the points! Glad I could help!


Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

808 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