problems with Find

C

Chris Ellis

Hi,

I'm writing an add-in for Word that does a lot of find/replace
operations. Some of these I do using Range.Find.Execute2007... for more
complex problems I built a Regex find/replace engine. There is one point at
which I am performing two searches back to back on the same range (two
duplicates of the same range, that is). Both strings are present in the
range. When I attempt to debug it I can verify this. One find succeeds,
the second one fails, but if I reverse the order, the same one fails whether
before or after the other... well... here's the text in the range that I'm
searching:

=========================================================================
"SCHEDULE A
TRUST ESTATE PROPERTY
The following described property is hereby conveyed and transferred to, and
deposited with TRUSTEE, Trustee of THE REVOCABLE LIVING TRUST, subject to
the terms and conditions of the foregoing Trust Agreement:
Real Estate:
All real estate wherever situated, owned by Grantor or in which Grantor has
an interest.
Personal Property:
1. All cash, gold or silver bullion or coins, checking accounts, savings
accounts, certificates of deposit, money fund deposits, or similar cash or
cash equivalent accounts now held in the name of Grantor.
2. All stocks, bonds, investments, and securities of any nature whatsoever
now owned beneficially or of records by Grantor.
3. All notes, contracts, mortgages, and deeds of trust receivable (if any)
now owned by Grantor.
4. All household furnishings, goods, and appliances, and all jewelry and
personal effects, or other personal property of any nature and wherever
situated (except motor vehicles) now owned by Grantor.
All of the above-described property consisted of, or while held in trust
hereunder, shall be deemed to be separate property of Grantor.
Dated the _________ day of _____________________, 2008.
, GRANTOR"
=========================================================================

the two ops are:

rng.Find.Execute2007("TRUSTEE", True) <- returns True
rng.Find.Execute2007("THE REVOCABLE LIVING TRUST", True) <- returns False

walking the code, the above are what the code boils down to, though my
actual code calls my own find function, passing range and text for which to
search as parameters... and it does first make a duplicate of the range...
the calls above are the ones that are actually being made in the heart of my
own little find/replace system... and I have verified that in both the range
text is the text above...

no matter the order, the first one above (TRUSTEE) always succeeds and the
second one always fails, but it seems clear to me that both are in the
range. Am I doing something fundamentally wrong here? Is this a known
issue? If so, any work-arounds? If I use my own regex system it works just
fine, but my concern is that I am using Word's internal find throughout my
add-in, but I have not taken the time to check to make sure that every find
is working as it ought to... if, after my code finishes running, I go into
Word and open its Find window and copy/paste the quote directly out of my
code, it finds "THE REVOCABLE LIVING TRUST" just fine... so I'm stumped...

Thank you,
Chris
 
K

Klaus Linke

Hi Chris,
rng.Find.Execute2007("TRUSTEE", True) <- returns True
rng.Find.Execute2007("THE REVOCABLE LIVING TRUST", True) <- returns False

You realize that after the first line, rng is set to the match (with the
text "TRUSTEE" in your case)?
So the second line won't ever match anything if the first did.

You'd have to reset rng to whatever Range you want to search in, between the
searches.

To debug stuff involving ranges, it's helpful to sprinkle some rng.Select
lines in for debugging, then single-step through the code.

Regards,
Klaus
 
T

Tony Jollans

If you have a single Find that is failing consistently you must either not
be matching with all the settings in the find object or the range is not
what you think it is. The first thing I would do is to step through the
code, and immediately before the find, do a rng.select so you can see what
the range is. If it is correct you'll need to check the settings - maybe
after selecting you could, while still at the break, do a find in the UI
where it's a little easier to see what's going on.

If the string can be found in the same document at more or less the same
time there is nothing inherently wrong and it's down to good old-fashioned
debugging.
 
C

Chris Ellis

I appreciate the reply Klaus, but what I was trying to say is that I only
search a duplicate of the original range... so the first find operation does
not affect the range that I am searching from... the second search is
another duplicate of the same search... and the order of the operations does
not affect the results... the text that I put into my post was copied
directly from the watch window for the value of rng.Text, just before the
failed search. So this is not the problem. Thank you for the reply though
 
C

Chris Ellis

The settings could definitely be to blame, though I do not understand how or
why... but... is there a way to reset all of the settings? or should I
rather be setting them all before every search? I don't mess with them much
throughout the project, but there are a couple of places where I have used
some of the other settings of the Find or the Find.Replacement objects.

As for the range, I am actually making an add-in, and it will not allow me
to use the UI from a breakpoint, but I am able to look at the range that I'm
using from the watch window just prior to the execution of the
Find.Execute2007 command, and the contents of rng.Text just before the point
of failure is the exact text that I posted in my original post. I copied
that directly from the watch window. It includes the string "THE REVOCABLE
LIVING TRUST", and that is what I was searching for... but the search
fails... I can see that it ought not to fail with my own eyes... but for
some reason it is failing... I actually suspect the settings, but I don't
know what to look for, or how to simply clear them in such a way as to
say... just look for the text that I'm giving you... if you could help me
understand how to do that... I think it might reveal the nature of the
problem that I'm having... this seems to simple a problem to be a bug in
Word... but I cannot as of yet see the problem on my end...

Thanks for your reply,
Chris
 
T

Tony Jollans

It's very hard to pin this down at a distance; take a look at the find
object in the locals window - you might spot something. I don't know what
would be stopping you using the UI at a breakpoint. What sort of AddIn are
you working on, and what language are you coding in? Anyway, try this...

Immediately before the failing find add this (temporarily)

dim temprng as word.range
set temprng = rng.parent.range(rng.start, rng.end)
msgbox temprng.findexecute2007("THE REVOCABLE LIVING TRUST", True)

This should give you a brand new range, with a brand new find object, set to
the same range as rng. If the find works then it must be a setting problem.
If not, it would appear to be something else.
 
K

Klaus Linke

Hi Chris,

Perhaps read my reply once more? <g>
Put some rng.Select in there to watch what happens to rng.

If you really have more code than was in your post, did you use
rng.Duplicate to set up the duplicate range?
When you use "Set rngOld=rng", rngOld and rng will be the same object -- so
whatever you do to rng will change rngOld too.

Klaus
 
K

Klaus Linke

If the text in the range is already the same as the text you're searching
for ("THE REVOCABLE LIVING TRUST"), Find.Execute2997 won't find it again...
It'll go off to search for the next occurrance.

Klaus
 
C

Chris Ellis

yeah, I'm actually writing an add-in using VB.NET... the code is similar,
but Set is not necessary, so my code is actually doing:

Dim rngSearch As Range = rng.Duplicate

then I'm doing my searching on rngSearch... and unfortunately, doing
rng.Select is not as much of an option... it sounds like in regular macros
you can interact with the UI during debugging... when debugging the add-in,
interacting with the UI is out... but I use my watch window to examine the
contents of rngSearch.Text and the range is proper... and the text that I'm
looking for is there...
 
C

Chris Ellis

I'm creating an add-in using VB.NET in Visual Studio 2005, using Visual
Studio Tools for Office Second Edition... when Visual Studio is at a break
point, Word's UI is not accessible... would be really nice if it were :eek:)

I will give that a shot when I get the chance, and I'll let ya know what
happens... I do expect it will work... as I cannot see whatelse could be
suspect other than the find settings... I just didn't know how to get a
clean find object... so as to test... so I take it that when you set one
range to a duplicate of another, it gets an exact duplicate, including all
of the find settings? For some reason I thought maybe it would get a clean
find object... but I guess it makes sense... since it is called "Duplicate"
:eek:)

thanks again for your help,
Chris
 
C

Chris Ellis

Well Tony, I appreciate your help... I tried what you said... this is
absolutely baffling! I passed in to my find function a brand new copy of
the range through the document as you suggested. I'm right now at a break
just after the Find function. Again, the range was fresh from the document.
The range contains the following text... copied directly from the watch
window of rng.Text:

rng.Text "SCHEDULE A
TRUST ESTATE PROPERTY
The following described property is hereby conveyed and transferred to, and
deposited with TRUSTEE, Trustees of THE REVOCABLE LIVING TRUST, subject to
the terms and conditions of the foregoing Trust Agreement:
Real Estate:
All real estate wherever situated, owned by Grantors or in which Grantors
has an interest.
Personal Property:
1. All cash, gold or silver bullion or coins, checking accounts, savings
accounts, certificates of deposit, money fund deposits, or similar cash or
cash equivalent accounts now held in the name of Grantors.
2. All stocks, bonds, investments, and securities of any nature whatsoever
now owned beneficially or of records by Grantors.
3. All notes, contracts, mortgages, and deeds of trust receivable (if any)
now owned by Grantors.
4. All household furnishings, goods, and appliances, and all jewelry and
personal effects, or other personal property of any nature and wherever
situated (except motor vehicles) now owned by Grantors.
All of the above-described property consisted of, or while held in trust
hereunder, shall be deemed to be separate property of Grantors.
Dated the _________ day of _____________________, 2008.
, GRANTOR

, GRANTOR

" String


and the variable that I'm passing in as the first method of the Find method:

text "THE REVOCABLE LIVING TRUST" String <-- (again copied directly from
watch window)

the only other variable in use is bMatchCase which is True. This is the
line that is failing, with all of the above data:

Dim bRes As Boolean = rng.Find.Execute2007(text, bMatchCase)

the result... False... Word failed to find it... I am left with the
conclusion that this is a bug in Word's find mechanism. I have a
work-around. I just use my own Regex search and it finds it just fine, but
I sure would like to understand this... it seems impossible that such a bug
would exist and never have been detected... but I can't see any other
logical conclusion... oh well...

thanks again,
Chris
 
T

Tony Jollans

it seems impossible that such a bug would exist and never have been
detected...

Well, Execute2007 is new and bugs in it won't necessarily have been picked
up by many people yet. Does it work if you use the old Execute?

I have just copied the document text from your post directly into a new
document and run this code

Dim rng As Word.Range, text As String, bMatchCase As Boolean
Set rng = ActiveDocument.Content
rng.End = rng.End - 1 ' drop final para mark
text = "THE REVOCABLE LIVING TRUST"
bMatchCase = True
Dim bRes As Boolean
bRes = rng.Find.Execute2007(text, bMatchCase)
MsgBox bRes

The REVOCABLE etc. text - and the execute - I also copied and pasted

And It showed True.

I'm not sure I'm competent to do it from VSTO but I must teach myself one
day. If I get a bit of free time I'll try to take a look at it.
 

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