How do I get a macro to stop at the bottom of the document using c

H

HowdeeDoodee

Many times I record a macro to do repeated actions. As an example, I record
the macro to do one set of actions and store the macro in Alt-5. To run the
macro down the page I keep hitting Alt-5. However, when I reach the end of
the document, the macro goes back to the top of the document and starts over
again. This is bad because of the text in the document. How do I stop a macro
from executing if the macro has reached the end of the document. Here is an
example of a multiple step macro I want to stop when the bottom of the page
is reached.

Selection.Find.ClearFormatting
With Selection.Find
.Text = "<p><a name="""
.Replacement.Text = "[GoTo]</a><br>^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
Selection.Cut
Selection.HomeKey Unit:=wdStory
ActiveWindow.ActivePane.SmallScroll Down:=4
Selection.Find.ClearFormatting
With Selection.Find
.Text = """>[GoTo]</a><br>"
.Replacement.Text = "[GoTo]</a><br>^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.TypeText Text:="}}}"
Selection.PasteAndFormat (wdPasteDefault)
Selection.Find.ClearFormatting
With Selection.Find
.Text = "</a><a href="
.Replacement.Text = "[GoTo]</a><br>^p"
.Forward = False
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
ActiveWindow.ActivePane.SmallScroll Down:=-1
Selection.Find.ClearFormatting
With Selection.Find
.Text = """>"
.Replacement.Text = "[GoTo]</a><br>^p"
.Forward = False
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
ActiveWindow.ActivePane.SmallScroll Down:=-2
Selection.Find.ClearFormatting
With Selection.Find
.Text = "<p><a name="""
.Replacement.Text = "[GoTo]</a><br>^p"
.Forward = False
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Find.ClearFormatting
With Selection.Find
.Text = "<a href=""#"
.Replacement.Text = "[GoTo]</a><br>^p"
.Forward = False
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
Selection.MoveUp Unit:=wdParagraph, Count:=1, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend
Selection.MoveRight Unit:=wdCharacter, Count:=1
End Sub
 
H

Helmut Weber

Hi,
How do I stop a macro from executing
if the macro has reached the end of the document.
I want to stop when the bottom of the page is reached.

End of page or end of document?

And too much code. Hardly possible to get from it,
what you want to do. And, moving the selection around
is not a good idea. Have you ever heard of ranges?

Plus:
Selection.HomeKey Unit:=wdStory
makes word to think you wanted to search all of the doc.

Plus:
Cut it all down into smaller pieces.

Sorry, can't post a beginners' book,
covering all, short, easy to understand and to read...

However, to get you going, a little example on
how to search and replace from the start of the
selection to the end of a page:

Sub Makro3()
Dim p As Long ' a position in the doc
ResetSearch
p = Selection.Start ' remember start of selection
ActiveDocument.Bookmarks("\page").Select
Selection.Start = p
With Selection.Find
.Text = "a"
.Replacement.Text = "b"
.Wrap = wdFindStop
.Execute Replace:=wdReplaceAll
End With
ResetSearch
End Sub
' ---
Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
' plus some more if needed
.Execute
End With
End Sub

HTH
Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.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