【问题标题】:C#: Writing to word document "string param too long"C#:写入 word 文档“字符串参数太长”
【发布时间】:2015-12-18 23:19:23
【问题描述】:

我正在使用带有 WinForms 的 C# 编写桌面应用程序。应用程序搜索文本并将其替换为用户输入。

问题是当textbox 的多行属性设置为true 时,如果输入的字符超过254 个,则string parameter too long 会从System.Runtime.InteropServices.COMException 返回错误。

方法如下:

private void FindAndReplace(Word.Application WordApp, object findText, object replaceWithText)
    {
        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);          
    }

我已阅读到解决此问题的一种可能方法是将值分解为多个字符串数组,但这是解决此问题的唯一方法还是有更简单的方法?

其他一些解决方案已经在 ASP.NET 中编写 JS 包装器,但我没有服务器端功能。

另外,在使用 Interop.Word 时,为什么多行 textbox 有 254 个长度限制?

【问题讨论】:

  • 几点: 1.你真的需要这么多参数吗?真的需要通过引用传递吗?你想改变他们的价值吗?您不会在这里节省太多开销。 2. 你为什么隐式地将你的 bool 和 int 转换为object?您意识到,当您这样做时,您会丢失该类型的所有属性和方法。如果它是一个对象,你不能这样做if (isTrue),那么你将如何使用它们? 3. 为什么不直接使用varvar 隐式检测类型,并保留其属性和方法。 4. 为什么不直接创建一个存储所有这些变量的参数对象?
  • 这个特定的答案可能会有所帮助:stackoverflow.com/a/32197436/68972

标签: c# winforms office-interop


【解决方案1】:

将 maxLength 设置为 0:

然后您可以输入文本框的最大字符数 (Max Char in TextBox C#?)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-16
    相关资源
    最近更新 更多