Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2005

Tip: Looking for answers? Try searching our database.

How to delete a range of text base on keywords?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Brad - 22 Nov 2005 17:42 GMT
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
Doug Robbins - Word MVP - 22 Nov 2005 19:36 GMT
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.

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 trying to format a log file from ACL that is inserted
> into
[quoted text clipped - 46 lines]
> Thanks for any help you may be able to provide!
> -Brad
Brad - 22 Nov 2005 20:22 GMT
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

> 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
[quoted text clipped - 54 lines]
> > Thanks for any help you may be able to provide!
> > -Brad
Brad - 22 Nov 2005 22:20 GMT
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

> 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
[quoted text clipped - 54 lines]
> > Thanks for any help you may be able to provide!
> > -Brad

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.