Vba printing a worksheet in vba in Excel
Printing a document is a routine task performed by almost everyone, who works with MS Excel.
The printing of documents has the same set of procedures irrespective of the type of application used, either Word, PowerPoint or Excel.
However, since Excel documents are tabular, there rises a need to accommodate the whole data(columns) in a single page within the print range.
This is where the Excel document printing differs from the rest.
With large number of rows, it can be printed in multiple pages, whereas, the columns cannot be split in most cases.
So, This chapter aims to provide some details on various printing methods followed in Excel.
The VBA has various methods and properties associated with printing worksheets.
These are as explained below.
PrintOut method
This method is used to instruct the application to print the worksheet.
The various settings set through the code are taken into account.
Syntax
PrintOut(�From�,�To�,�Copies�,�Preview�,�ActivePrinter�,�PrintToFile�,�Collate�,�PrToFileName�,�IgnorePrintAreas�)
From
The starting page number to begin the printing. If this argument is omitted, printing starts at the beginning.
A number that indicates the last page to be print. If not specified, the printing ends with the last page.
Copies
An Integer that represents the number of copies to be printed.
Default value is 1.
Preview
It takes binary values TRUE or FALSE.
A TRUE invokes the print preview dialog box before printing.
A FALSE simply prints without showing the preview dialog.
ActivePrinter
This parameter specifies or sets the name of the active printer to print with.
PrintToFile
This parameter takes a binary value TRUE or FALSE.
A TRUE is given to enable Print to File.
PrToFileName
When PrintToFile is set to TRUE, this argument specifies the name of the file to print to.
Collate
This Property takes a binary value.
A True to collate multiple copies and False for otherwise.
IgnorePrintAreas
A TRUE will ignore the print area selected and prints the entire object.
The following example demonstrates the print feature.
- Sub printsheet()
- Dim prtsht As Worksheet
- Set prtsht = Sheets("Q97")
- prtsht.PrintOut From:=97, To:=98, Copies:=1, Preview:=True
- End Sub
The above code is used to print this sheet.
The screenshot of the editor is as shown below