Lesson 39: Printing in VB6

Master the Printer object to create professional printed output from your VB6 applications

Key Takeaway

The Printer object in VB6 provides powerful capabilities to send output directly to printers, allowing you to create professional documents, reports, and printed forms from your applications.

Welcome to Lesson 39 of our Visual Basic 6 Tutorial! In this lesson, you'll learn how to use the Printer object to send output directly to printers, format your printed documents, and create professional printed materials from your VB6 applications.

39.1 Understanding the Printer Object

VB6 provides a built-in Printer object that allows your applications to send output directly to printers. This object has several key methods and properties:

Print Method

Sends text to the printer, similar to how Print works for forms

NewPage

Starts a new page in the current print job

EndDoc

Sends the entire document to the printer

VB6 Code

Create output using Printer object

Print Spool

Document is queued for printing

Printer

Physical document is printed

Basic Printing Example

The simplest way to print from VB6 is to use the Printer object with the EndDoc method:

Form_Load.vb
Private Sub Form_Load()
    Printer.Print "Welcome to Visual Basic"
    Printer.EndDoc
End Sub
                        

Important

Always include Printer.EndDoc to actually send your document to the printer. Without it, your output will only be sent to the print spooler when the application terminates.

Printing Variables and Calculations

You can print variables and calculated values just like you would display them on a form:

Command1_Click.vb
Private Sub Command1_Click()
    Dim x, y As String, z As Variant
    x = InputBox("Enter the first Number")
    y = InputBox("Enter the second Number")
    z = Val(x) + Val(y)
    Printer.Print "The answer is " & z
    Printer.EndDoc
End Sub
                        

Printing Text File Contents

You can easily print the contents of a text file or text box:

CmdPrint_Click.vb
Private Sub CmdPrint_Click()
    Printer.Print Text1.Text
    Printer.EndDoc
End Sub
                        

39.2 Formatting Printed Output

The Printer object provides several properties to format your printed output:

Formatting Example

This example demonstrates how to format printed text:

CmdPrintFormatted_Click.vb
Private Sub CmdPrintFormatted_Click()
    ' Set font properties
    Printer.FontName = "Verdana"
    Printer.FontSize = 16
    Printer.FontBold = True
    Printer.FontItalic = True
    Printer.FontUnderline = True
    
    ' Print centered title
    Printer.CurrentX = (Printer.ScaleWidth - Printer.TextWidth("Report Title")) / 2
    Printer.Print "Report Title"
    
    ' Reset formatting for body
    Printer.FontBold = False
    Printer.FontItalic = False
    Printer.FontUnderline = False
    Printer.FontSize = 10
    
    ' Print content
    Printer.Print "This is a formatted report generated from VB6"
    Printer.Print "Printed on: " & Format(Date, "Long Date")
    
    ' Add page number
    Printer.CurrentY = Printer.ScaleHeight - 500
    Printer.Print "Page " & Printer.Page
    
    Printer.EndDoc
End Sub
                        

Preview of Formatted Output

Printing Multiplication Table

This example uses nested loops to print a multiplication table:

PrintMultiplicationTable.vb
Private Sub Command1_Click()
    Dim i, j As Integer
    For i = 2 To 9
        For j = 2 To 9
            Printer.Print i & "x" & j & " =" & i * j
        Next j
        Printer.Print ' Print blank line
    Next i
    Printer.EndDoc
End Sub
                        

Lesson Summary

In this lesson, you've learned how to use the Printer object to create printed output from your VB6 applications:

Printer Object Basics

Using the Print method and EndDoc to send output to printers

Formatting Techniques

Controlling font properties, positioning, and page management

Practical Examples

Printing variables, calculations, text files, and formatted reports

Advanced Techniques

Using loops to generate complex printed output like tables

Pro Tip

For more complex printing needs, consider using the Printer object in combination with the PrintForm method to capture the current form state, or third-party reporting tools for advanced report generation.

Next Lesson

Continue your VB6 journey with Lesson 40: Report Creation where you'll learn to create professional database reports.

Related Resources

VB6 Printer Object Reference

Complete documentation for the Printer object

View Resource

Advanced Printing Techniques

Creating complex reports with VB6

View Guide

Printing Best Practices

Optimizing print performance in VB6

View Article

Database Reporting

Connecting printing to database systems

Learn More