XL97: Printing Formatted Text from a RichTextBox Control

ID: Q170232

The information in this article applies to:

SUMMARY

The SelPrint method of the RichTextBox control allows printing formatted text in Visual Basic. The SelPrint method is not supported in Visual Basic for Applications. This article contains information about how to print formatted text from a RichTextBox control in Microsoft Excel 97.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft Support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/

The RichTextBox control is available with the Microsoft Office 97 Developer Edition (ODE) Tools. The tools included in the Office 97 Developer Edition are tools that you can use in Microsoft Visual Basic 5.0; however, not all of the properties that work in the Visual Basic environment work in the Visual Basic for Applications environment.

You can print formatted text from a RichTextBox control, but because of limitations in Microsoft Excel, you must use Automation to print with Microsoft Word. In Microsoft Excel, you can create a Rich Text Format (RTF) file from the text in a RichTextBox control but you cannot print an RTF format file.

Example

To print formatted text from a RichTextBox control in Microsoft Excel 97, follow these steps:

1. In Microsoft Excel 97, press ALT+F11 to start the Visual Basic Editor.

2. On the Insert menu, click UserForm.

3. Draw a Command button and a RichTextBox control on UserForm1.

   NOTE: If the RichTextBox control is not available on the Toolbox
   commandbar, click Additional Controls on the Tools menu. Under Available
   Controls, click Microsoft RichText Control, version 5.0. If it is not
   listed, you must install the ActiveX controls from either Microsoft
   Visual Basic 5.0 or from the Microsoft Office 97 Developer Edition.

4. Double-click the command button to activate the module code for the
   button.

5. Type the following code into the module sheet:

   Private Sub CommandButton1_Click()
          Dim RTFString as Object

         ' Creates a file to receive the RTF information from the control.
         Open "RTFText.rtf" for Output As 1

         ' Places the RTF information from the control into the file and
         ' closes the file.
         Print #1, RichTextBox1.TextRTF
         Close 1

         ' Opens the new file in Microsoft Word and prints it.
         Set RTFString = GetObject("RTFText.rtf")
         RTFString.Activate
         RTFString.PrintOut Background:= False
         Set RTFString = Nothing
         Unload Me
   End Sub

6. Run the macro by clicking Run Sub/UserForm on the command bar. Type text
   in the RichTextBox control and click the command button.

   You cannot format text in the RichText box control but you can paste
   formatted text into the control. Note that the text you place in the
   control is printed.

For additional information, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q146022
   TITLE     : How to Set Up the RichTextBox Control for WYSIWYG Printing

Additional query words:
Keywords          : kbprg kbdta kbdtacode KbVBA 
Version           : WINDOWS:97
Platform          : WINDOWS
Issue type        : kbinfo

Last Reviewed: May 17, 1999