Word : Selection.Find.Execute
Wednesday, April 2, 2008
Just because I have run into this one this week. Under no circumstance use
Selection.Find.Execute
in the word object model, it is horribly buggy. It may work on your development box and 80% of the boxes you deploy it on but for 20% of them it will fail on with a lovely com death.
You have to if you need to use the find / replace command in Word. Late bind to it, don't early bind under any circumstance.
object searchText = @"\<\<*_*\>\>";
object myFind = wd.Selection.Find;
object[] Parameters;
Parameters = new object[15];
Parameters[0] = searchText;
Parameters[1] = wdFalse;
Parameters[2] = wdFalse;
Parameters[3] = wdTrue;
Parameters[4] = wdFalse;
Parameters[5] = wdFalse;
Parameters[6] = wdFalse;
Parameters[7] = wdFalse;
Parameters[8] = n;
Parameters[9] = n;
Parameters[10] = wdFalse;
Parameters[11] = wdFalse;
Parameters[12] = wdFalse;
Parameters[13] = wdFalse;
Parameters[14] = wdFalse;
while ((bool)myFind.GetType().InvokeMember("Execute", BindingFlags.InvokeMethod, null, myFind, Parameters))
{
Microsoft have released a KB also. Which took me a while to find searching through google.
http://support.microsoft.com/kb/313104/en-us
http://support.microsoft.com/kb/292744/en-us
Hopefully this one stops someone else having the same nightmare.

