Macro to Delete Range of text

B

Brad

Hello all. I am working on a macro to delete a range of code from a log file
inserted into MS Word based on beginning and ending keywords. We currently
have a template that kicks off 30 other macros and this feature has been
requested of me to add. What I need to do is the following:

Deleting the followng block of text:

COMMENT - Begin Delete (<---key phrase)
Delete this block of text and
and any subsequent lines until you
run into the the key phrase below
COMMENT - End Delete (<--key phrase)

The key phrases "COMMENT - Begin Delete" and "COMMENT - End Delete" need to
be deleted as well.

I thought I had a good start using the code below but I need to figure out
how to loop it correctly.

Thanks for any help you could provide as the deadline for this is by tomorrow!

Selection.Find.ClearFormatting
With Selection.Find
.Text = "COMMENT - Begin Delete"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Extend
Selection.ClearFormatting
With Selection.Find
.Text = "COMMENT - End Delete"
.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
Selection.HomeKey Unit:=wdStory
 
G

Greg

Brad,

The "-" seems to be giving me fits and I don't have time to work it
out.

Try this:
Sub ScratchMacro()
Dim oRng As Range
Set oRng = ActiveDocument.Content
With oRng.Find
.ClearFormatting
.Text = "COMMENT*Begin Delete*COMMENT*End Delete"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub
 
D

Doug Robbins - Word MVP

Use the following:

Dim drange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="COMMENT - Begin Delete",
MatchWildcards:=False, Wrap:=wdFindContinue, Forward:=True) = True
Set drange = Selection.Range
drange.End = ActiveDocument.Range.End
drange.End = drange.Start + InStr(drange, "COMMENT - End Delete") +
20
drange.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
 
T

Tony Jollans

Find "COMMENT - Begin Delete?{1,}COMMENT - End Delete" (without the quotes)
Replace with (blank)
Check Use Wildcards
Hit Replace all
 
B

Brad

Doug, the code works perfect. You are the man! Thanks again for your help.
Greg thanks your reply as well! Most appreciated guys!
 

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