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
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.Print "Welcome to Visual Basic"
Printer.EndDoc
End Sub
Beside printing messages in string form, you can actually print out other variables 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
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.EndDocEnd 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=4 | 2x3=6 | 2x4=8 | 2x5=10 | 2x6=12 | 2x7=14 | 2x8=16 | 2x9=18 |
3x2=6 | 3x3=9 | 3x4=12 | 3x5=15 | 3x6=18 | 3x7=21 | 3x8=24 | 3x9=27 |
4x2=8 | 4x3=12 | 4x4=16 | 4x5=20 | 4x6=24 | 4x7=28 | 4x8=32 | 4x9=36 |
5x2=10 | 5x3=15 | 5x4=20 | 5x5=25 | 5x6=30 | 5x7=35 | 5x8=40 | 5x9=45 |
6x2=12 | 6x3=18 | 6x4=24 | 6x5=30 | 6x6=36 | 6x7=42 | 6x8=48 | 6x9=54 |
7x2=14 | 7x3=21 | 7x4=28 | 7x5=35 | 7x6=42 | 7x7=49 | 7x8=56 | 7x9=63 |
8x2=16 | 8x3=24 | 8x4=32 | 8x5=40 | 8x6=48 | 8x7=56 | 8x8=64 | 8x9=72 |
9x2=18 | 9x3=27 | 9x4=36 | 9x5=45 | 9x6=54 | 9x7=63 | 9x8=72 | 9x9=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
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=16Printer.FontBold=TruePinter.FontItalic=TruePrinter.FontUndeline=TruePrinter.Print Text1.Text
Printer.EndDoc
End Sub
http://www.vbtutor.net/vb6/vbtutor.html
No comments:
Post a Comment