错误“字符串参数太长".在microsoft.office.interop.word.find.execute [英] error "string parameter too long". at microsoft.office.interop.word.find.execute

查看:186
本文介绍了错误“字符串参数太长".在microsoft.office.interop.word.find.execute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用C#创建一个世界文档. 这是我的替换word文档变量的代码.

I want to create a world document using C#. So this is my code for replace word document variables.

 private void FindAndReplace(Microsoft.Office.Interop.Word.Application WordApp, object findText, object replaceWithText) 
 {
    try {
        object matchCase = true;
        object matchWholeWord = true;
        object matchWildCards = false;
        object matchSoundsLike = false;
        object nmatchAllWordForms = false;
        object forward = true;
        object format = false;
        object matchKashida = false;
        object matchDiacritics = false;
        object matchAlefHamza = false;
        object matchControl = false;
        object read_only = false;
        object visible = true;
        object replace = 2;
        object wrap = 1;

        WordApp.Selection.Find.Execute(ref findText,
        ref matchCase, ref matchWholeWord,
        ref matchWildCards, ref matchSoundsLike,
        ref nmatchAllWordForms, ref forward,
        ref wrap, ref format, ref replaceWithText,
        ref replace, ref matchKashida,
        ref matchDiacritics, ref matchAlefHamza,
        ref matchControl);
    } catch (Exception error) {
        lblerror.Visible = true;
        lblerror.Text = error.ToString();
    }
}

但是在这里,如果"replaceWithText"太孤独了,就会报错

but in here if the "replaceWithText" too lone there is error and it says

String parameter too long.

那我该如何替换长字符串呢?

So how can I replace long string?

推荐答案

代替使用Find.Execute()进行替换:查找文本,获取其位置,插入新文本.这不会限制您使用新字符串的长度.

Instead of replacing using Find.Execute(): find a text, get its position, insert new text. That would not limit you in a length of the new string.

替换特定文本的示例

// Find text 
Range range = doc.Content;
range.Find.Execute(findText);
range.Text = "new text...";

在特定文字之后添加新文字的示例

Example to add a new text after specific text

// Find text 
Range range = doc.Content;
range.Find.Execute(findText);
// Define new range 
range = doc.Range(range.End + 1, range.End + 1);
range.Text = "new text...";

这篇关于错误“字符串参数太长".在microsoft.office.interop.word.find.execute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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