Share via


How to: Programmatically print worksheets

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

You can print any worksheet in a workbook.

Applies to: The information in this topic applies to document-level projects and VSTO Add-in projects for Excel. For more information, see Features available by Office application and project type.

To print a worksheet

  1. Call the PrintOut method of Sheet1, request two copies, and preview the document before printing.

    Globals.Sheet1.PrintOut(1, 1, 2, true);
    
    Globals.Sheet1.PrintOut(From:=1, To:=1, Copies:=2, Preview:=True)
    

    The PrintPreview method enables you to display the specified object in the Print Preview window. The following code assumes you have a Worksheet host item named Sheet1.

To preview a page before printing

  1. Call the PrintPreview method of the worksheet.

    Globals.Sheet1.PrintPreview();
    
    Globals.Sheet1.PrintPreview()
    

To print a worksheet

  1. Call the PrintOut method of the active worksheet, request two copies, and preview the document before printing.

    ((Excel.Worksheet)Application.ActiveSheet).PrintOut
        (1, 1, 2, true);
    
    CType(Application.ActiveSheet, Excel.Worksheet).PrintOut _
       (From:=1, To:=1, Copies:=2, Preview:=True)
    

    The PrintPreview method enables you to display the specified object in the Print Preview window.

To preview a page before printing

  1. Call the PrintPreview method of the active worksheet.

    ((Excel.Worksheet)Application.ActiveSheet).PrintPreview();
    
    CType(Application.ActiveSheet, Excel.Worksheet).PrintPreview()
    

See also