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

6

我正在寻找其他搜索并替换文档中特定文本的方法。目前我的方法是使用“查找和替换”功能,但我想知道是否有其他方法。

我尝试过的一种方法是逐段查找文本,将其替换并粘贴到一个新的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);
        }
    }
}

你为什么要寻找替代内置方法的方案? - stuartd
这是我正在编写的程序。我只想知道可以使用哪些不同的选择。内置的查找和替换方法可以工作,但对于我的情况并不是最佳解决方案。 - Dylan
4个回答

5
您可以像我一样做。可能代码不太好,但效果很棒。
using Microsoft.Office.Interop.Word;

 var application = new Word.Application();
        string path = Path.GetDirectoryName(Path.GetFullPath("YourDocsName.doc"));
        object path_YourDocsName = path + @"\folder\YourDocsName.doc";

        object o = Missing.Value;
        object oFalse = false;
        object oTrue = true;

        Word._Application app = null;
        Word.Documents docs = null;
        Word.Document doc = null;

        try
        {
            app = new Word.Application();
            app.Visible = false;
            app.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;

            docs = app.Documents;
            doc = docs.Open(ref path_YourDocsName, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o);
            doc.Activate();           
        
            foreach (Word.Range range in doc.StoryRanges)
            {
                
                Word.Find find = range.Find;
                object findText = "[Todays date]";
                //  object findText = { "[Todays date]","[]" };
                object replacText = todaysdate;   //gets todays date and time to doc
                object replace = Word.WdReplace.wdReplaceAll;
                object findWrap = Word.WdFindWrap.wdFindContinue;
                find.Execute(ref replaceThis, ref o, ref o, ref o, ref oFalse, ref o,
                    ref o, ref findWrap, ref o, ref replaceThisWith,
                    ref replace, ref o, ref o, ref o, ref o);

                Word.Find find1 = range.Find;
                object findText1 = "[doc content]";
                object replacText1 = Name;
                object replace1 = Word.WdReplace.wdReplaceAll;
                object findWrap1 = Word.WdFindWrap.wdFindContinue;
                find.Execute(ref findText1, ref o, ref o, ref o, ref oFalse, ref o,
                    ref o, ref findWrap1, ref o, ref replacText1,
                    ref replace1, ref o, ref o, ref o, ref o);

                Word.Find find2 = range.Find;
                object findText2 = "[doc content]";
                object replacText2 = Somestringyouneed;
                object replace2 = Word.WdReplace.wdReplaceAll;
                object findWrap2 = Word.WdFindWrap.wdFindContinue;
                find.Execute(ref findText2, ref o, ref o, ref o, ref oFalse, ref o,
                    ref o, ref findWrap2, ref o, ref replacText2,
                    ref replace2, ref o, ref o, ref o, ref o);

                Word.Find find3 = range.Find;
                object findText3 = "[Doc content]";
                object replacText3 = somesecondstringyouneed;
                object replace3 = Word.WdReplace.wdReplaceAll;
                object findWrap3 = Word.WdFindWrap.wdFindContinue;
                find.Execute(ref findText3, ref o, ref o, ref o, ref oFalse, ref o,
                    ref o, ref findWrap3, ref o, ref replacText3,
                    ref replace3, ref o, ref o, ref o, ref o);

                Word.Find find4 = range.Find;
                object findText4 = "[doc content]";
                object replacText4 = somesecondstringyouneed;
                object replace4 = Word.WdReplace.wdReplaceAll;
                object findWrap4 = Word.WdFindWrap.wdFindContinue;
                find.Execute(ref findText4, ref o, ref o, ref o, ref oFalse, ref o,
                    ref o, ref findWrap4, ref o, ref replacText4,
                    ref replace4, ref o, ref o, ref o, ref o);

                Word.Find find5 = range.Find;
                object findText5 = "[doc content]";
                object replacText5 = somesecondstringyouneed;
                object replace5 = Word.WdReplace.wdReplaceAll;
                object findWrap5 = Word.WdFindWrap.wdFindContinue;
                find.Execute(ref findText5, ref o, ref o, ref o, ref oFalse, ref o,
                    ref o, ref findWrap5, ref o, ref replacText5,
                    ref replace5, ref o, ref o, ref o, ref o);

                Word.Find find6 = range.Find;
                object findText6 = "[doc content]";
                object replacText6 = somesecondstringyouneed;
                object replace6 = Word.WdReplace.wdReplaceAll;
                object findWrap6 = Word.WdFindWrap.wdFindContinue;
                find.Execute(ref findText6, ref o, ref o, ref o, ref oFalse, ref o,
                    ref o, ref findWrap6, ref o, ref replacText6,
                    ref replace6, ref o, ref o, ref o, ref o);

                Word.Find find7 = range.Find;
                object findText7 = "[doc content]";
                object replacText7 = somesecondstringyouneed;
                object replace7 = Word.WdReplace.wdReplaceAll;
                object findWrap7 = Word.WdFindWrap.wdFindContinue;
                find.Execute(ref findText7, ref o, ref o, ref o, ref oFalse, ref o,
                    ref o, ref findWrap7, ref o, ref replacText7,
                    ref replace7, ref o, ref o, ref o, ref o);

                Word.Find find8 = range.Find;
                object findText8 = "[doc content]";
                object replacText8 = somesecondstringyouneed;
                object replace8 = Word.WdReplace.wdReplaceAll;
                object findWrap8 = Word.WdFindWrap.wdFindContinue;
                find.Execute(ref findText8, ref o, ref o, ref o, ref oFalse, ref o,
                    ref o, ref findWrap8, ref o, ref replacText8,
                    ref replace8, ref o, ref o, ref o, ref o);

                Word.Find find9 = range.Find;
                object findText9 = "[doc content]";
                object replacText9 = somesecondstringyouneed;
                object replace9 = Word.WdReplace.wdReplaceAll;
                object findWrap9 = Word.WdFindWrap.wdFindContinue;
                find.Execute(ref findText9, ref o, ref o, ref o, ref oFalse, ref o,
                    ref o, ref findWrap9, ref o, ref replacText9,
                    ref replace9, ref o, ref o, ref o, ref o);

                Marshal.FinalReleaseComObject(find);
                Marshal.FinalReleaseComObject(find1);
                Marshal.FinalReleaseComObject(find2);
                Marshal.FinalReleaseComObject(find3);
                Marshal.FinalReleaseComObject(find4);
                Marshal.FinalReleaseComObject(find5);
                Marshal.FinalReleaseComObject(find6);
                Marshal.FinalReleaseComObject(find7);
                Marshal.FinalReleaseComObject(find8);
                Marshal.FinalReleaseComObject(find9);
                Marshal.FinalReleaseComObject(range);
            }
            var path_YourDocsName = path + @"\folder\YourDocsName.doc";
            Console.WriteLine(path_YourDocsName);
            doc.SaveAs(path_YourDocsName);
            ((Word._Document)doc).Close(ref o, ref o, ref o);
            doc.Close();
            app.Quit(ref o, ref o, ref o);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.StackTrace);
        }
        finally
        {
            if (doc != null) Marshal.FinalReleaseComObject(doc);

            if (docs != null) Marshal.FinalReleaseComObject(docs);

            if (app != null) Marshal.FinalReleaseComObject(app);
        }

2
尝试这个: 查找和替换函数
 private void FindAndReplace(Microsoft.Office.Interop.Word.Application WordApp, object findText, object replaceWithText)
    {
        object matchCase = true;
        object matchWholeWord = true;
        object matchWildCards = false;
        object matchSoundsLike = false;
        object nmatchAllWordForms = 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 = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
        object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
        WordApp.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord, ref matchWildCards, ref matchSoundsLike,
        ref nmatchAllWordForms, ref forward,
        ref wrap, ref format, ref replaceWithText,
        ref replaceAll, ref matchKashida,
        ref matchDiacritics, ref matchAlefHamza,
        ref matchControl);
    }

然后执行这个操作

 public void print()
    {           
            Word.Application app = new Word.Application();
            string clearancename = Form1.Texts;
            Word.Document doc = app.Documents.Open("your document link here");
            Word.Words wds = doc.Sections[1].Range.Words;
            doc.Activate();

            //Iterate the word need to change font
            foreach (Word.Range wd in wds)
            {
                if (wd.Text.Equals("<") || wd.Text.Equals(">") || wd.Text.Equals("name"))
                    wd.Font.Color = Word.WdColor.wdColorBlack;
            }
        
            FindAndReplace(app, "<name>", textbox1.Text);
            
            doc.PrintPreview();           
    }

请确保在Word文档中有一个文本,上面写着< name >(不含空格)。使用查找和替换功能可以找到该字符串,并用文本框中存储的新字符串进行替换。希望这能帮到你,我是新手。

2

事实证明,找到并替换文本没有其他选择。唯一的方法是扫描文档,提取文本,然后如果匹配,发出替换指令。这基本上与“查找和替换”相同。但是,该方法有一些变化,例如选择仅搜索文本框、表格等。


1
你可以尝试使用GroupDocs.Redaction for .NET来查找和替换Word文档中所需的单词或短语。
using (Document doc = Redactor.Load("C:\\candy.docx"))
{
     doc.RedactWith(new ExactPhraseRedaction("candy", new ReplacementOptions("[replaced]")));
     // Save the document to "candy_Redacted.docx" file.
     doc.Save(new SaveOptions() { AddSuffix = true, RasterizeToPDF = false }); 
} 

声明:我在GroupDocs担任开发者大使。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接