Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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
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
  • 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:

Technology Partners: 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

Independent Software Vendors: 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!

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

722 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