Edit

Share via


Programmatically print documents

You can print an entire Microsoft Office Word document, or part of a document, to your default printer.

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

To print the entire document

  1. Call the PrintOut method of the ThisDocument class in your project to print the entire document. To use this example, run the code from the ThisDocument class.

    object copies = "1";
    object pages = "";
    object range = Word.WdPrintOutRange.wdPrintAllDocument;
    object items = Word.WdPrintOutItem.wdPrintDocumentContent;
    object pageType = Word.WdPrintOutPages.wdPrintAllPages;
    object oTrue = true;
    object oFalse = false;
    
    this.PrintOut(ref oTrue, ref oFalse, ref range, ref missing, ref missing, ref missing,
        ref items, ref copies, ref pages, ref pageType, ref oFalse, ref oTrue, 
        ref missing, ref oFalse, ref missing, ref missing, ref missing, ref missing);
    

To print the current page of the document

  1. Call the PrintOut method of the ThisDocument class in your project and specify that one copy of the current page be printed. To use this example, run the code from the ThisDocument class.

    object copies = "1"; 
    object pages = "1"; 
    object range = Word.WdPrintOutRange.wdPrintCurrentPage; 
    object items = Word.WdPrintOutItem.wdPrintDocumentContent; 
    object pageType = Word.WdPrintOutPages.wdPrintAllPages; 
    object oTrue = true; 
    object oFalse = false; 
    
    this.PrintOut(
        ref oTrue, ref oFalse, ref range, ref missing, ref missing, ref missing,
        ref items, ref copies, ref pages, ref pageType, ref oFalse, ref oTrue, 
        ref missing, ref oFalse, ref missing, ref missing, ref missing, ref missing);
    

To print an entire document

  1. Call the PrintOut method of the Document object that you want to print. The following code example prints the active document. To use this example, run the code from the ThisAddIn class in your project.

    this.Application.ActiveDocument.PrintOut(true, false, Word.WdPrintOutRange.wdPrintAllDocument,
        Item: Word.WdPrintOutItem.wdPrintDocumentContent, Copies:"1", Pages:"", 
        PageType:Word.WdPrintOutPages.wdPrintAllPages, PrintToFile:false, Collate:true,
        ManualDuplexPrint:false);
    

To print the current page of a document

  1. Call the PrintOut method of the Document object that you want to print, and specify that one copy of the current page be printed. The following code example prints the active document. To use this example, run the code from the ThisAddIn class in your project.

    object copies = "1";
    object pages = "1";
    object range = Word.WdPrintOutRange.wdPrintCurrentPage;
    object items = Word.WdPrintOutItem.wdPrintDocumentContent;
    object pageType = Word.WdPrintOutPages.wdPrintAllPages;
    object oTrue = true;
    object oFalse = false;
    Word.Document document = this.Application.ActiveDocument;
    
    document.PrintOut(
        ref oTrue, ref oFalse, ref range, ref missing, ref missing, ref missing,
        ref items, ref copies, ref pages, ref pageType, ref oFalse, ref oTrue,
        ref missing, ref oFalse, ref missing, ref missing, ref missing, ref missing);