8

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

 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”太孤单,就会出现错误,它会说

String parameter too long.

那么如何替换长字符串呢?

4

4 回答 4

8

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

替换特定文本的示例

// Find text 
Range range = doc.Content;
range.Find.Execute(findText);
range.Text = "new 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...";
于 2016-07-19T11:01:52.113 回答
1

我知道这已经有一年了,但由于问题的技术方面从未得到回答,我想我会花时间为偶然发现它的人发布这个。

你可以做这样的事情你真的必须这样做。

FindAndReplace(word, replacementKey, SequentialReplaceToken);
var restOfText = replaceWithText;
while (restOfText.Length > 20)
{
    var firstTwentyChars = restOfText.Substring(0, 20);
    firstTwentyChars += SequentialReplaceToken;
    restOfText = restOfText.Substring(20);
    FindAndReplace(word, SequentialReplaceToken, firstTwentyChars);
}
FindAndReplace(word, SequentialReplaceToken, restOfText);

FindAndReplace(...) 是 Word 互操作函数的包装器。像这样:

private void FindAndReplace(Application doc, object findText, object replaceWithText)
{
    //options
    object matchCase = false;
    object matchWholeWord = true;
    object matchWildCards = false;
    object matchSoundsLike = false;
    object matchAllWordForms = 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;
    //clear previous formatting
    doc.Selection.Find.ClearFormatting();
    //execute find and replace
    doc.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord,
        ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref replaceWithText, ref replace,
        ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl);
}

并且 SequentialReplaceToken 是一个已知永远不会出现在文档中的字符串常量。

于 2015-05-03T06:29:04.020 回答
1

我已经尝试过这段代码

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

问题是(它替换了所有文档)我希望它只是替换特定文本,例如“textwanttoreplace”

于 2020-09-22T17:17:10.947 回答
0

只需将您的长字符串拆分为子字符串、字符数组等(无论您想要什么)并多次调用您的 FindAndReplace(..)(对于所有新生成的数组项)。

于 2013-10-23T06:15:56.220 回答