Lesson 39: Using the Printer-Part 1

 

39.1 Printing using the Printer object

In previous lessons, we have only written programs that send output to the screen and not the printer. In this lesson, we will learn how to send an output to the printer and get it printed. Sending output to the printer is a simple task in Visual Basic, it involves the use of the Printer object and the print method. The standard code of sending an output to the printer and get it printed out is as follows:

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

However, the code above only send the output to the printer without actually printing it. It will only print the output when you terminate the application. It can a very inconvenience  if you need to close the program every time you want to print the output. To solve this little problem, we need to add the newpage or EndDoc method. So, add one extra line to the above code as follows:

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

                    Printer.EndDoc

                End Sub

 

Beside printing messages in string form, you can actually print out other varibales including numeric values. Below is an example:

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

If x=3 and y=4, the printing output is "The answer is 7'

You can also use loops to send output to the printer. In the follow example, I used the For......Next  loop to print out the multiplication table.

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 Chr(13)

Next i
Printer.EndDoc

End Sub

The command Printer.Print Chr(13) is equivalent to pressing the Enter and print the output on the next line. The output is as follows:

  

2x2=42x3=62x4=82x5=102x6=122x7=142x8=162x9=18
3x2=63x3=93x4=123x5=153x6=183x7=213x8=243x9=27
4x2=84x3=124x4=164x5=204x6=244x7=284x8=324x9=36
5x2=105x3=155x4=205x5=255x6=305x7=355x8=405x9=45
6x2=126x3=186x4=246x5=306x6=366x7=426x8=486x9=54
7x2=147x3=217x4=287x5=357x6=427x7=497x8=567x9=63
8x2=168x3=248x4=328x5=408x6=488x7=568x8=648x9=72
9x2=189x3=279x4=369x5=459x6=549x7=639x8=729x9=81

Now you might want to know whether it is possible to print the content of  text file created in Visual Basic. The answer is a big "YES". Let me use Example 17.3.2 of Lesson 17.  We shall add a command button to the form and rename it as cmdPrint and change the label to Print, and then double click the button to insert the follow code:

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

By clicking the Print button you should be able to print the content of the text box.

The full code of the program as follows:

Dim linetext As String

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

End Sub

Private Sub open_Click()
CommonDialog1.Filter = "Text files{*.txt)|*.txt"
CommonDialog1.ShowOpen
If CommonDialog1.FileName <> "" Then
Open CommonDialog1.FileName For Input As #1
Do
Input #1, linetext
Text1.Text = Text1.Text & linetext
Loop Until EOF(1)
End If

Close #1


End Sub

Private Sub save_Click()
CommonDialog1.Filter = "Text files{*.txt)|*.txt"
CommonDialog1.ShowSave
If CommonDialog1.FileName <> "" Then
Open CommonDialog1.FileName For Output As #1
Print #1, Text1.Text
Close #1

End If

End Sub

 

39.2 Formatting the Output using Printer Object properties
You can format your output before sending it to the printer using a number of font related Printer object properties. Some of these properties are listed below:

FontBold, FontItalic, FontSize, FontName and FontUnderline

The code to format your printed output is illustrated in the example below:

Private Sub CmdPrint_Click()

Printer.FontName="Verdana"

Printer.FontSize=16

Printer.FontBold=True

Pinter.FontItalic=True

Printer.FontUndeline=True

          Printer.Print Text1.Text
        Printer.EndDoc

        End Sub

 


 

 

  Bookmark and Share

<Previous Lesson> <<Home>> <Next Lesson>