Article: Q248882
Product(s): Microsoft Visual Basic for Windows
Version(s): WINDOWS:4.0,5.0,6.0
Operating System(s):
Keyword(s): kbprint kbCmnDlgPrint kbCtrl kbPrinting kbVBp kbVBp400 kbVBp500 kbVBp600 kbGrpDSVB kbDS
Last Modified: 11-SEP-2001
-------------------------------------------------------------------------------
The information in this article applies to:
- Microsoft Visual Basic Learning Edition for Windows, versions 5.0, 6.0
- Microsoft Visual Basic Professional Edition for Windows, versions 5.0, 6.0
- Microsoft Visual Basic Enterprise Edition for Windows, versions 5.0, 6.0
- Microsoft Visual Basic Standard Edition, 32-bit, for Windows, version 4.0
- Microsoft Visual Basic Professional Edition, 32-bit, for Windows, version 4.0
- Microsoft Visual Basic Enterprise Edition, 32-bit, for Windows, version 4.0
-------------------------------------------------------------------------------
SUMMARY
=======
The Print Dialog of the Common Dialog control has three print range options that
can be selected:
- A selection of text.
- A range of pages.
- All pages.
Depending upon the option selected, certain flags are set that can be checked
programmatically to determine which option has been chosen in the printer dialog
box. This article shows how to print a set of pages populated with records from
a database. The idea can be extended to other types of text. To do so it is
necessary to determine the margins and printable area of the page, as described
in the following Microsoft Knowledge Base article:
Q193943 HOWTO: Use GetDeviceCaps to Determine Margins on a Page
MORE INFORMATION
================
By default the Pages From ... To option is dimmed in the dialog box. To enable
this option you must set the Max property. This is so that the range entered by
the user can be validated.
In this article a set of three pages is created from the records of the Nwind.mdb
database that ships with Visual Basic. Each record is considered to be one line
on the page. Since the number of lines depends on the fontsize, the sample sets
a specific fontsize and determines how many lines will fit on the page. This is
necessary in order to inform the printer when to start a new page.
Step-By-Step Example:
1. Start a new Visual Basic Standard EXE project. Form1 is created by default.
2. From the Project menu, select Components. Select the Microsoft Common Dialog
Control and then click OK.
3. From the Project menu, select References. Select the Microsoft DAO 3.51
Object Library and then click OK.
4. Add a text box, a CommonDialog and two command button controls to Form1.
5. In the Properties dialog box for Text1 set the MultiLine property to True and
the ScrollBars property to 3-Both.
6. Add the following code to the General Declarations section of Form1:
Option Explicit
Private Sub Command1_Click()
Dim myDatabase As Database
Dim rsMyTable As Recordset
Dim i As Integer
Dim j As Integer
Dim startpage As Integer
' enable the page range selection option
CommonDialog1.Max = 3
' set the last page to be printed
CommonDialog1.FromPage = 1
CommonDialog1.ToPage = 3
' clear flags before showing printer dialog box
CommonDialog1.Flags = 0
' show the printer dialog box
CommonDialog1.ShowPrinter
' Enter the page number from which to start printing
startpage = CommonDialog1.FromPage
' set the font size. This will give 42 lines per page
Printer.FontSize = 18
' replace with your path to the nwind.mdb
Set myDatabase = OpenDatabase("nwind.mdb")
Set rsMyTable = myDatabase.OpenRecordset("Customers")
rsMyTable.MoveFirst
If (CommonDialog1.Flags And cdlPDPageNums) <> 0 Then
MsgBox " Printing pages " & CommonDialog1.FromPage & " to " & _
CommonDialog1.ToPage
Select Case startpage
Case 1
Case 2
' skip page 1
For i = 1 To 42
rsMyTable.MoveNext
Next
Case 3
' skip 2 pages
For i = 1 To 84
rsMyTable.MoveNext
Next
End Select
If startpage <> 0 Then
For j = startpage To CommonDialog1.ToPage
For i = 1 To 42
If rsMyTable.EOF Then Exit For
Text1.Text = Text1.Text & rsMyTable!CompanyName & vbCrLf
Printer.Print rsMyTable!CompanyName
rsMyTable.MoveNext
Next
Printer.NewPage
Next
Printer.EndDoc
End If
ElseIf (CommonDialog1.Flags And cdlPDSelection) <> 0 Then
rsMyTable.MoveLast
rsMyTable.MoveFirst
For i = 1 To rsMyTable.RecordCount
Text1.Text = Text1.Text & rsMyTable!CompanyName & vbCrLf
rsMyTable.MoveNext
Next
MsgBox "Select text to be printed"
Else
For i = 1 To rsMyTable.RecordCount
Text1.Text = Text1.Text & rsMyTable!CompanyName & vbCrLf
rsMyTable.MoveNext
Next
Printer.Print Text1.Text
Printer.EndDoc
MsgBox "Printing all pages"
End If
End Sub
Private Sub Command2_Click()
Printer.Print Text1.SelText
Printer.EndDoc
End Sub
Private Sub Form_Load()
Command1.Caption = "Select Printing Option"
Command2.Caption = "Print selected text"
End Sub
7. Run the project and test the various options.
REFERENCES
==========
For additional information on determining the margins and printable area of a
page, click the article number below to view the article in the Microsoft
Knowledge Base:
Q193943 HOWTO: Use GetDeviceCaps to Determine Margins on a Page
Additional query words:
======================================================================
Keywords : kbprint kbCmnDlgPrint kbCtrl kbPrinting kbVBp kbVBp400 kbVBp500 kbVBp600 kbGrpDSVB kbDSupport
Technology : kbVBSearch kbAudDeveloper kbZNotKeyword6 kbZNotKeyword2 kbVB500Search kbVB600Search kbVBA500 kbVBA600 kbVB500 kbVB600 kbVB400Search kbVB400
Version : WINDOWS:4.0,5.0,6.0
Issue type : kbhowto
=============================================================================