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 / November 2004

Tip: Looking for answers? Try searching our database.

Exit Find if string not found

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
WordCrafter - 05 Nov 2004 02:31 GMT
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

---
Message posted from http://www.ExcelForum.com/
WordCrafter - 05 Nov 2004 02:55 GMT
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

--
Message posted from http://www.ExcelForum.com
Helmut Weber - 05 Nov 2004 07:28 GMT
Hi,
if .execute then
would be sufficient.
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
Helmut Weber - 06 Nov 2004 12:37 GMT
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/
 
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.