Hi again,
So, your routine
1) finds the @ symbol (bookmarks it) and moves one character at a time to
the RIGHT while performing a check
2) selets the bookmark (i.e., the @ symbol), move one character at a time to
the LEFT of the @ symbol while performing a check (and prefixes the new text
to the email address)
3) after testing for a valid email address, you move the selection to the
RIGHT so that it's at the very beginning of the email address.
4) You then run SdnFmt ("@") again (which I presume is finding the @ symbol)
That LOOKS like and endless loop because it would select the next @ symbol
in the document and the cursor is in front of the email address that you
just parsed, so you would always be finding the same @ symbol. However, I
don't know the details of SdnFmt ("@"), and if you say that it doesn't
create an endless loop, I will defer to your hands-on experience with your
routine(s).
However, if you're really trying to optimize how well your routine performs,
I would suggest that you use the range object to get your email address
instead of the selection object. In fact, you MIGHT be able to use a
wildcard search for email address (to get the whole address in the selection
and avoid having to extend the selection to the left or right). Make a copy
of one of the documents that has a lot of email addresses in it and see if
the following could work:
Dim oRng As Range
With Selection.Find
.ClearFormatting
.Text = "[A-z0-9.]{2,}\@[A-z0-9.]{2,}"
.MatchWildcards = True
Do While .Execute
Set oRng = Selection.Range
If Right(oRng.Text, 1) = "." Then
oRng.MoveEnd Unit:=wdCharacter, Count:=-1
End If
MsgBox oRng.Text
Loop
End With
If this meets your needs, it will be a lot more efficient than what you
currently have.
HTH,
Dave