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
2103 Views

Similar Articles

[PageSpeed] 14

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:

Macro help #12
Hi, I am working on a macro in Excel 2003 Pro, and everytime I go to sort a column of data. There are about 10 rows of data in about 200+ columns. I can only get the macro to sort the column that I edited it with. How can I create the macro, so each time I click on the first row of each column, click the macro button I will assign to the toolbar, then that row will be sorted, and do this for each column? Any help at all would be greatly appreciated, Jeff Garrett (user_jeff@hotmail.com) Hi Jeff please post your current code and then we can suggest changes to it. Cheers JulieD "...

Excel 2007 Need to permanently change Normal settings for gridlines
I am using Excel 2007 for the first time and find the gridlines delineating the cells are so faint as to be nearly indistinguishable. I can go into the cell formatting and modify the normal style, and it is just the way I want it. But I can't find a way to make Excel remember this and treat it as the new definition of the Normal style. I don't want to have to redefine Normal every time I open a new spreadsheet. Someone please help! Regards Leonard Priestley The changes you describe are changing the Border color and NOT gridlines. Go into Excel Options and cl...

Pivot Table Cycling Through Page Fields Automatically
Hi. I am trying to cycle through a complete set of data in one of the parameters in the "Page" field. For example, there are 500 investments, and I want to compute the internal rate of return (IRR) for each investment based on a series of cashflows for each investment. The IRR is a function that is placed outside the pivot table. As each investment number is chosen, the underlying pivot table cashflow data changes, allow the IRR function to pick up these cashflows and compute the IRR. However, if there are 500 investments, this becomes very time consuming - especially if the...

How do I prevent "Help" from opening when I open Excel?
Whenever I open Excel 2003 the Help feature opens on the right. How do I pevent this? I only want Excel to open with a new sheet. -- Lee P Lee Help or TaskPane. For Help, you should be able to close it using the "x" and next time open Excel should not display. If TaskPane, go to Tools>Options>View and uncheck "Startup Task Pane" Gord Dibben Excel MVP On Mon, 12 Dec 2005 13:05:29 -0800, "Lee P" <LeeP@discussions.microsoft.com> wrote: >Whenever I open Excel 2003 the Help feature opens on the right. >How do I pevent this? I only want Exce...

SCHTask.exe returns "Failed to start" from SVR 2008
Hi, We have a number of 2003 servers which run lots of scheduled tasks around the clock. We also have some 2008 servers which do the same. We use SCHTask.exe from the 2008 servers, to query the scheduled tasks on the 2003 servers, and then look through the output of this query for errors, which is then used elsewhere to alert us that a scheduled task has failed on a given machine, and what the task is called. The problem we are having is that if a scheduled task runs sucessfully on a 2003 server, but has a NON ZERO return code, when SCHTask is run from a 2008 server and quer...

Printing as PDF
I have a portion of a sheet that I want to print/save as a pdf to a folder on my hard drive. The folders location is C:\Documents and Settings\Compaq_Owner\Desktop\Invoices. I also want the file name to be the value found in cell j19 on the "PO Form" worksheet. Any help? Hi Download and install Cute PDF-Writer (http://www.cutepdf.com/) It installs as a printer. Now, you can write any documents (Excel, Word, various pictures, web pages, etc. to PDF format. Simply open the document, select Print from menu, select CutePDF Printer, and when you click on Print button, a file save dia...

Publisher 2002 print issues
We have created a reference manual. What we see in the working document is not what goes to print. Some pages are fine while other pages have shifted pushing the title of page to the next page. Is there a way to fix the document in print preview mode? It appears to be an impossible problem! Please help if you can. thank you. There is no Print Preview mode. What you need to do is increase the size of the Text Frame fractionally. The easiest way to do this is by changing the Text Frame margins. Format Text Box > Format Margins. Publisher 2002 was a dog of a product in the area...

Printing problem from Excel 2007
Greetings, I am having an issue with printing from Excel 2007. Many users when printing from Excel with get several pages of non-sensical "junk" printed out on the pages. If it actually prints what is on the page, none of the items are in the cells, but look like they are just randomly thrown about the page. I have found that this most often is related to the Calibri font somehow, because I instruct them to change the font to Arial and all prints fine. That being said, if the user converts the Excel sheet into a PDF and print, it prints perfect. This is a great workaround, but kind ...

KB930879 needs clarification
Hi all, I just check the MS KB930879 reviewed on 2/22/2007. I am so confused now. Please look at the section what to do after you run exchange tool ******************************** What to do after you run the Exchange tool Install updates After you finish updating all Exchange servers in your environment, install the following updates on the Exchange servers: 931836 (http://support.microsoft.com/kb/931836/) February 2007 cumulative time zone update for Microsoft Windows operating systems 926666 (http://support.microsoft.com/kb/926666/) Update for daylight saving time changes in 200...

make a page permanent
I am new to onenote, I have set up some pages that I would like to make permanent. Is there away I can to that? It all depends on what you mean by "permanent." Can you clarify the task you have in mind? -- Thanks, John Guin OneNote Test Team http://blogs.msdn.com/johnguin "musicandbeer" wrote: > I am new to onenote, I have set up some pages that I would like to make > permanent. Is there away I can to that? John, I have made a note book with pages I never want to change, or atleast not very often. Every time I open it all the boxes show u...

Possible bug with MFC7 Print Preview?
Hi all, I have been writing an MFC SDI Doc/View app which makes use of the print preview facility provided. I have been experiencing some weird issues, so I decided to do a simple test. I created a new empty project of the same type using the MFC app wizard. I then built and run this new default project without making any changes, and the same problem occurs here. Both of these projects are using Microsoft Visual Studio .NET Pro 2003. I created the same default project using Visual C++ 5.0 Pro, and the problem does not exist. So it appears the bug is within the later version of the MF...

How to define specific cells as header and footer for printing?
I can define the header but not the footer. How to do this? Serdar, I think you're confusing "Rows to repeat at top" with the custom header, both in File - Page Setup. Rows defined as rows to repeat at top only appear at the top of the printed page. For stuff to appear at the bottom of every page, you can use the custom footer. You can't link it to cells in the sheet, but a macro can put cell contents into the custom footer. -- Earl Kiosterud www.smokeylake.com "serdar" <serdarsoy@yahoo.com> wrote in message news:uSvD8sEkFHA.576@tk2msftngp13.phx.g...

How do I display two excel pages at the same time?
I want to have two excel pages displayed on my computer at the same time, but when I open both pages it uses the same master excel and when I toggle between the two it won't open both of them up. I'm using office 2003. Thanks for the help. -- snowtime ------------------------------------------------------------------------ snowtime's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=25982 View this thread: http://www.excelforum.com/showthread.php?threadid=393499 Open both and then do windows>arrange and vertical (or any of the other choices, I usuall...

Do you need Access to use an Access Database for data entry only?
We have an Access database completed. Now we will use it for reading as well as for data entry. Do we need to install Access for every workstation needing the database for data entry or reading? No you don't necessarily need Access on every computer. You CAN use the Access Runtime and if it is in Access 2007 you can use it for free. If you are using another version then you would need to purchase the developer's edition to be able to legally distribute the associated Access runtime. If you do have 2007 and want to use the runtime, make sure your Full version of Access doe...

Date Range Formula Question
Hello, I'm having trouble with a formula and I'm hoping someone can help. :confused: Sample Data Includes the following: Pay Period Start Pay Period End Pay Period # 12/16/01 12/29/01 26 12/30/01 01/12/02 25 01/13/01 01/26/02 24 01/27/02 02/09/02 23 02/10/02 02/23/02 22 The pay periods continue until there are 26 pay periods for the entire year....

Page Number in Repeat Rows area
Is there a way that I can put a Page Number in the Repeat Rows area of a sheet - and get it to update when I print? I have found one piece of VBA that puts a page number within a cell but it is only updated if the cell is outside the Repeat Row area. When it is within the Repeat Rows area I just get a page number of 1 corresponding to the original row location.. I do some VBA programming but am not an expert. TIA cheers Chris Nothing comes to mind that doesn't require VBA code. You can paste the following in the sheet header (File | Page Setup) and page x of y will print on ea...

Print Macro #2
Trying to write a macro to print a spreadsheet where the number of rows of data varies. What's wrong with this code? Dim rng As Range Set rng = Range("A1").End(xlDown).End(xlToRight) With ActiveSheet.PageSetup .PrintArea = rng End With Thank you. Pam Hi Pam Leave your PrintArea empty Excel will print all cells with a value on the sheet -- Regards Ron de Bruin (Win XP Pro SP-1 XL2002 SP-2) www.rondebruin.nl "Pam" <gasp0225@juno.com> wrote in message news:16e401c38521$a7d25420$3501280a@phx.gbl... > Trying to write a macro to ...

Problems printing in Publisher 2003 with HP PSC 2410 #2
I am new to publisher 2003 and am having great difficulties printing anything properly using an HP PSC 2410 all in one. As an example when trying to print a half page side folded pre-designed greetings card all that prints is page 1 and part of page 4, minus any text. Pages 2 and 3 are missing altogether. The print preview however displays everything correctly. I have reinstalled Office 2003 and updated the printer drivers to the latest versions but the problem remains. Any help is much appreciated. ...

AAC/M4A VB Tag Decoder Source Code ??
Does anybody have source code that can read AAC/M4A audio files in order to extract the tags? I'm working in VB 6.0. "David Kaye" <sfdavidkaye2@yahoo.com> wrote in message news:i089v8$qrf$3@news.eternal-september.org... : Does anybody have source code that can read AAC/M4A audio files in order to : extract the tags? I'm working in VB 6.0. I just Googled it. Losts of example there. Any reason you couldn't try that first? -- Customer Hatred Knows No Bounds at MSFT Free usenet access at http://www.eternal-september.org ClassicVB Users ...

Printing Labels on Sato CL408
We are in the process of migrating from our existing proprietary POS and Inventory system to RMS. Part of the migration involves migrating the label printing function for our existing Sato CL408 label printers. I have done this successfully except for one detail. The labels are small labels (1 in. by 1.5 in) that are printed side-by-side. To get the second side-by-side label to print, I have to "fool" the label designer wizard into thinking it is printing to a label 3.5 inches wide. The data on the left side is duplicated on the right side, resulting in two labels being p...

HELP!!! 08-30-03
please elp me. my mom is a medical transcriber and she has a dictionary she uses through windows 98. and her comp has no memory, so we need to update her operating system inoder to put more memory on there. she needs her dictionary and i was wandering how to access it and if possible how to save it on to a cd. thank you for ur time Dear jason you have to have a dictornary corect i have one i could email you if you like just email me back i mean i use this dictornary really well and it works for 95-Xp so if you want give me a hollar ok peace out danny "jason" <jasonr_206@hotm...

Exchange server recovery advice needed
Folks We have been hit by a relatively big disk crash and I have to rebuild our exchange 2003 (SP1) server Fortunately I have a fully working backup but I still need to "replay" some LOG files as we only do weekly "full" snapshot. So to cut a long story short I have recovered and restarted the server as it was one week ago and everything seems fine EXCEPT for the messages (and other items) created since last backup. I DO have the log file for those days... but how do I "replay" them ? What is my best course of action ? At the moment I plan to create a separat...

SBS 2003 migration to Server 2008 STD
Hi. We are at the limit of 75 Users and want to Migrate away from our old SBS 2003 Server to two new 2008 STD Server (one for Exchange 2010) I have looked for infos how to do that correctly but i could not find any good basis to start with. Someone already accomplished that task? I recommend Server 2008 R2 over 2008. If you use Server 2008 make sure you have SP2 or better installed before you try to install Exchange 2010. I have done it. It was fairly straightforward. That said it was not easy. There is a lot of conflicting information on the web. The basic procedure is...

help with installation isue
Hope this is correct NG When I try to run `Karens replicator` (for external backup) My PC automatically runs an installation for publisher 2003 and then gives error message........... Istallation error require file SKU019.CAB then asks for original CD to be inserted. Publisher still works ok I can`t see what the relationship between the 2 are, how do I stop this trying to install? thanks -- Rent a villa in Turkey http://www.freewebs.com/turkeyrent/ I would suggest aksing the folks over at karenware.com. http://www.karenware.com/powertools/troubl...

Outlook 2002 multiple file opening
In Outlook 2000, CTRL A would select all attached picture files, then Open would, in my case, open the files in Paintshop Pro. With the 2002 version I cannot find a way to do this. Any suggestions? ...