XL97: Printing Formatted Text from a RichTextBox Control

Last reviewed: February 27, 1998
Article ID: Q170232
The information in this article applies to:
  • Microsoft Excel 97 for Windows

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

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

Microsoft provides examples of Visual Basic for Applications procedures 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. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

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 : kbcode kbprg xlvbainfo
Version : WINDOWS:97
Platform : WINDOWS
Issue type : kbinfo


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 27, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.