run macro if text is found

  • Thread starter gaubahn via OfficeKB.com
  • Start date
G

gaubahn via OfficeKB.com

Hi,

How do i program a macro to run only if certain words are found within a
document. Below is my code (quite unelegant but its the best i can come up
with given my limited VB know-how :]) )

Sub DeleteRemittance()
' Delete Remittance Page
' Macro added on 12/3/2008 by bmsvbs
'
Selection.Find.ClearFormatting
With Selection.Find
.Text = "DELETE PAGE"
.Font.Bold = True
.Font.Size = 1
.Font.Name = "Arial"
.MatchCase = True
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute
Selection.GoTo What:=wdGoToBookmark, Name:="\section"
Selection.Find.ClearFormatting
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.GoTo What:=wdGoToBookmark, Name:="\section"
Selection.Find.ClearFormatting
Selection.TypeBackspace
Selection.TypeBackspace
Selection.TypeBackspace
End Sub

The problem with this code is even if the "DELETE PAGE" is not found it still
performs the Selection.TypeBackspace command.

Any ideas for a work around is greatly appreciated.

Thanks and regards,
gau
 
D

Doug Robbins - Word MVP

To delete any page on which the words DELETE PAGE appear, use:

Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(Findtext:="DELETE PAGE", Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=True) = True
Selection.Bookmarks("\page").Range.Delete
Loop
End With


--
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
 
G

gaubahn via OfficeKB.com

Hi Doug,

Worked like a charm!!!

Thanks and regards,
gau
To delete any page on which the words DELETE PAGE appear, use:

Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(Findtext:="DELETE PAGE", Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=True) = True
Selection.Bookmarks("\page").Range.Delete
Loop
End With
[quoted text clipped - 35 lines]
Thanks and regards,
gau
 

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