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 2005

Tip: Looking for answers? Try searching our database.

Performing Repeated Actions Until End of Document is Reached

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
USAFA - 16 Feb 2005 20:31 GMT
I'm trying to find the text throughout a document and perform an action (go
to the beginning of the line & backspace 5 times) to take out space in my
document.  After it does this I want it to continue performing these steps
(loop I guess) throughout the document, but I don't want it to stop once it
reaches the end of the document.

I need to use this macro on documents that range from 2 pages to 50 pages in
length.  Please assist.  

Here's my code so far:

Sub Step1()
Dim oRng As Range
Set oRng = ActiveDocument.Range
       With Selection.Find
           .Text = "      *****************  PERSONAL"
           .Replacement.Text = ""
           .Forward = True
           .Wrap = wdFindContinue
           .Format = False
           .MatchCase = False
           .MatchWholeWord = False
           .MatchWildcards = False
           .MatchSoundsLike = False
           .MatchAllWordForms = False
           Selection.Find.Execute
           Selection.HomeKey Unit:=wdLine
           Selection.TypeBackspace
           Selection.TypeBackspace
           Selection.TypeBackspace
           Selection.TypeBackspace
           Selection.TypeBackspace
           Selection.MoveRight Unit:=wdCharacter, Count:=6
           Selection.EndKey Unit:=wdLine
       End With
End Sub
Ed - 16 Feb 2005 22:27 GMT
A couple of questions:
your post title says
  "Performing Repeated Actions Until End of Document is Reached"
while your post says
  > but I don't want it to stop once it
  > reaches the end of the document.
Do you want to stop when you reach the end of the document?
If  not, then what is your criteria for stopping?

Also, it looks like you recorded the macro, then went back and set oRng as a
Range object.  Once you've done that, you can use oRng.Find.  One thing to
remember when using Find - if you use Selection, your selection is changed
to the found text; if you use a Range, your range is redefined to the found
text.  In your case, oRng.Find would redefine your range from the entire doc
to just the found text.  In some ways, this is good - Range objects can be
easier to work with.

For instance, if at the beginning of each loop you set a range equal to oRng
  Set myRange = oRng
and used
  myRange.Find
myRange would redefine itself to encompass just the found text.  Then
  myRange.Collapse Direction:=wdCollapseStart

  myRange.Delete Unit:=wdCharacter, Count:=5
would delete the first five spaces and
  myRange.Collapse Direction:=wdCollapseEnd
would take you back to the end of the found text.

HTH
Ed

> I'm trying to find the text throughout a document and perform an action (go
> to the beginning of the line & backspace 5 times) to take out space in my
[quoted text clipped - 32 lines]
>         End With
> End Sub
Greg Maxey - 16 Feb 2005 22:45 GMT
USAFA,

Again, it seems to me that all you need is Find and Replace:

Sub Test()
Dim oRng As Range

Set oRng = ActiveDocument.Range

With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "      *****************  PERSONAL"
.Replacement.Text = "*****************  PERSONAL"
assignments"
.Execute Replace:=wdReplaceAll

End With
End Sub

Signature

Greg Maxey/Word MVP
A Peer in Peer to Peer Support

> A couple of questions:
> your post title says
[quoted text clipped - 69 lines]
>>         End With
>> End Sub
Ed - 16 Feb 2005 23:07 GMT
Yeah, Greg, that *is* a whole lot simpler than two ranges and looping and .
. .
I keep hoping I'll learn one of these days!  8>/

Ed

> USAFA,
>
[quoted text clipped - 89 lines]
> >>         End With
> >> End Sub
USAFA - 16 Feb 2005 23:31 GMT
Greg & Ed,

Sorry for the confusion & thanks for the help.  I actually DO want it to
stop performing the "find/move to line home/backspace 5x/move to line end"
actions once it reaches the end of the document.  I had edited the macro code
using some info Greg provided, but I think I wasn't clear in getting across
my question.  

To clarify, once I locate the text entitled "      *****************  
PERSONAL" then I am striking the HOME key to go to the beginning of the line.
Then I am deleting the 5 previous lines (not the spaces that precede the
***).  So I'm not performing a replace action but rather a series of spacing
and move actions after I locate the text.

Thanks again.
USAFA

> Yeah, Greg, that *is* a whole lot simpler than two ranges and looping and .
> .. .
[quoted text clipped - 95 lines]
> > >>         End With
> > >> End Sub
Greg Maxey - 17 Feb 2005 00:49 GMT
USAFA,

Try something like:

Sub ScratchMacro()
 ActiveDocument.Range(0, 0).Select
 With Selection.Find
   .ClearFormatting
   .Replacement.ClearFormatting
   .Text = "      *****************PERSONAL"
   .Forward = True
   .Wrap = wdFindStop
   .Format = False
   .MatchCase = False
   .MatchWholeWord = False
   .MatchWildcards = False
   .MatchSoundsLike = False
   .MatchAllWordForms = False
   Do While .Execute
     On Error Resume Next
     Selection.Collapse Direction:=wdCollapseStart
     Selection.MoveUp Unit:=wdLine, Count:=5
     Selection.MoveDown Unit:=wdLine, Count:=5, Extend:=wdExtend
     Selection.Delete
     Selection.MoveEndUntil Cset:=vbCr, Count:=wdForward
     Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
     If Selection.Range.End = ActiveDocument.Range.End Then
       ActiveDocument.Range(0, 0).Select
       Exit Sub
     End If
     Selection.MoveDown Unit:=wdLine, Count:=1
     Selection.Collapse wdCollapseEnd
   Loop
   ActiveDocument.Range(0, 0).Select
 End With

 End Sub

P.S.  Go Navy!!
Signature

Greg Maxey/Word MVP
A Peer in Peer to Peer Support

> Greg & Ed,
>
[quoted text clipped - 118 lines]
>>>>>         End With
>>>>> End Sub
USAFA - 17 Feb 2005 01:39 GMT
Greg,
I see what you're trying to do, but after copying it into a new macro &
running it, nothing happened.  I'm stumped!

Thoughts?
USAFA

> USAFA,
>
[quoted text clipped - 157 lines]
> >>>>>         End With
> >>>>> End Sub
Greg Maxey - 17 Feb 2005 01:51 GMT
USAFA,

I just copied it to a new document and it worked fine.  Do I have the spaces
and number of *'s right in the find string?

Signature

Greg Maxey/Word MVP
A Peer in Peer to Peer Support

> Greg,
> I see what you're trying to do, but after copying it into a new macro
[quoted text clipped - 168 lines]
>>>>>>>         End With
>>>>>>> End Sub
USAFA - 17 Feb 2005 03:57 GMT
Greg,

It finally worked.  I can't tell you how much that helped me!  Thanks again.

USAFA

> USAFA,
>
[quoted text clipped - 173 lines]
> >>>>>>>         End With
> >>>>>>> End Sub

Rate this thread:






 
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.