1
votes

I am writing a desktop application using C# with WinForms. The application searches for text and replaces it with the users input.

The problem is when with the textbox that has its multiline property set totrue, if there are more than 254 chars entered a string parameter too long error is returned from System.Runtime.InteropServices.COMException.

Here is the method:

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);          
    }

I have read that one possible way to solve this is to break the value into multiple arrays of strings, but is that the only way around this or is there an easier way?

Some other solutions have been in ASP.NET to write a JS wrapper but I dont have server side functionality.

Also why is there a 254 length limit on a multiline textbox when working with Interop.Word anyway?

1
A few things: 1. Do you really need so many parameters do they really need to be passed by reference? Are you trying to change their value? You're not saving much overhead here. 2. Why are you implicitly casting your bools and ints to object? You realize that when you do that, you loose all properties and methods of that type. You cant if do this if (isTrue) if its an object, so how would you use them? 3. Why not just use var? var implicitly detects the type, and retains its properties and methods. 4. Why not just create a parameter object that stores all these variables? - AustinWBryan
This particular answer may help: stackoverflow.com/a/32197436/68972 - Jcl

1 Answers

0
votes

Set the maxLength to 0:

enter image description here

You can then enter the maximum number of char (Max Char in TextBox C#?) for the textbox.

enter image description here