Need help printing multiple pages in VB.Net 2008

I am having a problem printing multiple pages with a common header/footer etc 
in VB 2008.

I have the following code which will print a single page exactly as I need it.


'***************** Print button click event ************

Private Sub cmdPrint_Click(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles cmdPrint.Click

        pdPrintCal.PrinterSettings.PrinterName = lblCurPrt.Text
        pdPrintCal.DefaultPageSettings.Landscape = True

        If chkPrtPView.Checked Then

            pvPrintCal.Document = pdPrintCal
            pvPrintCal.Icon = Me.Icon
            pvPrintCal.ShowDialog()

        Else
           
            pdPrintCal.Print()
       
        End If

    End Sub

'************************************

'***** _PrintPage Sub *************************

Private Sub pdPrintCal_PrintPage(ByVal sender As System.Object, ByVal e As 
System.Drawing.Printing.PrintPageEventArgs) Handles pdPrintCal.PrintPage

        Dim PageNum as Integer = 1
        Dim PDate As Date = Today


        Dim CenterString As New StringFormat
        CenterString.Alignment = StringAlignment.Center
        CenterString.LineAlignment = StringAlignment.Near

        Dim RightAlignString As New StringFormat
        RightAlignString.Alignment = StringAlignment.Far
        RightAlignString.LineAlignment = StringAlignment.Near

'***** Start of Header section, this is same for all Pages *****

        Dim newImage As Image = Image.FromFile("C:\Documents and 
Settings\UserName\My Documents\Visual Studio 2008\Projects\EMS Clinical 
V2\EMS SPC Logo.bmp")
        Dim ImgW As Integer = newImage.Width * 0.85
        Dim ImgH As Integer = newImage.Height * 0.85

        e.Graphics.DrawImage(newImage, 50, 9, ImgW, ImgH)


        e.Graphics.DrawString("Sandford College Clinical Schedule for:", New 
Font("Times New Roman", 16, FontStyle.Bold), Brushes.Black, 550, 10, 
CenterString)

        e.Graphics.DrawString(lblCalName.Text, New Font("Times New Roman", 
16, FontStyle.Bold), Brushes.Black, 550, 42, CenterString)

        e.Graphics.DrawString("Date Printed: " & 
PDate.Date.ToShortDateString, New Font("Times New Roman", 12, 
FontStyle.Bold), Brushes.Black, 1050, 50, RightAlignString)

        e.Graphics.DrawLine(Pens.Red, 50, 70, 1050, 70)

'******** End of header section ********

'******** Print unique data here, may take more than one 8 1/2 x 11 page to 
print all data ***************

e.Graphics.DrawString("Print a bunch of Data here.", New Font("Times New 
Roman", 16, FontStyle.Bold), Brushes.Black, 550, 100, CenterString)

'************* End of unique data ************

'************** Start of Footer which just prints a Page Num *******

        e.Graphics.DrawString("Page number: " & PageNum, New Font("Times New 
Roman", 16, FontStyle.Bold), Brushes.Black, 550, 750, CenterString)

'******************** End of Footer *********

'******* Here I put code to see if I need another page to finish printing my 
data ****

if MoreData = true then

PageNum += 1

'******* Code I need help with to cause new page etc. goes here ******

' *** Have tried... e.HasMorePages = True ... then looping back to top of 
routing etc...

end if


    End Sub


I have tried several different ways to get it to print more than one page 
but It either fails to print one page or seems to print an infinate number of 
pages.

As a simple test I tried the following....


'***** _PrintPage Sub *************************

Private Sub pdPrintCal_PrintPage(ByVal sender As System.Object, ByVal e As 
System.Drawing.Printing.PrintPageEventArgs) Handles pdPrintCal.PrintPage

        Dim PageNum as Integer = 1
        Dim PDate As Date = Today


        Dim CenterString As New StringFormat
        CenterString.Alignment = StringAlignment.Center
        CenterString.LineAlignment = StringAlignment.Near

        Dim RightAlignString As New StringFormat
        RightAlignString.Alignment = StringAlignment.Far
        RightAlignString.LineAlignment = StringAlignment.Near

For PageNum = 1 to 3  '**** This For next loop is used to create 3 pages 
with PageNum being only difference ******


'***** Start of Header section, this is same for all Pages *****

        Dim newImage As Image = Image.FromFile("C:\Documents and 
Settings\UserName\My Documents\Visual Studio 2008\Projects\EMS Clinical 
V2\EMS SPC Logo.bmp")
        Dim ImgW As Integer = newImage.Width * 0.85
        Dim ImgH As Integer = newImage.Height * 0.85

        e.Graphics.DrawImage(newImage, 50, 9, ImgW, ImgH)


        e.Graphics.DrawString("Sandford College Clinical Schedule for:", New 
Font("Times New Roman", 16, FontStyle.Bold), Brushes.Black, 550, 10, 
CenterString)

        e.Graphics.DrawString(lblCalName.Text, New Font("Times New Roman", 
16, FontStyle.Bold), Brushes.Black, 550, 42, CenterString)

        e.Graphics.DrawString("Date Printed: " & 
PDate.Date.ToShortDateString, New Font("Times New Roman", 12, 
FontStyle.Bold), Brushes.Black, 1050, 50, RightAlignString)

        e.Graphics.DrawLine(Pens.Red, 50, 70, 1050, 70)

'******** End of header section ********

'******** Print unique data here, may take more than one 8 1/2 x 11 page to 
print all data ***************

e.Graphics.DrawString("Print a bunch of Data here.", New Font("Times New 
Roman", 16, FontStyle.Bold), Brushes.Black, 550, 100, CenterString)

'************* End of unique data ************

'************** Start of Footer which just prints a Page Num *******

        e.Graphics.DrawString("Page number: " & PageNum, New Font("Times New 
Roman", 16, FontStyle.Bold), Brushes.Black, 550, 750, CenterString)

'******************** End of Footer *********

'******* Here I put code to see if I need another page to finish printing my 
data ****



'******* For my test to see if I can control multi page printing I used the 
following in conjunction with the For Next Loop....  ******

    If PageNum < 3 then

          e.HasMorePages = True

    Else

         e.HasMorePages = False

    End If

    Next

    End Sub

To make a long story short this did not work. All I got was one page and the 
3 page numbers were writen on top of each other.

How can I make this work?

Any help would be greatly appreciated.

I would also like to know how to put the header and footer in to a seperate 
Sub I could call as needed in the _PrintPage Sub in a maner similar to the 
following one but that is not as important as just getting it to create new 
pages as needed....

'****** Psudo Code..... ***


Private Sub PrintHeader ()

' ***** Do all the printing for the Header section used above ****

End Sub

Private Sub PrintFooter (ByVal PageNum as Integer)

' ***** Do all the printing for the Footer section used above ****

End Sub


Private Sub pdPrintCal_PrintPage(ByVal sender As System.Object, ByVal e As 
System.Drawing.Printing.PrintPageEventArgs) Handles pdPrintCal.PrintPage


Define Variable and constants.....

'**** Send Header to the printer....

       PrintHeader()

'******* Send Data to Printer ......

e.Graphics.DrawString("Print a bunch of Data here.", New Font("Times New 
Roman", 16, FontStyle.Bold), Brushes.Black, 550, 100, CenterString)


'*********** Send Footer to Printer......

PrintFooter(PageNum)

'******** Test for more data to print *****

If MoreData = True

PageNum = PageNum + 1

Loop back to Print New page....

Else

End Print Job

End Sub


This was a lot eaisier in VB6. I am currently converting a program I wrote 
in VB6 to VB2008.Net which is why I am here needing help. This is my first 
VB.Net project and the printing of my data is the last hurdle I need to get 
over.


Thanks again for all the help,

Ralph Malph
0
Utf
4/29/2010 9:31:01 PM
dotnet.languages.vb 830 articles. 0 followers. Follow

2 Replies
1837 Views

Similar Articles

[PageSpeed] 59

Hello, Ralph Malph,

What you need to do is set e.HasMorePages = True and then exit the routine.  
That is, you don't loop back to the top.  If e.HasMorePages is True the 
PrintPage routine will be called again (and continue to be called until 
e.HasMorePages is False).  After the last page has been printed set 
e.HasMorePages = False so that the PrintPage routine won't be called again.

One issue that you will have to deal with is that your PageNum variable is 
local (i.e. private) to the PrintPage routine.  So the value will be reset to 
one for each new page -- probably not what you want.

Re: "I would also like to know how to put the header and footer in to a 
seperate 
Sub " 
I can't see any reason why you would have a problem doing this.  Just make a 
real implementation of what you have shown as pseudo code.

Cheers,
Randy

0
Utf
4/29/2010 11:30:54 PM
Randy,

Thank you for the info. I needed to know how the "Flow" logic worked for the 
e.HasMorePages function. Exiting the Sub and making the PageNum variable good 
for the whole form got my print routine working AOK.

I knew it had to be something simple.

Now to get my Datagrid to print....

Thanks again,

Ralph Malph


"OmegaSquared" wrote:

> Hello, Ralph Malph,
> 
> What you need to do is set e.HasMorePages = True and then exit the routine.  
> That is, you don't loop back to the top.  If e.HasMorePages is True the 
> PrintPage routine will be called again (and continue to be called until 
> e.HasMorePages is False).  After the last page has been printed set 
> e.HasMorePages = False so that the PrintPage routine won't be called again.
> 
> One issue that you will have to deal with is that your PageNum variable is 
> local (i.e. private) to the PrintPage routine.  So the value will be reset to 
> one for each new page -- probably not what you want.
> 
> Re: "I would also like to know how to put the header and footer in to a 
> seperate 
> Sub " 
> I can't see any reason why you would have a problem doing this.  Just make a 
> real implementation of what you have shown as pseudo code.
> 
> Cheers,
> Randy
> 
0
Utf
4/30/2010 1:38:01 PM
Reply:

Similar Artilces:

Not sure if Excel can do what i need
Hello all, I am obviously a new user to excel. I have a psychological assessmen that needs to be computerised for ease of use in obtaining results. Here is what i would like to happen. I have 28 questions with fiv Likert scale options(0-4) for answers. Each question addresses 1 of different personality traits. So questions 1, 6, 8, 22 look at anger. Where as questions 2, 9, 28 look at happiness. What i would like is fo excel to group the answers and give me the total number for each trait. Then based on that number, give out a prewritten (which i would provide result of the assessment. ...

Need Min Value from Row and Heading Value that Corresponds to it.
I have a table that looks somewhat like this w/o the *'d headings: h1 h2 h3 h4 h5 h6 h7 *Min Price *Heading $5.00 $6.00 $3.00 $4.00 $9.00 $1.00 $9.00 $1.00 h6 $3.00 $6.00 $8.00 $65.00 $9.00 $2.00 $4.00 $2.00 h6 $4.00 $9.00 $5.00 $7.00 $1.00 $2.00 $8.00 $1.00 h5 $9.00 $7.00 $6.00 $3.00 $9.00 $8.00 $2.00 $2.00 h7 $7.00 $8.00 $9.00 $7.00 $9.00 $8.00 $8.00 $7.00 h1 I have several SKU's with various price contract/programs. I want to find the best price in one cell and the heading that would correspond to that cell in another. I was trying to do this with an expression ...

Print any file to a specific printer (silently)
Hi, I have a requirement where I have to print any given file to a specific printer. My application then consumes the output of this printer. I also need to do this without any user intervention and without any GUI. I have managed to get the printing done using Shell Execute, however, it always shows the associated application even though I have specified ProcessWindowStyle.Hidden as window style. My code snippets are given below: <CODE> Process objProcess = new Process(); objProcess.StartInfo.FileName = fileName; //file to print objProcess.StartInfo.WindowStyle = ...

Excel need help
I have 2 columns a and b . a contains payment b contains balance. I have the entire col b with the formula. my ? is when the balance appearsin b it is in all the cells in that column.I only want it in the used ones, not the unused ones:( example $270.00 16,730.00 $270.00 16,460.00 $100.00 16,360.00 $300.00 16,060.00 $270.00 15,790.00 15,790.00 15,790.00 15,790.00 15,790.00 15,790.00 this 15790.00 is in col b...not a...I want no bal in unused cells -- fiftieslady ------------------------------------------------------------------------ fiftieslady's Profile: http://www.excelforum.co...

PRINT SHORTCUT
Hi Sorry if this has been asked before, I have just installed Publisher 2007 after using 2003 for many years. To my dismay when I click on the 'print' icon in the toolbar, it prints all pages instead of the current one (that is, it remembers my previous choice for printing). I do inserts for greetings cards and have sometimes over a hundred pages in my folders, it was just one click before to print the current chosen page, now I have to bring up the print dialogue box, and choose 'current page ' again for each one - can any kind person tell my how to get round this o...

HELP.....Setting up Outlook express again
I've just installed everything on my computer again and im having trouble with outlook express. its asking for my incoming and outgoing server names and i have no idea what they are or where i get them, so i can access or set my account up again. if anyone knows can they please help me thanks!!!! you can reach me at : bl182ink@hotmail.com Amanda Arbuckle what type of account? who is the email provider? have you checked their website for assistance? -- "Amanda" <bl182ink@hotmail.com> wrote in message news:A37A86BC-5E22-4B1F-B6E3-9459C70971C1@microsoft.com... > I...

Need Help Linking Sheets
How can I link 2 pages without using the actual sheet as a reference? In other words, can I have two sheets linked by Identifying the same exact names one on each sheet? ...

Excel newbie needs help from math wiz (poker related)
I want to create a Excel workbook that will help in specific poker situations. Short version: I want to be able to calculate how often my opponent will need to fold for an all-in raise to show a positive expectation. Explanation: In poker you can win a hand by having the best hand at the end OR getting your opponent to fold. A common situation comes up where you have a "drawing hand" and face a bet by your opponent. In this situation you know you are currently behind in the hand, but have a chance to win if you catch some of your "outs" (cards that will give you th...

does not print but saves instead!
I have 4 profiles set up on my computer. When I use word on the main profile I can print normally. However when I try to print from word on the other 3 profiles it goes to save as and will not let me print. I have tried reparing office using my disc. Any ideas on how to restore the print settings? ...

upgrade from office 2004 to 2008
hi upgraders, <br> are there any problems regarding the upgrade to 2008, esp. assuming all old data like emails, dates and so on ? <br> thanx In article <ee89959.-1@webcrossing.caR9absDaxw>, realist01@officeformac.com wrote: > hi upgraders, <br> > are there any problems regarding the upgrade to 2008, esp. assuming all old > data like emails, dates and so on ? <br> You can read on these groups that there are some people who have had problems. Of course, the people that haven't had problems usually don't post! While Entourage 2008 will impor...

Multiple Recipient Policies #2
I have a dilema. I have created (2) storage groups in Exchange 2003 SP1. I had my default storage group for a while but I wanted to start archiving. I allready had my maximum (4) mailbox stores on my default so I created a second storage group called "Archive Storage Group" with a single mailbox store also called "Archive". Here is my question; How do I set a retention (recipient) policy on my Archive storage group/user without affecting the default storage group? Currently my recipient policy deletes anything in deleted items older than 7 days. For my Exchange Archive use...

Macro help needed........
How do I sort a particular column in ascending order and change the pag setup to landscape and margins to 0.25 using a macro code ? does anyone have a macro code for this ????? -- Message posted from http://www.ExcelForum.com Hi you have already received some answers in your previous threads please don't multipost! -- Regards Frank Kabel Frankfurt, Germany > How do I sort a particular column in ascending order and change the > page setup to landscape and margins to 0.25 using a macro code ? > > does anyone have a macro code for this ?????? > > > --- > Me...

Need to test a mobo
I had a power surge about 3 weeks ago - lost a drive, now making ticking noises. I had another two drive that seem to work sporadically now. So, I thought it might have been my power supply. Bought a tester and found out that all seems well. Next, I bought a new hard drive and found that it was also acting up - making noises like whirring up and down (like the other drives I thought were dead). So I am starting to wonder if it is my mobo - but how can I test this out? I am using a dual boot - Win XP and Win 7 - both drives work and don't work. Up and down. Right now my Win7 ...

A full page of email text shrinks to one inch or less w/ new batte
I was answering emails and everyhting was fine. The batteries in my wireless mouse died. I replaced them and went back to answering emails. When I clicked the "forward" or "reply" button , a full page text in an email shrank to about one inch. Changing the font size did not correct the problem. HELP it's your zoom setting. Hold ctrl and roll the mouse wheel to increase. Depending on the mouse, you could have accidently pressed a mouse button and changed it when you changed the batteries. -- Diane Poremsky [MVP - Outlook] Outlook Tips: http://www.o...

Publisher 2002 Inserting a landscape page into portrait document
How can I insert one landscape page into a presentation consisting portrait pages? I understand the inserting of page numbers, but I can't find the page orientation. Publisher does not allow you to have different orientation pages. Group all items on the page and rotate 90�. -- ...

Display Multiple User Forms
Is there a way to display two userforms simulataneously. I have an application with about 10 data entry forms. I want to creat a navigation form that would always be open that would allow the use to jump from one form to another. How can I do this? Thanks for any help -- Message posted from http://www.ExcelForum.com If you have Excel2000 or later, just create modeless forms (ShowModal property = False) -- HTH Bob Phillips ... looking out across Poole Harbour to the Purbecks (remove nothere from the email address if mailing direct) "Ripan >" <<Ripan.103gp7@exce...

Help needed
is there anyway you can see what links to other workbooks are associated with the workbook you have open. The problem i am having is when i open the workbook in question a message appears and states "The workbook you opened contains automatic links to information in another workbook. do you want to update this workbook with changes made to the other workbook" Yes....... No. if i click No the workbook opens, however if i click Yes the workbook just hangs and i have to go into task manager to shut down the system. I can only assume that there is a problem with one of the links....

Help with formula 12-17-09
Let's say I have in cell A1 a combo box with options: 1. Expense 2. Refund 3. Tax In cell B1 I will type a number In cell C3 I want a formula like this: If in cell A1 I have selected 1. Expense than calculate B1*25 If in cell A1 I have selected 2. Refund than calculate B1*50 If in cell A1 I have selected 3. Tax than calculate B1*150 How can I apply the combo box to entire column, like if I go to row 2 to find in B1 the combo box from A1 and in C2 the formula from C1 Thanks for the help In c1: =IF(A1="Expense",B1*25,IF(A1="Refund",B1*50,B1*150)) Micky ...

Need help on this....thanks
I have an Excel worksheet which stored a survey outcome of over 1200 members. There are over 30 questions in the survey and the first column captured the member ID. The problem is 3 of the survey questions that allow multiple answers, instead the answer for those three questions are captured in seperate column, the answers are stored in a single column and it makes it very difficult to analysis those result. As a result I created addittional columns depending on the total number of the answers available for that question and assign the result manually to that column. It is very time consuming ...

Help with Error
I got this error recently, and just want to make sure I'm reading it right. Logon Failure on database "First Storage Group\Mailbox Store (EXCHANGE)" - Windows 2000 account PARADIGM\user1; mailbox /o=Paradigm/ou=First Administrative Group/cn=Recipients/cn=user2. Error: -2147221231 Does this mean the user1 tried to access the user2 mailbox and failed? Thanks in advance hello albert, well, this looks familiar to: http://support.microsoft.com/default.aspx?scid=kb;en-us;260782 and http://support.microsoft.com/default.aspx?scid=kb;en-us;309113 regards, Alex "AlbertP&quo...

I need Help
I have a random popup that i cant stop,when it does come up it seems to know what i was looking at,like when i was looking for registry cleaner it popped up and showed me one,any help would be great. On Jan 22, 6:57=A0am, Larry <La...@discussions.microsoft.com> wrote: > I have a random popup that i cant stop,when it does come up it seems to k= now > what i was looking at,like when i was looking for registry cleaner it pop= ped > up and showed me one,any help would be great. To eliminate questions and guessing, please provide additional information about your sys...

need help badly
How do you print ole objects.. Thanks lachanda -- Message posted via AccessMonster.com http://www.accessmonster.com/Uwe/Forums.aspx/access-forms/200710/1 On Wed, 03 Oct 2007 18:13:40 GMT, "misschanda via AccessMonster.com" <u36612@uwe> wrote: >How do you print ole objects.. >Thanks >lachanda What *kind* of OLE objects? Many of them are binary blobs (such as programs) which are inherently not printable. More details please! John W. Vinson [MVP] ...

Invoice print order
Can invoices be printed in customer NAME order? Laura, I don't believe so. The reason I don't believe that will work is that the sort is hard coded in Dexterity and while I haven't tested this in a while, I'm pretty sure the "sort by" clause as issue by Dexterity will override any user added sorts. patrick developer support -- This posting is provided "AS IS" with no warranties, and confers no rights. "Laura Anne" <LauraAnne@discussions.microsoft.com> wrote in message news:0FC967CD-80DC-431A-AA4F-F1FBC75F88F4@microsoft.com... >...

Outlook hangs on pointing to the print button
We have one user that has this problem. When he points to the print button in Outlook, Outlook hangs for up to two minutes. Any idea where to look for the cause of this issue? Thanks, Neal Hello Neal, Could you give us more information about the problem? - Could you confirm than only one user has his behavior? - What's the Outlook version? - Did the user install Add-in, software which generete .pdf? - Does the hang of Outlook occur each time when pointing to the print button? - You can disable all third part software and disable Add-in of Ooutlook as well Let me know if the i...

Stuck in review and need to be in design.
I am using Visio 2007. I need to complete the diagram that I have been working on and I hit the review key by mistake. How do I get back to the design screen? On Wed, 4 Nov 2009 16:14:01 -0800, cameron <cameron@discussions.microsoft.com> wrote: >I am using Visio 2007. I need to complete the diagram that I have been >working on and I hit the review key by mistake. How do I get back to the >design screen? menu Tools -> Track Markup and then close the Review window. -- Regards, Paul Herber, Sandrila Ltd. DFD/SSADM for Visio http://www.visio-dfd.sa...