Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2005

Tip: Looking for answers? Try searching our database.

Problem in While loop

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Joanne - 10 Mar 2005 05:01 GMT
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
Jean-Guy Marcil - 10 Mar 2005 06:16 GMT
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
[quoted text clipped - 5 lines]
> 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
>    Wend

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.
> End With

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

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.