首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C# word自动查找和替换长文本域

C# word自动查找和替换长文本域
EN

Stack Overflow用户
提问于 2013-01-08 20:48:58
回答 6查看 6.9K关注 0票数 2

我有一个在word文档中查找和替换文本占位符的函数。它可以很好地处理短文本字符串。但是,当尝试替换为更大的文本字符串时,不执行任何操作。

代码语言:javascript
运行
复制
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来定位占位符,然后手动选择文本并插入新文本。而不是使用查找和替换这个词。

EN

Stack Overflow用户

发布于 2013-01-08 21:02:39

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

尝试在每次跨越一行时添加"\r\n“,而且您不能只生成这样的多行字符串,您需要在开头使用@:

代码语言:javascript
运行
复制
@"firstLine\r\n
second";

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

另外,请编辑您的问题多行确实导致了问题。

编辑:不是将替换文本作为参数,而是应该在之后设置它。http://social.msdn.microsoft.com/forums/en-US/vsto/thread/9c50450e-9579-4e89-8e9c-8c84c8319d0b

代码语言:javascript
运行
复制
range.Execute(... arguments ...);
range.Text = "Replacement text more than 255 characters";

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

代码语言:javascript
运行
复制
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
票数 0
EN
查看全部 6 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14215308

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档