• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 917
  • Last Modified:

RTF preview and RTF save of generated document.

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)
  • 4
  • 3
  • 2
  • +1
1 Solution
tellis070898Author Commented:
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 ?
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!

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:

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


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

tellis070898Author Commented:
Adjusted points from 100 to 150
tellis070898Author Commented:
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 ?
# 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"... ;-)


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
tellis070898Author Commented:
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

Thanks for the points! Glad I could help!


Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now