Range.Find problem

D

David Thielen

Hi;

I call the following code:
object start = rangeStart; // = 249
object end = rangeEnd; // = 2851
Range range = app.ThisDocument.Range(ref start, ref end);
range.Find.ClearFormatting();
range.Find.Forward = forward;
range.Find.Text = text;
range.Find.Wrap = WdFindWrap.wdFindStop;

object missingValue = Type.Missing;

range.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);

Returns:
Range.Find.Found = true
Range.Start = 237
Range.End = 241

The string I am looking for is there too - but it is before the start. What
is going on here?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 
W

Wei Lu [MSFT]

Hello Dave,

In the range, did it have the text you want to find?

I did not reproduce this issue in VBA.

Sincerely,

Wei Lu
Microsoft Online Community Support

==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

Cindy M.

Hi =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=,
I call the following code:
object start = rangeStart; // = 249
object end = rangeEnd; // = 2851
Range range = app.ThisDocument.Range(ref start, ref end);
See if it makes any difference to use app.ActiveDocument
instead of ThisDocument. ThisDocument is actually the "VBA
code container" for a document, not the document object you
may think you're working with.

It might also not hurt to put a range.Select() line in there,
right after you define the range, just to make sure it's where
you think it is. Working with numbers to define Start and End
points is very unreliable...

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun
17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :)
 
W

Wei Lu [MSFT]

Hello Dave,

Yes, I reproduce this issue in the XML file. If I only copy those text into
a word document, the Range.Find works fine.

I will contact with the internal team.

Sincerely,

Wei Lu
Microsoft Online Community Support

==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

Cindy M.

Hi =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=,
I tried the select but still get the problem.
OK, when I try the Select with the original range (249 to
2851) it selects all the way back to position 237 in this
document.

The reason is that all the information in this paragraph is
generated from a field code. Press Alt+F9 and you'll see
this is an AutoTextList field.

This kind of thing is a bit like setting ranges in tables:
as soon as you extend a range across cell borders the
entire row (or more) is included in the range. Extending
the range beyond the field's end - or even within the field
result - includes the entire field.

Select that paragraph, then Shift+Ctrl+F9 to turn the field
result into static text. Now try your code. On my machine I
get the "expected result".

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update
Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any
follow question or reply in the newsgroup and not by e-mail
:)
 
C

Cindy M.

Hi Dave,
In that case, how can I get it to start the find at 249? I start there
because that is where the range ends and I am walking through the document
finding my <wr:...> tags.
What happens with Selection.Find, being sure to set the direction to
"Forward"?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
C

Cindy M.

Hi =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=,
I do have it set to forward.
But have you tried using Selection.Find instead of
Range.Find? Selection.Find should start from the point
where you just finished. You don't need to tell it a
specific range to search in.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update
Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any
follow question or reply in the newsgroup and not by e-mail
:)
 
C

Cindy M.

Hi =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=,
Oh...

Can't do that because it moves the document selection around and we call it
when we are just checking where the caret is.
Mmmm. You could, perhaps:
- Application.ScreenUpdating = false
- Save the original selection.Range in a Range variable
- check whether the found Range is in a field, if it is Range.Select then
continue the Find with Selection until you're out of a field (to keep the
jumping around to a minimum)

Another approach, if you find yourself within a field, would be to "walk" all
the characters until you're out of the field. Not as efficient as Find, but
doable.

Note that you'll encounter similar problems when working with Tables. There,
however, you have the option of looping through each cell.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

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