C#单词自动查找和替换长文本字段 [英] C# word automation find and replace long text field

查看:130
本文介绍了C#单词自动查找和替换长文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个功能来查找和替换word文档中的文本占位符.对于短文本字符串来说,这很好用.但是,当尝试替换较大的文本字符串(即一段文本)时,什么也不做.

I have a function to find and replace a text placeholder inside a word document. Which works fine for short text strings. Does nothing however when attempting to replace with larger text strings i.e. a paragraph of text.

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",这是可行的,但是我如何将

This works find if i try replacing "[placeholder]" with "Something" but how do i replace "[placeholder]" with

"Nullam non lorem sapien,and imperdiet sapien.Curabiturvestibulum justo eu en enim bibendum vulputate.整数vel velit non elit molestie auctor non ut nisi. ".穆拉(Sam)在普鲁斯(Pures)坐下,坐在非neque的膝盖上,不拘一格.praesent laoreet mauris id sem venenatis pellentesque.

"Nullam non lorem sapien, et imperdiet sapien. Curabitur vestibulum justo eu enim bibendum vulputate. Integer vel velit non elit molestie auctor non ut nisi. Suspendisse potenti. Donec augue nulla, vestibulum a pulvinar id, scelerisque quis mauris. Integer eget ullamcorper velit. Sed at purus sit amet felis pulvinar dictum at non neque. Praesent laoreet mauris id sem venenatis pellentesque."

例如

更新:

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

The issue seems to be that word's find and replace cannot replace with more than 255 characters. The search is matching the placeholder but cannot actually replace the text. Does anyone have an example of calling the find to locate the placeholder but then manually selecting the text and inserting new text. Instead of using the word find and replace.

推荐答案

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

It's quite simple to replace text: Once you found it, the range object will contain the found portion of the document. You need to first delete the found text, then insert the new one:

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.";

这篇关于C#单词自动查找和替换长文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆