如何使用 .NET C# 在 MS Word 中使用高级查找

How to use advanced find in MS Word with .NET C#

我有这样的问题:我需要使用高级查找来查找特定的文本(基于文本的字体、文本字符串、通配符...),然后写到记事本文件中我在哪一页找到了那个文字

我在 C# 中看到 .Net 有这个 Find.Execute method,但我不知道是否可以这样做,我用谷歌搜索但没有希望。

但是我的想法是这样的代码

using Microsoft.Office.Core;
using Word = Microsoft.Office.Interop.Word;
using System.Reflection;

...

Word.Application oWord;
Word._Document oDoc;

oWord = new Word.Application();
oWord.Visible = false;

oDoc = oWord.Documents.Open(strPath, ReadOnly: true);

Word.Range findRange;
Word.Range resultRange;
int nPage;

//Get the range of the whole word document
int nEnd;
nEnd = oDoc.Paragraphs.Last.Range.Sentences.First.End;          
findRange = oDoc.Range(0, nEnd);

//Setup find condition
//The color of the found text: RGB(243,99, 195) . Please help!

//Execute find --> Loop until not found anymore
{
//findRange.Find.Execute... Please help!

//Get the range of the found text
//resultRange = ... Please help!

//Get page of the result range
nPage = resultRange.get_Information(Word.WdInformation.wdActiveEndPageNumber);

//Do anything you like with nPage
}
//Close the process
oDoc.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
((Word._Application)oWord).Quit(Word.WdSaveOptions.wdDoNotSaveChanges);

提前谢谢你。

感谢上帝,我找到了解决方案。 看完后:

  • this article 了解如何循环查找下一个特征。

  • This article 找出必须使用 Find.Font.Color 而不是 Find.Font.TextColor.RGB

  • 获取页面范围(代码很不干净,但可用)

好的,开始了

    Word.Application oWord;
    Word._Document oDoc;

    oWord = new Word.Application();
    oWord.Visible = false;

    oDoc = oWord.Documents.Open(strWorkingPath, ReadOnly: true);
    //===================Excute===================
    /*Word 2013*/
    oWord.ActiveWindow.View.ReadingLayout = false;

    // Get pages count
    Word.WdStatistic PagesCountStat = Word.WdStatistic.wdStatisticPages;
    int nTotalPage = oDoc.ComputeStatistics(PagesCountStat);

    int nEndOfTheDoc = oDoc.Paragraphs.Last.Range.Sentences.First.End;

    int nStart = 0;
    int nEnd = nEndOfTheDoc;

    List<int> lstPage = new List<int>();
    int color = 696969;//The color you can get by read the Font.Color of the Range in Debug view

    Word.Range findRange;

    object What = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
    object Which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToAbsolute;
    object nCrtPage;
    object nNextPage;

    bool bPageIsIn = false;

    /*Loop the pages*/
    for (int i = 1; i <= nTotalPage; i++)
    {
        /*Get the start and end position of the current page*/
        nCrtPage = i;
        nNextPage = i + 1;
        nStart = oWord.Selection.GoTo(ref What,
           ref Which, ref nCrtPage).Start;
        nEnd = oWord.Selection.GoTo(ref What,
           ref Which, ref nNextPage).End;

        /*The last page: nStart will equal nEnd*/
        if(nStart == nEnd)
        {
            /*Set nEnd for the last page*/
            nEnd = nEndOfTheDoc;
        }

        /*Set default for Count page trigger*/
        bPageIsIn = false;
        /*Set the find range is the current page range*/
        findRange = oDoc.Range(nStart, nEnd);

        /*Set up find condition*/
        findRange.Find.Font.Color = (Word.WdColor)color;
        findRange.Find.Format = true;
        findRange.Find.Text = "^?";

        do
        {
            /*Loop find next*/
            findRange.Find.Execute();

            /*If found*/
            if (findRange.Find.Found)
            {
                /*If found data is still in the page*/
                if (findRange.End <= nEnd)
                {
                    /*If found data is visible by human eyes*/
                    if (!string.IsNullOrWhiteSpace(findRange.Text))
                    {
                        /*Ok, count this page*/
                        bPageIsIn = true;
                        break;/*no need to find anymore for this page*/
                    }
                }
            }
            else
                break;/*no need to find anymore for this page*/
        }while (findRange.End < nEnd);/*Make sure it is in that page only*/

        if (bPageIsIn)
            lstPage.Add(i);
    }

    //===================Close===================
    oDoc.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
    ((Word._Application)oWord).Quit(Word.WdSaveOptions.wdDoNotSaveChanges);

    foreach (var item in lstPage)
    {
        builder.AppendLine(item.ToString());//Do anything you like with the list page
    }