Word interop from c# - Find replace sometimes not working

P

progger100

I use the following code to replace all instances of "[VAR]" with "value to
set" in a word document :

Word.Document document;
....
Word.Range rng = document.Content;
rng.Find.ClearFormatting();
Object findText = "[VAR]";
Object matchCase = Type.Missing;
Object matchWholeWord = Type.Missing;
Object matchWildcards = Type.Missing;
Object matchSoundsLike = Type.Missing;
Object matchAllWordForms = Type.Missing;
Object forward = true;
Object wrap = Word.WdFindWrap.wdFindStop;
Object format = Type.Missing;
Object replaceWith = "value to set";
Object replace = Word.WdReplace.wdReplaceAll;

rng.Find.Execute(ref findText, ref matchCase, ref matchWholeWord, ref
matchWildcards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref
wrap, ref format, ref replaceWith, ref replace);

On most pc's, this ALWAYS works fine, but on some pc's, it NEVER works.
The text is not replaced, nothing happens.
I had this problem 2 times until now, while 13 other machines worked
perfectly.

Then I tried :
....
Object replaceWith = Type.Missing;
Object replace = Type.Missing;

while(rng.Find.Execute(ref findText, ref matchCase, ref matchWholeWord, ref
matchWildcards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref
wrap, ref format, ref replaceWith, ref replace) == true)
{
rng.Text = "value to set";// The range is now changed to the found "[VAR]"
instance, we can replace it
rng = document.Content;// reset to loop further
}

This also does or does not work on the same systems.

The basic problem is, that the Find.Execute method does NOT select the found
text part on those pc's where it does not work. Also when I tried to work
through Selection.Find (instead of using Range.Find), nothing changed.

So at this moment, I have before me 2 almost identical pc's, with the same
windows and office version (office XP SP3), handling the same document, and
on 1 pc it works, on the other one it does not. I checked all Word options
and settings, there is no difference.

Does anyone have any idea where this is coming from ?
 
P

progger100

I found the solution : this is a bug in the Word interop dll.
If you invoke the "Execute" method through late binding, it works perfectly.

Word.Find find;

Object findText = text;
Object matchCase = Type.Missing;
Object matchWholeWord = Type.Missing;
Object matchWildcards = Type.Missing;
Object matchSoundsLike = Type.Missing;
Object matchAllWordForms = Type.Missing;
Object forward = true;
Object wrap = Word.WdFindWrap.wdFindStop;
Object format = Type.Missing;
Object replaceWith = Type.Missing;
Object replace = Type.Missing;

object[] args = new object[]{findText, matchCase, matchWholeWord,
matchWildcards, matchSoundsLike, matchAllWordForms, forward, wrap, format,
replaceWith, replace};

if ( (bool)(find.GetType().InvokeMember("Execute",
System.Reflection.BindingFlags.InvokeMethod, null, find, args)) == true)
{
...
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top