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
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
Tony Jollans - 23 Nov 2005 19:58 GMT
Find "COMMENT - Begin Delete?{1,}COMMENT - End Delete" (without the quotes)
Replace with (blank)
Check Use Wildcards
Hit Replace all
--
Enjoy,
Tony
> Brad,
>
[quoted text clipped - 14 lines]
> End With
> End Sub
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

Signature
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
> Hello all. I am working on a macro to delete a range of code from a log
> file
[quoted text clipped - 52 lines]
> Selection.Delete
> Selection.HomeKey Unit:=wdStory
Brad - 23 Nov 2005 21:17 GMT
Doug, the code works perfect. You are the man! Thanks again for your help.
Greg thanks your reply as well! Most appreciated guys!
> Use the following:
>
[quoted text clipped - 68 lines]
> > Selection.Delete
> > Selection.HomeKey Unit:=wdStory