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.

Macro to Delete Range of text

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Brad - 23 Nov 2005 18:14 GMT
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
Greg - 23 Nov 2005 19:05 GMT
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
Doug Robbins - Word MVP - 23 Nov 2005 19:09 GMT
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

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.