Find Error?

G

George Lee

When using VBA code to find "U.S." (for example), the Find range returns four
words with the first three being as expected ("U", ".", "S").

However, the last word may contain not only the final period but also the
next character in the document if the next one is a space or another
puncuation mark such as ".)". The user interface version of the Find does not
do this.

Is the code version a bug or have I overlooked something?
 
G

Greg

George,

I don't see that using:

Sub Test()
Dim oRng As Range
Set oRng = ActiveDocument.Content
With oRng.Find
.ClearFormatting
.Text = "U.S."
.Forward = True
.Wrap = wdFindStop
Do While .Execute
MsgBox oRng.Text
Loop
End With
End Sub

What is your code?
 
G

George Lee

Looks to be the same:

With myRange.Find
.ClearFormatting
.Text = "U.S."
.Forward = True
.MatchWholeWord = False
.MatchCase = True
End With

However, the Find comes out with four words: "U", ".", "S", ".?"
 
G

George Lee

I'm not determining what is or isn't a word. I'm using the
myFindRange.Words(x) from the Find results.
 
T

Tony Jollans

Yes, when you use VBA - and you are getting what I would expect (although
not necessarily what I would like).

But what makes you say anything is different when using the UI?

Actually, I think I may know what you're not understanding. When a Range
intersects with a Word, the complete word (or what Word considers the
complete word) is returned from any request for the word. The Found
Selection contains only four characters (U.S.) but the fourth 'word' is only
a partial word (the period from period-space) and the whole word is what you
get for word(4).
 

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