[使用C#以编程方式在Word文档中搜索文本短语,但没有Word Interop

问题描述 投票:0回答:1

我正在通过C#寻找一种方法,以编程方式在给定文件夹中搜索包含特定文本短语的Word文档。该程序需要能够在不使用Word Interop的情况下运行,因为它可能正在服务器上运行。

我试图搜索解决方案,但是我发现的任何东西都非常古老,并且基于过时的库和应用程序版本。

c# .net word text-search
1个回答
0
投票

尝试此

protected bool FindTextInWordDocument(object text, string flname)
{
    object matchCase = false;
    object matchWholeWord = true;
    object matchWildCards = false;
    object matchSoundsLike = false;
    object matchAllWordForms = false;
    object forward = true;
    object format = false;
    object matchKashida = false;
    object matchDiacritics = false;
    object matchAlefHamza = false;
    object matchControl = false;
    object read_only = false;
    object visible = true;
    object replace = 2;
    object wrap = 1;

    Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
    Microsoft.Office.Interop.Word.Document docOpen = app.Documents.Open(flname);
    bool val = false;
    try
    {
        val = app.Selection.Find.Execute(ref text, ref matchCase, ref matchWholeWord,
        ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap,
        ref format, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    }
    finally
    {
        app.Documents.Close();
    }
    return val;
}

将此方法称为

FindTextInWord((object)"Word you want to search","File name you want to search");
© www.soinside.com 2019 - 2024. All rights reserved.