Exit Find if string not found

W

WordCrafter

I used the macro recorder to capture the keystrokes of finding a string.
Once it finds the string, it deletes from the point of the start of the
string to end of the document.

When the string exists, the macro works correctly. When the text does
not exist, the WHOLE document gets deleted. I'm trying to figure out how
to capture the string-not-found condition and cause the routine to exit
without completing the deletion.

Private Sub RemoveFooter()
'
' RemoveFooter Macro
' Macro recorded 11/2/2004 by bs2829
'
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
..Text = "From this point to EOF"
..Replacement.Text = ""
..Forward = True
..Wrap = wdFindContinue
..Format = False
..MatchCase = False
..MatchWholeWord = False
..MatchWildcards = False
..MatchSoundsLike = False
..MatchAllWordForms = False
End With

Selection.Find.Execute
Selection.EndKey Unit:=wdStory, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1

End Sub
 
W

WordCrafter

Never mind.... it took some playing but I figured out the solution.

Private Sub RemoveFooter()
'
' RemoveFooter Macro
' Macro recorded 11/2/2004 by bs2829
'
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "From this point to EOF."
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

footer = "From this point to EOF."

Selection.Find.Execute
Found = Selection.Text

If footer <> Found Then End

Selection.EndKey Unit:=wdStory, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1

End Su
 
H

Helmut Weber

Hi,
still a bit messy ;-)
Sub Test()
Dim rDoc As Range
Set rDoc = ActiveDocument.Range
ResetSearch
With rDoc.Find
.Text = "design"
If .Execute Then
rDoc.End = ActiveDocument.Range.End
rDoc.Delete
End If
End With
ResetSearch
End Sub
' ---
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
.Execute
End With
End Sub
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.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