现在的位置: 首页 > 综合 > 正文

如何:将文档发送到打印机(转)

2012年08月27日 ⁄ 综合 ⁄ 共 4633字 ⁄ 字号 评论关闭

文档出自:http://www.cnblogs.com/hone/archive/2005/12/29/307302.html

可以使用 PrintOut 方法将 Microsoft Office Word 文档(或文档的一部分)发送到打印机。可以从 ApplicationDocument 对象调用 PrintOut()

打印文档

  • 下面的代码用所有默认选项打印活动文档:

    ThisDocument.PrintOut()

PrintOut 方法有多个可选参数,允许您微调打印文档的方式,下表概括了这些参数。

参数 说明
Background 设置为 True 可允许在 Word 打印文档时继续处理。
Append OutputFileName 参数一起使用。设置为 True 可将指定的文档名称追加到由 OutputFileName 参数指定的文件名后。设置为 False 将重写 OutputFileName 的内容。
Range 页面范围。可以为任何 WdPrintOutRange 枚举:wdPrintAllDocumentwdPrintCurrentPagewdPrintFromTowdPrintRangeOfPageswdPrintSelection
OutputFileName 如果 PrintToFile 为 True,此参数指定输出文件的路径和文件名。
From Range 设置为 wdPrintFromTo 时的起始页码。
To Range 设置为 wdPrintFromTo 时的结束页码。
Item 要打印的项。可以是任何 WdPrintOutItem 枚举:wdPrintAutoTextEntrieswdPrintCommentswdPrintDocumentContentwdPrintKeyAssignmentswdPrintPropertieswdPrintStyles
Copies 要打印的份数。
Pages 要打印的页码和页码范围,由逗号分隔。例如,“2, 6-10”意为打印第 2 页和第 6、7、8、9、10 页。
PageType 要打印的页面的类型。可以是任何 WdPrintOutPages 常量:wdPrintAllPageswdPrintEvenPagesOnlywdPrintOddPagesOnly
PrintToFile 设置为 True 可将打印机指令发送到文档。确保使用 OutputFileName 指定一个文件名。
Collate 打印一个文档的多个副本时使用此参数。设置为 True 则在打印下一个副本前将打印此文档的所有页面。
FileName 仅适用于 Application 对象。要打印的文档的路径和文件名。如果不用此参数,Word 将打印活动文档。
ManualDuplexPrint 设置为 True 可在打印机上没有双面打印装置的情况下打印双面文档。

常用的 PrintOut 参数。

打印活动文档的第一页

  • 下面的过程打印活动文档的第一页:

    ' Visual Basic
        Friend Sub PrintOutDoc()
        ThisDocument.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)
        End Sub
        // C#
        internal void PrintOutDoc()
        {
        object myTrue = true;
        object myFalse = false;
        object missingValue = Type.Missing;
        object range = Word.WdPrintOutRange.wdPrintCurrentPage;
        object items = Word.WdPrintOutItem.wdPrintDocumentContent;
        object copies = "1";
        object pages = "1";
        object pageType = Word.WdPrintOutPages.wdPrintAllPages;
        ThisDocument.PrintOut(ref myTrue, ref myFalse, ref range,
        ref missingValue, ref missingValue, ref missingValue,
        ref items, ref copies, ref pages, ref pageType, ref myFalse,
        ref myTrue, ref missingValue,  ref myFalse, ref missingValue,
        ref missingValue, ref missingValue, ref missingValue);
        }

    如何:在 Word 文档中搜索文本

    Find 对象既是 Selection 的成员,又是 Range 对象的成员,您可以使用这两者中的任意一个来搜索文本。“替换”命令是“查找”命令的扩展。

    使用 Selection 对象查找文本时,指定的所有搜索条件只适用于当前选择的文本。如果 Selection 是插入点,则搜索整个文档。如果找到匹配搜索条件的项,选择将自动更改以突出显示找到的项。

    使用 Selection 对象查找文本

    • 下面的过程搜索字符串“find me”。找到第一个后,它突出显示该词并显示一个消息框。
      ' Visual Basic
              Friend Sub SelectionFind()
              Dim strFind As String = "find me"
              With ThisApplication.Selection.Find
              .ClearFormatting()
              .Text = strFind
              If .Execute = True Then
              MessageBox.Show("Text found.")
              Else
              MessageBox.Show("The text could not be located.")
              End If
              End With
              End Sub
              // C#
              internal void SelectionFind()
              {
              string strFind = "find me";
              Word.Find fnd = ThisApplication.Selection.Find;
              fnd.ClearFormatting();
              fnd.Text = strFind;
              object missingValue = Type.Missing;
              if (fnd.Execute(ref missingValue, ref missingValue,
              ref missingValue, ref missingValue, ref missingValue,
              ref missingValue, ref missingValue, ref missingValue,
              ref missingValue, ref missingValue, ref missingValue,
              ref missingValue, ref missingValue, ref missingValue,
              ref missingValue))
              {
              MessageBox.Show("Text found.");
              }
              else
              {
              MessageBox.Show("The text could not be located.");
              }
              }

    需要注意的是,Find 条件是累积的,这意味着一个条件会与前面的搜索条件累加起来。在搜索前可使用 .ClearFormatting() 方法清除上一次搜索的格式设置。

    使用 Range 对象查找文本可以使您在用户界面不显示任何内容的情况下搜索文本。如果找到匹配搜索条件的文本,Find 方法将返回 True,否则返回 False。如果找到了文本,该方法还会重新定义 Range 对象以匹配搜索条件。

    使用 Range 对象查找文本

    1. 定义一个由文档的第二段组成的 Range 对象。

      ' Visual Basic
              Dim rng As Word.Range = ThisDocument.Paragraphs(2).Range
              // C#
              Word.Range rng = ThisDocument.Paragraphs[2].Range;
    2. 使用 Find 方法,首先清除任何现有的格式设置选项,然后搜索字符串“find me”。
      ' Visual Basic
              With rng.Find
              .ClearFormatting()
              If .Execute(FindText:="find me") Then
              // C#
              Word.Find fnd = rng.Find;
              fnd.ClearFormatting();
              object missingValue = Type.Missing;
              object findStr = "find me";
              if (fnd.Execute(
    3. 在消息框中显示搜索结果,最后选择 Range 以使其可见。
      ' Visual Basic
              MessageBox.Show("Text found.")
              Else
              MessageBox.Show("Text not found.")
              End If
              End With
              rng.Select()
              // C#
              {
              MessageBox.Show("Text found.");
              }
              else
              {
              MessageBox.Show("Text not found.");
              }
              rng.Select();

      如果搜索失败,则第二段被选中;如果搜索成功,则显示搜索条件。

    完整的方法如下所示:

    ' Visual Basic
        Friend Sub RangeFind()
        Dim rng As Word.Range = _
        ThisDocument.Paragraphs(2).Range
        With rng.Find
        .ClearFormatting()
        If .Execute(FindText:="find me") Then
        MessageBox.Show("Text found.")
        Else
        MessageBox.Show("Text not found.")
        End If
        End With
        rng.Select()
        End Sub
        // C#
        internal void RangeFind()
        {
        Word.Range rng = ThisDocument.Paragraphs[2].Range;
        Word.Find fnd = rng.Find;
        fnd.ClearFormatting();
        object missingValue = Type.Missing;
        object findStr = "find me";
        if (fnd.Execute(ref findStr, ref missingValue, ref missingValue,
        ref missingValue, ref missingValue, ref missingValue,
        ref missingValue, ref missingValue, ref missingValue,
        ref missingValue, ref missingValue, ref missingValue,
        ref missingValue, ref missingValue, ref missingValue))
        {
        MessageBox.Show("Text found.");
        }
        else
        {
        MessageBox.Show("Text not found.");
        }
        rng.Select();
        }

【上篇】
【下篇】

抱歉!评论已关闭.