Go Premium for a chance to win a PS4. Enter to Win


RTF preview and RTF save of generated document.

Posted on 2000-04-10
Medium Priority
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:

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!


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 600 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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

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…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

971 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