Word's "Find" not working as expected

O

Omatase

I have a string of text found twice in the same word document. Even
though I indicate a starting point that is before the first instance
of this string and I mark Forward = true the first instance it finds
is the instance at the end of the document. Is there some trick I can
apply to cause it consistently to find the first instance?

Here is my code (aDoc is a Document):

object missingValue = System.Reflection.Missing.Value;

object start = 0;
object end = aDoc.Content.End;

searchResultRange = aDoc.Range(ref start, ref end);
searchResultRange.Find.ClearFormatting();
searchResultRange.Find.Forward = true;
searchResultRange.Find.Text = "zip";
searchResultRange.Find.Wrap = WdFindWrap.wdFindStop;

searchResultRange.Find.Execute(ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue);

I am using Word XP
 
S

Shauna Kelly

Hi Omatose

My first check would be to ensure that there are no settings in the .Find
object carried over from former use in the UI or code. For example, if you'd
previously used .MatchCase and one of the "zip" texts was actually "Zip",
the .Find would fail.

For more info and sample code, see the ClearFindAndReplaceParameters routine
at
http://www.word.mvps.org/FAQs/MacrosVBA/FlushFR.htm

For what it's worth, I run code like that before any .Find operation.

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 

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