.Next feature

G

Greg Maxey

In the following code why does the line MsgBox aWord.Next return just the
first letter of the next word rather than the complete word? Thanks

Sub Testing()
Dim aWord
For Each aWord In ActiveDocument.Range.Words
MsgBox aWord
MsgBox aWord.Next
Next aWord
End Sub
 
V

Vince

Greg,

I am no expert but I think when you say "aWord.Next" it moves to the next
character by default. This code works:

Dim aWord As Range
For Each aWord In ActiveDocument.Range.Words
MsgBox "BEF: " & aWord & " AFT: " & aWord.Next(unit:=wdWord, Count:=1)
Next aWord

Vince
 
G

Greg Maxey

Vince,

You come along with a suggestion that works and you are "expert" in my eyes.
Thanks.

Another user and I where discussing using OR in a find text problem. The
discussion turned to a very real human situation where the house is locked
and the car is locked. A known spare house key is in the car and a known
spare car hey is in the house. There are other keys somewhere to be found.
Obviously the problem is resolved and the search can end when either a house
or car key is found.

Cheers

Sub HelmutsDilemma()
Dim aWord As Range
Dim myStr As String
For Each aWord In ActiveDocument.Range.Words
myStr = aWord
If InStrRev(myStr, " ") = Len(myStr) Then
myStr = Left(myStr, (Len(myStr) - 1))
End If
Select Case myStr
Case Is = "car", "house"
If aWord.Next(Unit:=wdWord, Count:=1) = "keys " Or _
aWord.Next(Unit:=wdWord, Count:=1) = "keys" Then
With aWord
.MoveEnd Unit:=wdWord, Count:=1
.Select
End With
Exit Sub
End If
Case Else
'Do Nothing
End Select
Next
End Sub
 
V

Vince

Ah, so it's Helmut Weber's dilemma (he has helped me on many occasions).

Well, I am sure about the human approach to this, which is to break open the
car's window and get the house key. Later, you could get insurance to pay
for the window!

Vince
 
H

Helmut Weber

Hi everybody,

it seems to me, searching for "this" OR "that"
in a purist sense is impossible. One might model
the human way, as Greg did, or use brute force:

Sub FindFirstOr(strA As String, strB As String)
Dim rDcm As Range
Dim rTmp1 As Range
Dim rTmp2 As Range

Set rDcm = ActiveDocument.Range
' ResetSearch
With rDcm.Find
.Text = strA
If .Execute Then Set rTmp1 = rDcm
End With
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = strB
If .Execute Then Set rTmp2 = rDcm
End With
If rTmp1 Is Nothing And rTmp2 Is Nothing Then
MsgBox "nothing found"
Exit Sub
End If
If rTmp1 Is Nothing Then
rTmp2.Select
Exit Sub
End If
If rTmp2 Is Nothing Then
rTmp1.Select
Exit Sub
End If
If rTmp1.start < rTmp2.start Then
rTmp1.Select
Else
rTmp2.Select
End If
' ResetSearch
End Sub
' -------
Sub test000()
Call FindFirstOr("Housekey", "Carkey")
End Sub

Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 

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