Problem in While loop

J

Joanne

I wrote the following code to get rid of extraneous characters at the end of
a document. Once I get the "Move Start" working correctly, I want to delete
any character at the end of the document that is not an actual A-z character.
I am testing the macro on the numbers "1234". The macro works fine with the
"4" and the "3". The value of rng becomes first "4" and then "34" but the
"Like" comparison doesn't seem to see the "2" as a non alphabetic character
because as soon as it passes the "Like" line, it jumps out of the while loop.
I can't see what I'm doing wrong. Thanks so much for your help.

Joanne

Sub FixEndSemis()
Dim rng As Range
Set rng = ActiveDocument.Range
With rng
.Collapse wdCollapseEnd
.MoveStart Unit:=wdCharacter, Count:=-1
While .Text Like "[!A-Za-z]"
.MoveStart Unit:=wdCharacter, Count:=-1
'.Select
Wend
End With
 
J

Jean-Guy Marcil

Joanne was telling us:
Joanne nous racontait que :
I wrote the following code to get rid of extraneous characters at the
end of a document. Once I get the "Move Start" working correctly, I
want to delete any character at the end of the document that is not
an actual A-z character. I am testing the macro on the numbers
"1234". The macro works fine with the "4" and the "3". The value of
rng becomes first "4" and then "34" but the "Like" comparison doesn't
seem to see the "2" as a non alphabetic character because as soon as
it passes the "Like" line, it jumps out of the while loop. I can't
see what I'm doing wrong. Thanks so much for your help.

Try this:

'_______________________________________
Sub FixEndSemis()

Dim rng As Range
Set rng = ActiveDocument.Range

With rng
.Collapse wdCollapseEnd
While .Characters.First Like "[!A-z]"
.MoveStart Unit:=wdCharacter, Count:=-1
Wend
.MoveStart Unit:=wdCharacter, Count:=1
.Delete
End With

End Sub
'_______________________________________

Here is how I see the problem with your code: (By the way, it has nothing to
do with the "2" digit...)
Sub FixEndSemis()
Dim rng As Range
Set rng = ActiveDocument.Range
With rng
.Collapse wdCollapseEnd

Select the last character in the document
.MoveStart Unit:=wdCharacter, Count:=-1

Make sure it non-alphabetic, by the way, I think you can use A-z
While .Text Like "[!A-Za-z]"

If it isn't, select he preceding character
.MoveStart Unit:=wdCharacter, Count:=-1
'.Select

Go back to the begining of the loop

This is the problem, now you have 2 characters that you are trying to match
to a single string pattern. Of course it does not match and the loop is
exited.

At least, this is how I understand it. SO my code compares one character at
the time.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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