How to delete a range of text base on keywords?

B

Brad

Hello all. I am trying to format a log file from ACL that is inserted into
Word. There is certain code when commented that we do not want to show up in
the Word document. I cannot seem to write a successful loop to do the
following:

1) Search for the phrase "COMMENT - Begin Delete" in the word document
2) Delete that phrase and any subsequent characters until it finds the next
phrase "COMMENT - End Delete" (inwhich that phrase also needs to be deleted)

My idea was to do something similar to below but I need it to be able to
delete the range multiple times if it appears.

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

Thanks for any help you may be able to provide!
-Brad
 
D

Doug Robbins - Word MVP

You don't really need a macro to do it. Use Edit>Replace and click on the
More button and then check the "Use Wildcards" box and enter

COMMENT - Begin Delete * COMMENT - End Delete

into the Find what control and leave the Replace with control empty and then
click on Replace All.

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

Brad

Doug, thanks for the response. However, I will need to create a macro for
this. We run a macro that kicks off about 30 macros in turn to format our
log files a certain way in MS Word. This is another feature that has been
requested of me to add.

Thanks!
Brad
 
B

Brad

I tried out the wildcard search and it worked, but only if the 2 phrases were
on the same line. I should have mentioned that these phrases will occur
around blocks of code that we want deleted.

Any further help would be appreciated developing this macro!
-Brad
 

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