65.9K
CodeProject 正在变化。 阅读更多。
Home

MS Word 文档命令行搜索工具

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2021 年 3 月 4 日

CPOL

3分钟阅读

viewsIcon

4665

downloadIcon

232

使用 Microsoft.Office.Interop.Word 进行关键字搜索的 .NET 控制台应用程序

背景

多年来,我一直在 MS Word 文档中保存一些小的随机笔记。新的笔记或主题以一条以连字符开头的行引入,并且每季度或半年创建一个新文档。此 NoteSearch 实用程序提供了一种方便的方法来搜索多个 Word 文档(例如我多年来收集的笔记),以查找包含用户指定关键字的条目。此外,还包含了一些探索性命令,以帮助理解 Microsoft.Office.Interop.Word 库如何将文档划分为段落和单词。

引言

鉴于需要搜索多个 Word 文档,使用 Microsoft.Office.Interop.Word 库似乎是一个显而易见的选择。 Microsoft 提供的文档非常简短,因此首先编写了探索性命令,以便基本熟悉使用该库来打开和关闭文档以及检查段落和单词。搜索命令更进一步,使用该库搜索指定关键字的存在或不存在。此程序不利用该库修改文档的能力。

文档 - 示例 1

最初的示例基于包含美国宪法的文档。该文本是从费城国家宪法中心的网站下载的。在第一个代码片段中,我们使用“af”(添加文件)命令来选择 USConstitution 文档,并将搜索设置为包含单词“defence”并排除单词“jury”(用于包含和排除的 ix 命令)。然后,笔记搜索(nn 命令)检查文档中是否包含符合这些规则的段落。 命令和响应是

af usconstitution.docx
ns
i defence
x jury
nn

Paragraphs 1 - 1 from C:\...\data\usconstitution.docx
We the People of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility, provide for the common defence, ... do ordain and establish this Constitution for the United States of America.
ready
Press [return]/q for next/quit
[RETURN]


Paragraphs 35 - 35 from C:\...\data\usconstitution.docx
The Congress shall have Power To lay and collect Taxes, ... and provide for the common Defence and general Welfare of the United States; but all Duties, Imposts and Excises shall be uniform throughout the United States;
ready
Press [return]/q for next/quit
[RETURN]


End of notes.
请注意,这些代码片段可能会省略某些细节。该文档提供了这些示例的所有命令的完整列表。

文档 - 示例 2

在前面的示例中,我们没有将文档划分为子部分,因此它执行了逐段搜索。我们可以使用更大的文档划分进行搜索,我们使用术语notes来指定这些子部分。制定者将宪法分为若干节,因此单词 SECTION 为该文档提供了一个方便的分隔符。在此示例中,我们使用 ns(注释开始)命令选择单词 SECTION 作为划分的前缀,然后执行相同的搜索。

af usconstitution.docx
i defence 
x jury
ns section
nn

Paragraphs 1 - 2 from C:\Projects\NoteSearch\data\usconstitution.docx
We the People of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility, provide for the common defence, promote the general Welfare, and secure the Blessings of Liberty to ourselves and our Posterity, do ordain and establish this Constitution for the United States of America.
Article 1
ready
Press [return]/q for next/quit


RETURN
Paragraphs 34 - 52 from C:\Projects\NoteSearch\data\usconstitution.docx
Section 8: Powers of Congress
The Congress shall have Power To lay and collect Taxes, Duties, Imposts and Excises, to pay the Debts and provide for the common Defence and general Welfare of the United States; but all Duties, Imposts and Excises shall be uniform throughout the United States;
To borrow Money on the credit of the United States;
[listing of enumerated powers]
To make all Laws which shall be necessary and proper for carrying into Execution the foregoing Powers...
ready
Press [return]/q for next/quit


RETURN
End of notes.
ready

这里的区别在于搜索是按节而不是按段落进行的,因此宪法的整个部分(在本例中,是第 34 到 52 段)都会被写入。

文档 - 示例 3

编写此程序的主要目的是在我自己的日记文档中找到笔记。您可以在以下摘录中看到这些文档的结构

Monday, February 9, 2015
- Fell in driveway today. Bumped head on concrete stairs. Ouch!
- I had a car problem with hitting a couple of potholes...
I didn't notice anything with the car but it seemed to have a drag on it when I tried to drive on Sunday...
Finally gave up and called AAA...
I called dealer at 7:30 on Monday and they had the tire in stock ....
I also told them they could align the wheels so the total bill was about $525.
- Installed the cover on the side basement window. I should have cut openings for the phone wires and it would have fit better. We'll see whether this does any good.

基本格式是存在日期行和关于随机主题的各个笔记。笔记可以包含多个段落,并且每个笔记都以一个以连字符开头的行开始。在下面的代码片段中,我想使用单个命令搜索两个文档,使用一个文本文件(其中列出了两个 Word 文件的文件名)来搜索包含单词“car”的笔记条目。

af notes-q1-2015.docx
ready
af notes-q2-2015.docx
ready
l
  0 - C:\...\notes-q1-2015.docx
  1 - C:\...\notes-q2-2015.docx
i car
ready
nn


Monday, February 09, 2015 Paragraphs 20 - 23 from C:\...\data\notes-q1-2015.docx
-  I had a car problem with hitting a couple of potholes...
   I didn't notice anything with the car but it seemed to have a drag on it when I tried to drive on Sunday... 
   Finally gave up and called AAA...
   I called dealer at 7:30 on Monday and they had the tire in stock ....
   I also told them they could align the wheels so the total bill was about $525. 


ready
Press [return]/q for next/quit
[RETURN]


Monday, June 08, 2015 Paragraphs 33 - 35 from C:\Projects\NoteSearch\data\notes-q2-2015.docx

- Last week I was driving car to PM when the oil low light came on.  It said it was okay to continue driving but I could add a quart of oil.
    I drove to the dealer.  ...
[etc]   

搜索可以继续搜索多个文档,并将显示每个包含字符串“car”的笔记。

结论和关注点

该程序提供了一个非常简单的 Interop.Word 库编程入门。它也是一个易于使用的程序,用于在一系列 MS Word 文档中搜索关键字。对于此应用程序,它可以检查多个文档并且可以写入整个段落或部分,这一事实使其成为一种非常通用的搜索工具。添加其他命令和搜索类型可以将该程序变成一个通用的文档搜索工具。

历史

  • 2021 年 3 月 4 日:初始版本
© . All rights reserved.