I've been learning to code for the last year. This question might have been answered, but I can't seem to string the write combo together to google the answer up.
I'm trying to print a multi-page set of forms from a windows form. Think Open Enrollment forms for the year for each employee at my organization. I can get all the forms to print, but when I move to the next record, the print job seems to start where the e.hasmorepages=false left off. I'm getting a blank page. I've got a few combos depending on what they chose. If the next record happens to have a different combo- it prints fine. It's when it comes back to one of the combos that has already printed I get the blank page. It's like the print sub doesn't exit fully.
I've tried "Exit Sub" and "Return" in multiple locations throughout my code. Nothing seems to work. I'm posting some code below, know that this is very abbreviated as my actual code is like 15,000 rows long.
Imports System.Drawing.Printing
Public Class PrintOEForms
Dim PrintAll As Integer = 0
Dim Print1 As Integer = 0
Private Sub cmdPrintDocs_Click(sender As Object, e As EventArgs) Handles cmdPrintDocs.Click
If cbUpdate911.Checked = True AndAlso cbUpdateCheckRel.Checked = True AndAlso cbUpdateW4.Checked = True AndAlso cbOE22.Checked = True AndAlso cbNoIns.Checked = True Then
PrintDocAll.DefaultPageSettings.Margins = New Printing.Margins(50, 50, 50, 50)
PrintDialog1.Document = PrintDocAll
If PrintDialog1.ShowDialog() = DialogResult.OK Then
PrintDocAll.Print()
****'I've tried "Exit Sub" and "Return" here****
End If
PrintDialog2.Document = PrintDocAll
End If
If cbUpdate911.Checked = True AndAlso cbUpdateCheckRel.Checked = True AndAlso cbUpdateW4.Checked = True AndAlso cbOE22.Checked = True AndAlso cbNoIns.Checked = True Then
PrintDoc1.DefaultPageSettings.Margins = New Printing.Margins(50, 50, 50, 50)
PrintDialog1.Document = PrintDoc1
If PrintDialog1.ShowDialog() = DialogResult.OK Then
PrintDoc1.Print()
End If
PrintDialog2.Document = PrintDoc1pdf
End If
End Sub
Private Sub PrintDocAll_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocAll.PrintPage
Select Case PrintAll
Case 0
'Confidential Cover Sheet
e.HasMorePages = True
Case 1
'Ins Options
e.Graphics.DrawImage(pbInsOptions.BackgroundImage, 50 15, 50 20)
e.HasMorePages=True
Case 2
'Ins Pg1
e.Graphics.DrawImage(pbIns1.Image, 13, 15)
e.HasMorePages = True
Case 3
'Ins Pg2
e.grahics.DrawImage(pbIns2.Image, 13,15)
e.HasMorePages = True
End Select
PrintAll = 1
End Sub
Private Sub PrintDoc1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDoc1.PrintPage
Select Case Print1
Case 0
'Confidential Cover Sheet
e.HasMorePages = True
Case 1
'Ins Options
e.Graphics.DrawImage(pbInsOptions.BackgroundImage, 50 15, 50 20)
Case 2
'Ins Pg1
e.Graphics.DrawImage(pbIns1.Image, 13, 15)
e.HasMorePages = False
**'I've tried "Exit Sub" and "Return" here**
End Select
Print1 = 1
End Sub
End Sub
CodePudding user response:
Change this:
If PrintDialog1.ShowDialog() = DialogResult.OK Then
PrintDocAll.Print()
End If
To this:
If PrintDialog1.ShowDialog() = DialogResult.OK Then
PrintAll = 0
PrintDocAll.Print()
End If
and this:
If PrintDialog1.ShowDialog() = DialogResult.OK Then
PrintDoc1.Print()
End If
to this:
If PrintDialog1.ShowDialog() = DialogResult.OK Then
Print1 = 0
PrintDoc1.Print()
End If
Otherwise those values are not reset after each document and you'll start the next one after already advancing to the end of the sequence.
CodePudding user response:
Have you tried to initialize the printDocument object? Perhaps changing
If cbUpdate911.Checked = True AndAlso cbUpdateCheckRel.Checked = True AndAlso cbUpdateW4.Checked = True AndAlso cbOE22.Checked = True AndAlso cbNoIns.Checked = True Then
PrintDocAll.DefaultPageSettings.Margins = New Printing.Margins(50, 50, 50, 50)
into:
If cbUpdate911.Checked = True AndAlso cbUpdateCheckRel.Checked = True AndAlso cbUpdateW4.Checked = True AndAlso cbOE22.Checked = True AndAlso cbNoIns.Checked = True Then
PrintDocAll = new PrintDocument
PrintDocAll.DefaultPageSettings.Margins = New Printing.Margins(50, 50, 50, 50)
In the following procedure:
Private Sub PrintDocAll_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocAll.PrintPage
Select Case PrintAll
Case 0
Case 1
Case 2
Case 3
'Ins Pg2
e.grahics.DrawImage(pbIns2.Image, 13,15)
e.HasMorePages = True ' This should be = False
End Select
PrintAll = 1
End Sub
In Case 3, set e.HasMorePages to False. Also, before printing reset PrintAll to zero.