Controlling Printing and Printer Setup on the Viewer

The viewer control has a Printer property that you can use to set up the printer or call the print method for some pages in the Pages collection.

Note: Paper size and page orientation are not reflected in the report until you restart it since the report has been rendered to the original print specification.

You can create a global printer object and use it for all reports included in your application. Before starting each report you can set the printer property of the report to the global printer object.

Using the same preview form, implement printer setup and print menu options. Add the following code to your form:

  1. Continuing with the project started above, add the following code to the Form_Load event:

    Private Sub Form_Load()
    addButtonsToARV
    ' This overrides the print tool
    For cnt = 0 To arv.ToolBar.Tools.Count - 1
    If "&Print..." = arv.ToolBar.Tools(cnt).Caption Then
    arv.ToolBar.Tools(cnt).ID = CONST_PRINTTOOLID
    End If
    Next cnt
    End Sub

  2. Modify the arv_ToolbarClick event to handle the printer click event:

    Private Sub arv_ToolbarClick(ByVal Tool As DDActiveReportsViewer2Ctl.DDTool)
    Dim bReturn As Boolean
     
    Select Case Tool.Caption
    Case Is = "O&pen"
    'call the open sub
    tbOpen
    Case Is = "&Close"
    'call the exit sub
    tbExit
    Case Is = "&PDF"
    'call the PDFExport sub
    tbPDFExport
    Case Is = "&Save"
    'call the Save sub
    tbSave
    Case Is = "&Print..."
    arv.UseSourcePrinter = True
    arv.Printer.SetupDialog
    bReturn = arv.Printer.PrintDialog(Me.hWnd)
    If bReturn Then
    arv.PrintReport False
    CancelledJob = False
    Else
    CancelledJob = True
    End If
    End Select
    End Sub

  3. Modify the Open sub to enable the printer button:

    Private Sub tbOpen()
    cmndlg.Filter = "Report Document File (*.rdf)|*.rdf"
    cmndlg.ShowOpen
    If cmndlg.FileName <> "" Then
    If Not arv.ReportSource Is Nothing Then
    Set arv.ReportSource = Nothing
    End If
    arv.Pages.Load cmndlg.FileName
    'Enables buttons when a report is loaded
    arv.ToolBar.Tools.Item(5).Enabled = True
    arv.ToolBar.Tools.Item(arv.ToolBar.Tools.Count - 1).Enabled = True
    'Enable print button
    arv.ToolBar.Tools.Item(7).Enabled = True
    End If
    End Sub