Printing using VBA.

To display print preview window with fit to page:

With Worksheets("dataImport").PageSetup
	.Zoom = False
	.FitToPagesTall = 1
	.FitToPagesWide = 1
End With
Worksheets("Sheet Name").PrintPreview

Print preview above will show old style preview window. To open File>Print window use code below:

Worksheet.Activate
Application.CommandBars.ExecuteMso ("PrintPreviewAndPrint")

Possible Options:

.Orientation = xlLandscape
.CenterHeader = 
.RightFooter =
.LeftFooter = 
.CenterFooter =
.Orientation = xlPortrait
.topMargin = Application.CentimetersToPoints(value)
.leftMargin = Application.CentimetersToPoints(value)
.rightMargin = Application.CentimetersToPoints(value)
.bottomMargin = Application.CentimetersToPoints(value)
.headerMargin = Application.CentimetersToPoints(value)
.footerMargin = Application.CentimetersToPoints(value)
.paperSize = xlPaperA4

Paper Size table:

NameDescription
xlPaperLetterLetter (8-1/2 in. x 11 in.)
xlPaperLetterSmallLetter Small (8-1/2 in. x 11 in.)
xlPaperTabloidTabloid (11 in. x 17 in.)
xlPaperLedgerLedger (17 in. x 11 in.)
xlPaperLegalLegal (8-1/2 in. x 14 in.)
xlPaperStatementStatement (5-1/2 in. x 8-1/2 in.)
xlPaperExecutiveExecutive (7-1/2 in. x 10-1/2 in.)
xlPaperA3A3 (297 mm x 420 mm)
xlPaperA4A4 (210 mm x 297 mm)
xlPaperA4SmallA4 Small (210 mm x 297 mm)
xlPaperA5A5 (148 mm x 210 mm)
xlPaperB4B4 (250 mm x 354 mm)
xlPaperB5A5 (148 mm x 210 mm)
xlPaperFolioFolio (8-1/2 in. x 13 in.)
xlPaperQuartoQuarto (215 mm x 275 mm)
xlPaper10x1410 in. x 14 in.
xlPaper11x1711 in. x 17 in.
xlPaperNoteNote (8-1/2 in. x 11 in.)
xlPaperEnvelope9Envelope #9 (3-7/8 in. x 8-7/8 in.)
xlPaperEnvelope10Envelope #10 (4-1/8 in. x 9-1/2 in.)
xlPaperEnvelope11Envelope #11 (4-1/2 in. x 10-3/8 in.)
xlPaperEnvelope12Envelope #12 (4-1/2 in. x 11 in.)
xlPaperEnvelope14Envelope #14 (5 in. x 11-1/2 in.)
xlPaperCsheetC size sheet
xlPaperDsheetD size sheet
xlPaperEsheetE size sheet
xlPaperEnvelopeDLEnvelope DL (110 mm x 220 mm)
xlPaperEnvelopeC5Envelope C5 (162 mm x 229 mm)
xlPaperEnvelopeC3Envelope C3 (324 mm x 458 mm)
xlPaperEnvelopeC4Envelope C4 (229 mm x 324 mm)
xlPaperEnvelopeC6Envelope C6 (114 mm x 162 mm)
xlPaperEnvelopeC65Envelope C65 (114 mm x 229 mm)
xlPaperEnvelopeB4Envelope B4 (250 mm x 353 mm)
xlPaperEnvelopeB5Envelope B5 (176 mm x 250 mm)
xlPaperEnvelopeB6Envelope B6 (176 mm x 125 mm)
xlPaperEnvelopeItalyEnvelope (110 mm x 230 mm)
xlPaperEnvelopeMonarchEnvelope Monarch (3-7/8 in. x 7-1/2 in.)
xlPaperEnvelopePersonalEnvelope (3-5/8 in. x 6-1/2 in.)
xlPaperFanfoldUSU.S. Standard Fanfold (14-7/8 in. x 11 in.)
xlPaperFanfoldStdGermanGerman Legal Fanfold (8-1/2 in. x 13 in.)
xlPaperFanfoldLegalGermanGerman Legal Fanfold (8-1/2 in. x 13 in.)
xlPaperUserUser-defined

Categorized in:

VBA Excel,

Last Update: May 18, 2024