Friday, May 10, 2013

Word interop problem : String parameter too long.

Hi Cheong00


my problem has been solved via this code :



private static bool FindAndReplaceFoods(Microsoft.Office.Interop.Word.Application wordApp, List<string> findTags, List<string> replaceTexts)
{
bool result = false;

for (int i = 0; i < findTags.Count; i++)
{
object findText = findTags[i];
object replaceWith = replaceTexts[i];
result = FindAndReplace2(wordApp, findText, replaceWith);
}

return result;
}

private static bool FindAndReplace2(Microsoft.Office.Interop.Word.Application wordApp, object findText, object replaceWithText)
{
bool result = false;

object matchCase = true;
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 readOnly = false;
object visible = true;
object replace = 2;
object wrap = 1;

string strNewReplaceWith = string.Empty;
string[] strReplacements = replaceWithText.ToString().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < strReplacements.Length; i++)
{
if (i != strReplacements.Length - 1)
{
strNewReplaceWith = string.Format("{0}\r\n{1}", strReplacements[i], findText);
}
else
{
strNewReplaceWith = strReplacements[i];
}
object newReplaceWith = strNewReplaceWith;
result = wordApp.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord, ref matchWildCards, ref matchSoundsLike, ref matchAllwordForms,
ref forward, ref wrap, ref format, ref newReplaceWith, ref replace, ref matchKashida, ref matchDiacritics, ref matchAlefHamza,
ref matchControl);
}

return result;
}

thanks for your attention and helps.


regards




http://www.codeproject.com/KB/codegen/DatabaseHelper.aspx


No comments:

Post a Comment