C#word自动查找和替换长文本字段
我有一个在word文档中查找和替换文本占位符的功能。这对于短文本字符串很好。但是,当尝试替换为较大的文本字符串(即文本段落)时,不执行任何操作C#word自动查找和替换长文本字段,c#,ms-word,C#,Ms Word,我有一个在word文档中查找和替换文本占位符的功能。这对于短文本字符串很好。但是,当尝试替换为较大的文本字符串(即文本段落)时,不执行任何操作 DocReplaceField(ref Word.Document objDoc, string Field, string Value) { object missing = System.Reflection.Missing.Value; Word.Range range = objDoc.Content; object fin
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);
}
如果我尝试用“某物”替换“[placeholder]”,这是可行的,但是如何用“某物”替换“[placeholder]”
“无知识的智者,无知识的智者。前庭库拉比图尔酒店。整数级非Elite分子拍卖商非ut nisi。潜力悬钩子。顿涅克·奥古斯·努拉,前庭一个普尔文纳id,权杖奎斯·莫里斯。整数eget ullamcorper velit。在purus坐着amet felis pulvinar的名言在non neque。帕雷森特·拉奥里特·莫里斯(Praesent laoreet mauris id sem Venetatis pellentesque)。”
比如说
更新:
问题似乎是word的“查找和替换”不能替换为超过255个字符。搜索与占位符匹配,但实际上无法替换文本。是否有人举过这样的例子:调用“查找”来查找占位符,然后手动选择文本并插入新文本。而不是使用“查找和替换”一词eplace.你能试着用Word书签代替查找和替换吗 一个狙击手的例子-
object oBookMark = "MyBookmark";
oDoc.Bookmarks.Item(ref oBookMark).Range.Text = "Some Text Here";
您的问题可能是搜索多行文本 每次横过一行时,请尝试添加“\r\n”,而且您不能仅创建这样的多行字符串,您需要在开始时使用@:
@"firstLine\r\n
second";
除此之外,我在execute方法中没有看到任何多行选项。请注意,execute方法中的参数都有一个默认值,您可以使用命名参数,而不使用命名参数
另外,请编辑您的问题多行确实是造成问题的原因
编辑:
您应该在以后设置替换文本,而不是将其作为参数。
另一个选项是使用^c作为替换文本,这将提示Word使用剪贴板上的任何文本作为替换文本。
替换文本非常简单:找到文本后,range对象将包含文档中找到的部分。您需要先删除找到的文本,然后插入新文本:
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);
}
string acierto;//其中acierto是我的占位符
弦[]卡德纳斯;
cadenas=BSV.functiones.ParteCadenas(valor,170);//将字符串拆分为170个字符的函数
对于(int xx=0;xx
这是一个快速而简短的问题解决方案。如果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.Selection.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();
不,因为系统将使用100个不会添加书签的模板。我认为这是Word的一个限制。在替换中最多只能使用255个字符。直接在查找和替换Word中尝试。我在Word 2013中看到这个限制。是的,我同意Word本身有限制,我正在寻找一个代码解决。在这种情况下,您将uld需要稍微修改一下,将您的重放字符串拆分为250个字符的块,然后迭代替换。这不是您能做的最干净的事情。搜索工作很好,替换部分是Srikanth Venugopalan提到的问题替换文本长度超过255个字符,这是“查找替换”功能的限制word中的字符串。搜索短语不能超过255个字符。这不起作用,在运行时,它将在查找时停止。执行findtext参数太长。
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;
}
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();