Lost in .NET Code

Developing software in .NET, Security and other ramblings.

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.

Labels: ,


Subscribe in a reader


Blogs I read

Tristan Phillips
Sarah Blow (.Net Mobile)
Mike Taulty (MS DPE)
Ian Griffths (WPF)
Jack Greenfield


Useful Links

Fircroft Trust Ltd
Unwind Software Ltd


Archives

December 2006   January 2007   February 2007   March 2007   April 2007   May 2007   June 2007   October 2007   November 2007   February 2008   April 2008   May 2008   June 2008   July 2008   August 2008   October 2008   November 2008   December 2008   January 2009  


Fun and Games



 

This page is powered by Blogger. Isn't yours?