2013-10-23 32 views
4

我想用C#创建一个世界文档。 所以这是我的代码来替换word文档变量。错误“字符串参数太长”。在microsoft.office.interop.word.find.execute

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. 

所以,我怎么能取代长字符串?

+2

这是从ASP.NET或其他服务器技术使用Office互操作一个可怕的想法添加一个新的文本。这些API被编写用于桌面应用程序,用于自动化Office(一套桌面应用程序)。服务器应用程序在许多方面有所不同,因此在其中使用Office Interop是非常非常糟糕的主意。它也不受Microsoft的支持,并可能违反您的Office许可证。请参阅[服务器端自动化Office的注意事项](http://support.microsoft.com/kb/257757) –

回答

0

只需将您的长字符串分割为(无论你想要什么)和调用你的FindAndReplace(..)多次(为你所有新生成的数组项目)。

+0

如何操作?如何多次替换一个单词?请解释它。 – user1348351

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是已知可从来没有拿出文档中的字符串常量。

+0

只需要添加,Find.Execute的限制似乎是255,因此如果首选的话,一次可以替换20个以上的字符。 – MoonBoots89

3

而不是使用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..."; 
+0

这个工程。这真的帮助我很多!谢谢 – CB4

+0

这对我来说也非常有用,非常感谢 – GGSoft