RE: macro to repeat commands until end of document by hayes
hayes
Tue Apr 17 22:54:00 CDT 2007
Bear,
The Paginator, Bear's MS version of the Terminator
"I'll be Bach, you be Beethoven!"
"Write your own code or I will Paginate YOU."
LOL
--
Chris Hayes
Still a beginner (only 12 years)
"Bear" wrote:
> ka2cil:
>
> If you let me write all your code, no matter how pressing your business
> needs, you're doing yourself a disservice. I 'm going on with the provision
> that you promise to find out the difference between a Range and a Selection.
>
> IF the key text always begins within the first ten characters in the
> document, you can avoid looking there with your Find. You'd add a line to
> move the starting point of the range (that part of the document you're
> searching) ahead ten (or whatever)characters.
>
> Here's the original line, and the line you'd need to add.
>
> ~~~~~
> Set objDocumentRange = ActiveDocument.Range
> objDocumentRange.Start = 10
> ~~~~~
>
> IF you can't guarantee that the text falls at the beginning, but you know
> you ALWAYS want to discard the first hit, you'll have to do two finds. One to
> locate the first instance so you can move the start of objDocumentRange, and
> another to globally replace (within what's left of objDocumentRange).
>
> Try this:
>
> ~~~~~
> Sub Paginator()
>
> Dim objDocumentRange As Range
>
> ' Set the range (search area) to the entire
> ' document
> Set objDocumentRange = ActiveDocument.Range
>
> ' Find the first instance, this resets the
> ' range to the text found. Collapse the range
> ' then re-extend the end to the document end.
> With objDocumentRange.Find
> .Text = "Heading"
> .Execute
> ' Is the text found?
> If .Found = True Then
> ' Collapse then expand the range
> ' to search from the first instance
> ' to the end
> objDocumentRange.Collapse Direction:=wdCollapseEnd
> objDocumentRange.End = ActiveDocument.Range.End
> Else
> ' If there's no text, display a message and exit
> MsgBox Prompt:="No special text was found.", _
> Buttons:=vbExclamation, _
> Title:="Paginator"
> Set objDocumentRange = Nothing
> Exit Sub
> End If
> End With
>
> ' Replace everything in the new range (which
> ' now excludes the first instance
> With objDocumentRange.Find
> .Text = "Heading"
> .Replacement.Text = "^mHeading"
> .Execute Replace:=wdReplaceAll
> End With
>
> Set objDocumentRange = Nothing
>
> End Sub
> ~~~~~
>
> Bear