Problem with VB code printing an extra page

stanger1

Registered User.
Local time
Today, 12:40
Joined
Jul 30, 2002
Messages
53
I have this VB Module that prints a report. The first page prints on letterhead and the rest prints on regular paper. This code is set up to print on two trays. My problem is that when I print a report that is only 1 page, a second page will print. The second page will have page number 2 of 1 and will only print the heading info. When I print multiple pages, this does not happen. Below is my code. Can anyone look this over and provide some insight? Any help would be greatly appreciated.



Type zwtDevModeStr
RGB As String * 94
End Type

Type zwtDeviceMode
dmDeviceName As String * 16
dmSpecVersion As Integer
dmDriverVersion As Integer
dmSize As Integer
dmDriverExtra As Integer
dmFields As Long
dmOrientation As Integer
dmPaperSize As Integer
dmPaperlength As Integer
dmPaperWidth As Integer
dmScale As Integer
dmCopies As Integer
dmDefaultSource As Integer
dmPrintQuality As Integer
dmColor As Integer
dmDuplex As Integer
dmResolution As Integer
dmTTOption As Integer
dmCollate As Integer
dmFormName As String * 16
dmPad As Long
dmBits As Long
dmPW As Long
dmDFI As Long
dmDRr As Long
End Type


Sub setPaperSource1(rptName As String)
Dim rpt As Report
Dim DM As zwtDeviceMode
Dim DevString As zwtDevModeStr
Dim DevModeExtra As String

DoCmd.SetWarnings False
' Set Paper Tray for page 1
DoCmd.OpenReport "cert report official", acViewDesign, , "Invoice_no = " & INVOICE_NO
Set rpt = Reports("[cert report official]")
DevModeExtra = rpt.PrtDevMode
DevString.RGB = DevModeExtra
LSet DM = DevString
DM.dmDefaultSource = 2 '1 = Upper Tray, 2 = Lower Tray, 5 = _
Envelope Feeder
LSet DevString = DM
Mid$(DevModeExtra, 1, 68) = DevString.RGB
rpt.PrtDevMode = DevModeExtra
DoCmd.Save acReport, "cert report official"
DoCmd.SelectObject acReport, "cert report official", True
DoCmd.PrintOut acPages, 1, 1, copies, 2

' Set Paper Tray for page 2
DoCmd.OpenReport "cert report official", acViewDesign
Set rpt = Reports("[cert report official]")
DevModeExtra = rpt.PrtDevMode
DevString.RGB = DevModeExtra
LSet DM = DevString
DM.dmDefaultSource = 1 '1 = Upper Tray, 2 = Lower Tray, 5 = _
Envelope Feeder
LSet DevString = DM
Mid$(DevModeExtra, 1, 68) = DevString.RGB
rpt.PrtDevMode = DevModeExtra
DoCmd.Save acReport, "cert report official"
DoCmd.SelectObject acReport, "cert report official", True
DoCmd.PrintOut acPages, 2, 20, copies, 2

DoCmd.SetWarnings True
End Sub
 
Maybe there needs to be logic between your paper tray assignments to determine if there is more to the report. With your existing code you appear to open the report twice and force it to print something both times.
 
Look into the Pages Collection features of the report object. Use the Count method of the Collection. If the Count is equal to 1, then exit the sub PaperSource following the first page printed.

In your example...

.
.
.
DoCmd.SelectObject acReport, "cert report official", True
DoCmd.PrintOut acPages, 1, 1, copies, 2

'Inserted code
if rpt.Pages.Count = 1 then Exit Sub


' Set Paper Tray for page 2
DoCmd.OpenReport "cert report official", acViewDesign
.
.
.

of course, check for error handling and close all objects properly
 
Pages property of the report object is not a collection.

Sub SetPaperSource1()
Dim rpt As Report

'First tray assignment

Set rpt = Reports![cert report official]
If rpt.Pages > 1 Then

'other tray assignment

End If
End Sub
 
I tried your suggestion and it worked! Thanks for your help!
 

Users who are viewing this thread

Back
Top Bottom