c# word自动查找和替换长文本字段

本文关键字:替换 文本 字段 查找 word | 更新日期: 2023-09-27 17:50:56

我有一个函数来查找和替换word文档中的文本占位符。它适用于短文本字符串。然而,当试图用较大的文本字符串,即一段文本替换时,什么也不做。

DocReplaceField(ref Word.Document objDoc, string Field, string Value)
{
   object missing = System.Reflection.Missing.Value;
   Word.Range range = objDoc.Content;
   object findtext = Field;
   object f = false;
   object findreplacement = Value;
   object findforward = false;
   object findformat = true;
   object findwrap = WdFindWrap.wdFindContinue;
   object findmatchcase = false;
   object findmatchwholeword = false;
   object findmatchwildcards = false;
   object findmatchsoundslike = false;
   object findmatchallwordforms = false;
   object findreplace = WdReplace.wdReplaceAll;
   range.Find.Execute(
       findtext,
       findmatchcase,
       findmatchwholeword,
       findmatchwildcards,
       findmatchsoundslike,
       findmatchallwordforms,
       findforward,
       findwrap,
       findformat,
       findreplacement,
       findreplace,
       missing,
       missing,
       missing,
       missing);
}

如果我尝试用"某事"替换"[占位符]",但我如何用

替换"[占位符]"呢?

"没有智慧,就没有智慧。"前庭库库特仅与凹形双门相邻。整数级别的velit,非精英级的molestie,非精英级的拍卖会。Suspendisse potenti。前庭是一种受欢迎的东西,而前庭是一种受欢迎的东西。整数eget ullamper velit。他说:"在美国,我认为这是一种流行的格言。"现在的老奥雷特·毛里斯看起来就像威尼斯人一样出色。"

例如

更新:

问题似乎是单词的查找和替换不能替换超过255个字符。搜索匹配占位符,但实际上不能替换文本。有没有人有一个例子,调用查找来定位占位符,但然后手动选择文本并插入新文本。而不是使用单词查找和替换

c# word自动查找和替换长文本字段

替换文本非常简单:找到文本后,范围对象将包含找到的文档部分。您需要首先删除找到的文本,然后插入新的文本:

range.Find.Execute(findtext, findmatchcase, findmatchwholeword, 
    findmatchwildcards, findmatchsoundslike, findmatchallwordforms, findforward, 
    findwrap, findformat, findreplacement, findreplace, missing, 
    missing, missing, missing);
range.Delete();
range.Text = "This is the new content.";

我所做的是将替换字符串分成更小的部分,并在每个字符串的末尾添加占位符(最后一个除外),然后重复替换命令的次数与字符串片段一样多。它也适用于更小的字符串,所以你不必使用不同的方法:

string acierto; // where acierto is my placeholder
string[] cadenas;
cadenas = BSV.Funciones.ParteCadenas(valor, 170); // function to split a string into 170 character pieces
for (int xx = 0; xx < cadenas.Length; xx++)
{
    if (xx < cadenas.Length - 1) cadenas[xx] += acierto;
    parrafo.Range.Find.Execute(acierto, nulo, nulo, nulo, nulo, nulo, nulo, nulo, nulo, cadenas[xx], WdReplace.wdReplaceAll, nulo, nulo, nulo, nulo);
}

这里有一个快速而简短的解决你的问题的方法。如果range.Find.Execute方法不能处理超过255个字符,那么您可以简单地手动扫描文档并搜索文本,然后从文本字符串发出替换。

要使用下面的代码,必须包含以下内容:

Using Word = Microsoft.Office.Interop.Word;
Using System.Text.RegularExpressions;   //This is used for Regex

您需要添加Microsoft Word对象库。下面是从头到尾的代码:

string filePath = @"C:'YourfilePathHere";
string findText = "Your text to locate goes here";
string replaceText = "The replacement text longer than 255 characters";
Word.Application fileOpen = new Word.Application;
Word.Document doc = fileOpen.Documents.Open(filePath, ReadOnly: false);
foreach (Word.Paragraph para in doc.Paragraphs) {
   Word.Range paraRange = para.Range;
   string text = paraRange.Text;
   Regex regex = new Regex(findText);
   string final = regex.Replace(text, replaceText);
   paraRange.Text = final;
}

现在上面的文字将被替换为一个无限大小的替换。

word . application . select . find。Execute不支持替换文本中超过255个字符。

因此,我们需要首先选择我们想要替换的单词,然后用所需的文本替换选中的文本。

object missing = System.Reflection.Missing.Value;
  wordApp.Application.Selection.Find.Execute(findText, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing,
                                 missing, missing, missing);

            wordApp.Application.Selection.Text = (String)(replaceText);
            wordApp.Application.Selection.Collapse();

您可以尝试使用Word书签代替查找和替换吗?

狙击器示例-

object oBookMark = "MyBookmark";
oDoc.Bookmarks.Item(ref oBookMark).Range.Text = "Some Text Here";

您的问题可能是搜索多行文本。

尝试添加"'r'n"每次你越过一行,你也不能只是做一个多行字符串,你需要在开始使用@:

@"firstLine'r'n
second";

除此之外,我在execute方法中没有看到任何多行选项。请注意,execute方法中的参数都有默认值,您可以使用命名参数并省略不使用的参数。

也请编辑你的问题,多行确实是造成问题的原因。

编辑:与其将替换文本作为参数给出,不如在之后设置它。http://social.msdn.microsoft.com/forums/en us/vsto/thread/9c50450e - 9579 - 4 - e89 - 8 e9c - 8 c84c8319d0b

range.Execute(... arguments ...);
range.Text = "Replacement text more than 255 characters";

另一个选项是使用^c作为替换文本,这将提示Word使用剪贴板上的任何文本作为替换文本。http://word.tips.net/T000021_Replacing_Long_Blocks_of_Text.html

System.Windows.Forms.Clipboard.SetText("Replacement text longer than 255 chars");
range.Execute(... replacementText: "^c ...); // don't actually know where you enter your replacement text :P