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 / February 2008

Tip: Looking for answers? Try searching our database.

Keep this from going out of range?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ed from AZ - 04 Feb 2008 17:36 GMT
I have code which is supposed to find instances of 2 or more spaces
and replace them with one space.  It's supposed to work within a set
range, but it keeps going out of the range all the way to the end of
the document.  How do I stop it at the end of the range?

Ed

With oRng.Find
 .ClearFormatting
 .Text = "[! ] {2,}[! ]"
 .MatchWildcards = True
 While .Execute
   Select Case oRng.Characters.First.Text
     Case Is = "."
       oRng.Text = oRng.Characters.First & "  " &
oRng.Characters.Last
     Case Is = ":"
       oRng.Text = oRng.Characters.First & "  " &
oRng.Characters.Last
     Case Is = "-"
       oRng.Text = oRng.Characters.Last
     Case Else
       oRng.Text = oRng.Characters.First & " " & oRng.Characters.Last
   End Select
   oRng.Collapse Direction:=wdCollapseEnd
 Wend
End With
Jay Freedman - 04 Feb 2008 18:33 GMT
Hi Ed,

That's a pain, isn't it? :-S  It's one of a number of situations where VBA
behaves differently from the same operation in the UI.

Declare another Range object, say

  Dim oContainRange As Range
  Set oContainRange = Selection.Range

Then change the .Execute line to

  While .Execute And oRng.InRange(oContainRange)

The second part of the condition will be false if oRg is outside
oContainRange.

Signature

Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

> I have code which is supposed to find instances of 2 or more spaces
> and replace them with one space.  It's supposed to work within a set
[quoted text clipped - 23 lines]
>  Wend
> End With
Ed from AZ - 04 Feb 2008 19:43 GMT
Beautiful!!  Thank you, Jay!  I didn't know about that InRange - I
always tried something like setting another range and comparing the
ends or such.  This is going to make a _lot_ of things much easier!

Ed

> Hi Ed,
>
[quoted text clipped - 49 lines]
>
> - Show quoted text -
 
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.