BUG: Printer Error When Printing from RTF Control

Last reviewed: February 12, 1996
Article ID: Q142290
The information in this article applies to:
  • Standard, Professional and Enterprise Editions of Microsoft Visual Basic, 32-bit only, for Windows, version 4.0

SYMPTOMS

After printing from a Rich Text Box control, Visual Basic 4.0 raises error 482 - "Printer Error". This error can occur when exiting a Visual Basic application or when executing the EndDoc method of the Printer object.

WORKAROUND

After printing from a RTF control, executing an EndDoc will exhibit this problem. Visual Basic executes an automatic Printer.EndDoc if a program stops execution with buffered data. Even though this error is raised, the pages do print correctly. The workaround for this problem is to trap error 482 and simply ignore it. The example below contains code to demonstrate this technique.

STATUS

Microsoft has confirmed this to be an issue in the Microsoft products listed at the beginning of this article. We are researching this problem and will post more information here in the Microsoft Knowledge Base when it becomes available.

MORE INFORMATION

Steps to Reproduce

  1. Start Visual Basic, or, if it is already running, choose New Project from the File Menu.

  2. Add an RTF control and a command button to the form.

  3. Insert the following code in the Command1_Click event.

       Private Sub Command1_Click()
           Printer.Print ""
           RichTextBox1.SelPrint Printer.hDC
           Printer.EndDoc
       End Sub
    
    

  4. Run the application.

  5. Enter some text in the RTF text box, and click on the command button to print. The program will stop and an error dialog box, complaining of the above mentioned error, will be displayed. Clicking on the "Debug" button in the error dialog box will show that the line of code that execution stopped on was the call to EndDoc.

This problem can be avoided by changing the code in the Command1_Click procedure to the following:

   Private Sub Command1_Click()
       Printer.Print ""
       RichTextBox1.SelPrint Printer.hDC

       On Error GoTo EndDocError
       Printer.EndDoc
       On Error GoTo 0

       Exit Sub

   EndDocError:
       If Err.Number = 482 Then
           'do nothing
       Else
           Err.Raise Err.Number
       End If
   End Sub

This code initializes the printer object and prints from the RTF control. Error trapping is turned on, and if the error that occurs with the EndDoc method is number 482, it is ignored. If some other error is raised as a result of the EndDoc method, that error is passed upwards in the call stack to be handled by another procedure or Visual Basic as normal.

Additional Note

The RTF sample application included with the Visual Basic 4.0 MSDN Starter Kit was written using a different version of the RTF control than the one that shipped with Visual Basic 4.0. As a result, the above fix is not implemented in the code for that sample application. The RTF sample can be fixed by the same method outlined above; simply trap error 482 and ignore it.


Additional reference words: 4.00 vb4win vb432
KBCategory: kbprg kbbuglist
KBSubcategory: PrgCtrlsCus


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 12, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.