Microsoft KB Archive/170232

= XL97: Printing Formatted Text from a RichTextBox Control =

Article ID: 170232

Article Last Modified on 11/23/2006

-

APPLIES TO


 * Microsoft Excel 97 Standard Edition

-



This article was previously published under Q170232





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 a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:

https://partner.microsoft.com/global/30000104

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

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:  In Microsoft Excel 97, press ALT+F11 to start the Visual Basic Editor. On the Insert menu, click UserForm. 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.

 Double-click the command button to activate the module code for the button.  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  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.</li></ol>

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

146022 How to Set Up the RichTextBox Control for WYSIWYG Printing

Keywords: kbdtacode kbinfo kbprogramming KB170232

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.