Microsoft KB Archive/248882

From BetaArchive Wiki
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Knowledge Base


Article ID: 248882

Article Last Modified on 7/1/2004



APPLIES TO

  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 6.0 Learning Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic 6.0 Enterprise Edition
  • Microsoft Visual Basic 4.0 Standard Edition
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 4.0 32-Bit Enterprise Edition



This article was previously published under Q248882

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:

193943 How To 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:

193943 How To Use GetDeviceCaps to Determine Margins on a Page


Keywords: kbhowto kbprint kbcmndlgprint kbctrl KB248882