C#查找和替换Word文档中的文本的替代方法

时间:2017-06-06 13:35:55

标签: c# .net ms-word interop

我正在寻找其他方法来搜索单词文档中的某些文本,然后将其替换为另一个文本。我当前的工作使用查找和替换方法,但我想知道是否有其他方法来做到这一点。

我尝试过的一种方法是逐段检查并搜索文本,将其换出,然后将其粘贴到新的word文档中并保存。然而,当涉及图像,教科书,表格等时,这使事情变得更加复杂。此外格式化也没有被保留,因此这是另一个问题。

我目前的代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;

namespace Test
{
    static class Program
    {
        static void Main()
        {
            //Create a new microsoft word file
            Microsoft.Office.Interop.Word.Application fileOpen = new Microsoft.Office.Interop.Word.Application();
            //Open a already existing word file into the new document created
            Microsoft.Office.Interop.Word.Document document = fileOpen.Documents.Open(@"C:\Users\dpatel\Desktop\Test1.docx", ReadOnly: false);
            //Make the file visible 
            fileOpen.Visible = true;
            document.Activate();
            //The FindAndReplace takes the text to find under any formatting and replaces it with the
            //new text with the same exact formmating (e.g red bold text will be replaced with red bold text)
            FindAndReplace(fileOpen, "useless", "very useful");
            //Save the editted file in a specified location
            //Can use SaveAs instead of SaveAs2 and just give it a name to have it saved by default
            //to the documents folder
            document.SaveAs2(@"C:\Users\dpatel\Desktop\NewFile1");
            //Close the file out
            fileOpen.Quit();
        }
        //Method to find and replace the text in the word document. Replaces all instances of it
        static void FindAndReplace(Microsoft.Office.Interop.Word.Application fileOpen, object findText, object replaceWithText)
        {
            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;
            //execute find and replace
            fileOpen.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord,
                ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref replaceWithText, ref replace,
                ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl);
        }
    }
}

3 个答案:

答案 0 :(得分:2)

事实证明,除了查找和替换文本之外别无选择。唯一的方法是扫描文档,提取文本,如果匹配,则发出替换。这与“查找和替换”基本相同。但是该方法有一些变化,例如选择仅搜索文本框,仅搜索表格等。

答案 1 :(得分:1)

你可以像我一样做。很抱歉可能编码错误,但效果很好

    printf("%d ", x);

答案 2 :(得分:1)

您可以尝试使用GroupDocs.Redaction for .NET来查找和替换Word文档中所需的单词或短语。

extbuildconf --add-modules-dir=ext_folder
configure --enable-cli --enable-pdo --enable-pdo_my_mysql
nmake 

披露:我是GroupDocs的开发人员布道者。