我有一个在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);
}如果我尝试将"placeholder“替换为"Something”,那么如何将"placeholder“替换为
"Nullam non lorem sapien,et imperdiet sapien. Curabitur vestibulum ut eu enim bibendum .整数velit non elit molestie non ut nisi. Suspendisse potenti. Donec augue,vestibulum a pulvinar id,scelerisque mauris.整数eget ullamcorper velit. Sed at purus sit amet felis at non neque. Praesent laoreet mauris sem venenatis pellentesque.“
例如
更新:
问题似乎是word的查找和替换不能替换超过255个字符。搜索与占位符匹配,但无法实际替换文本。有没有人有这样的例子:调用find来定位占位符,然后手动选择文本并插入新文本。而不是使用查找和替换这个词。
发布于 2013-01-08 21:02:39
您的问题可能是搜索多行文本。
尝试在每次跨越一行时添加"\r\n“,而且您不能只生成这样的多行字符串,您需要在开头使用@:
@"firstLine\r\n
second";除此之外,我在execute方法中没有看到任何多行选项。请注意,execute方法中的参数都有一个默认值,您可以使用命名参数,而不使用不使用的参数。
另外,请编辑您的问题多行确实导致了问题。
编辑:不是将替换文本作为参数,而是应该在之后设置它。http://social.msdn.microsoft.com/forums/en-US/vsto/thread/9c50450e-9579-4e89-8e9c-8c84c8319d0b
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 :Phttps://stackoverflow.com/questions/14215308
复制相似问题