如何:打印文档

更新:2007 年 11 月

适用对象

本主题中的信息仅适用于指定的 Visual Studio Tools for Office 项目和 Microsoft Office 版本。

项目类型

  • 文档级项目

  • 应用程序级项目

Microsoft Office 版本

  • Word 2003

  • Word 2007

有关更多信息,请参见按应用程序和项目类型提供的功能

您可以打印整个 Microsoft Office Word 文档,也可以打印文档的一部分。

打印属于文档级自定义项的文档

打印整个文档

  • 调用 Microsoft.Office.Tools.Word.Document 类的 PrintOut 方法以打印整个文档。若要使用此示例,请从项目内的 ThisDocument 类中运行代码。

    Me.PrintOut()
    
    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);
    

打印文档的当前页面

  • 调用 Microsoft.Office.Tools.Word.Document 类的 PrintOut 方法并指定打印一份当前页面。若要使用此示例,请从项目内的 ThisDocument 类中运行代码。

    Me.PrintOut( _
        Background:=True, _
        Append:=False, _
        Range:=Word.WdPrintOutRange.wdPrintCurrentPage, _
        Item:=Word.WdPrintOutItem.wdPrintDocumentContent, _
        Copies:="1", _
        Pages:="1", _
        PageType:=Word.WdPrintOutPages.wdPrintAllPages, _
        PrintToFile:=False, _
        Collate:=True, _
        ManualDuplexPrint:=False)
    
    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);
    

使用应用程序级外接程序打印文档

打印整个文档

  • 调用想要打印的 Microsoft.Office.Interop.Word.Document 对象的 PrintOut 方法。下面的代码示例将打印活动文档。若要使用此示例,请从项目内的 ThisAddIn 类中运行代码。

    Me.Application.ActiveDocument.PrintOut()
    
    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.Application.ActiveDocument.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);
    

打印文档的当前页面

  • 调用想要打印的 Microsoft.Office.Interop.Word.Document 对象的 PrintOut 方法,并指定打印一份当前页面。下面的代码示例将打印活动文档。若要使用此示例,请从项目内的 ThisAddIn 类中运行代码。

    Me.Application.ActiveDocument.PrintOut( _
        Background:=True, _
        Append:=False, _
        Range:=Word.WdPrintOutRange.wdPrintCurrentPage, _
        Item:=Word.WdPrintOutItem.wdPrintDocumentContent, _
        Copies:="1", _
        Pages:="1", _
        PageType:=Word.WdPrintOutPages.wdPrintAllPages, _
        PrintToFile:=False, _
        Collate:=True, _
        ManualDuplexPrint:=False)
    
    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);
    

请参见

任务

如何:在打印预览中显示文档

概念

了解 Office 解决方案中的可选参数