Use:
Dim Found As Boolean
Found = False
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="SearchString", Forward:=True, _
MatchWildcards:=False, MatchCase:=True, Wrap:=wdFindStop) = True
Found = True
Selection.Delete
Loop
End With
If Found = False Then
MsgBox "SearchString does not appear in the document."
End If
--
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP
"Alan Stancliff"
news:epiLlradIHA.6092@TK2MSFTNGP06.phx.gbl...
> Hi all,
>
> In Word 2003, it is possible to write a VBA macro that searches for a
> string, positions the cursor at the string found, deletes the string,
> leaving the cursor where the string had been located. Such code might look
> like this:
>
> Selection.Find.ClearFormatting
> With Selection.Find
> .Text = "searchstring"
> .Replacement.Text = ""
> .Forward = True
> .Wrap = wdFindContinue
> .Format = False
> .MatchCase = False
> .MatchWholeWord = False
> .MatchWildcards = False
> .MatchSoundsLike = False
> .MatchAllWordForms = False
> End With
> Selection.Find.Execute
> Selection.Delete Unit:=wdCharacter, Count:=1
>
> However, if the string is not found, the macro deletes the character the
> cursor is resting on at the beginning of the search.
>
> What I want to know is if there is a way to test if the string is not
> found, so that another action can be taken.
>
> Such code might look like this:
>
> Selection.Find.ClearFormatting
> ' on not found, go to label NoSuchStringExists
> With Selection.Find
> .Text = "searchstring"
> .Replacement.Text = ""
> .Forward = True
> .Wrap = wdFindContinue
> .Format = False
> .MatchCase = False
> .MatchWholeWord = False
> .MatchWildcards = False
> .MatchSoundsLike = False
> .MatchAllWordForms = False
> End With
> Selection.Find.Execute
> Selection.Delete Unit:=wdCharacter, Count:=1
> Exit Sub
>
> NoSuchStringExists:
> MsgBox "Sorry, I could not find this string"
>
> Regards,
>
> Alan Stancliff
>