惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 【当耐特】
Help Net Security
Help Net Security
P
Proofpoint News Feed
J
Java Code Geeks
爱范儿
爱范儿
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Full Disclosure
Google DeepMind News
Google DeepMind News
H
Help Net Security
G
Google Developers Blog
Jina AI
Jina AI
Vercel News
Vercel News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
Lohrmann on Cybersecurity
S
Schneier on Security
Microsoft Azure Blog
Microsoft Azure Blog
IT之家
IT之家
Security Archives - TechRepublic
Security Archives - TechRepublic
阮一峰的网络日志
阮一峰的网络日志
N
News and Events Feed by Topic
GbyAI
GbyAI
B
Blog
O
OpenAI News
博客园_首页
Cisco Talos Blog
Cisco Talos Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Hacker News: Ask HN
Hacker News: Ask HN
TaoSecurity Blog
TaoSecurity Blog
腾讯CDC
MongoDB | Blog
MongoDB | Blog
M
MIT News - Artificial intelligence
C
Cybersecurity and Infrastructure Security Agency CISA
Cyberwarzone
Cyberwarzone
Webroot Blog
Webroot Blog
Simon Willison's Weblog
Simon Willison's Weblog
Y
Y Combinator Blog
C
Cisco Blogs
A
Arctic Wolf
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
The Exploit Database - CXSecurity.com
Security Latest
Security Latest
AI
AI
W
WeLiveSecurity
aimingoo的专栏
aimingoo的专栏
The Register - Security
The Register - Security
Project Zero
Project Zero
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
Netflix TechBlog - Medium
Blog — PlanetScale
Blog — PlanetScale

博客园 - hone

管理 java学习 服务器相关 总统和亿万富翁经典语录(转载) js学习笔记 最近学习计划 VC控制Excel 真乱(转自csdn) 沟通(网上搜集) 工作 无题 经典68个故事 Manually Invalidating All Stale Sessions java的zip压缩(转自Jeet) 图形相关 在Oracle9i中计算时间差 六度分隔 经典故事 新年竟然第一篇?!
将文档发送到打印机(From MSN)
hone · 2005-12-29 · via 博客园 - hone

可以使用 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(); 
    }