p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Word VBA (http://p2p.wrox.com/forumdisplay.php?f=231)
-   -   Deleting variable ranges based on keyword (http://p2p.wrox.com/showthread.php?t=88446)

qrobrets September 14th, 2012 03:14 PM

Deleting variable ranges based on keyword
 
So basically what I'm looking to do is sort a long document of text entries in the following format:

Name:XXXXXXXXXXXXX...
XXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXX
XXXXXKeywordXXXXXXXX
XXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXLocation:

each separated by one or more spaces.

What I want to do is delete between "Name" and "Location: " if the keyword is found in the body of text. There may be zero, one or many instances of the keyword throughout the document.

What I have so far is listed below. It works, but only if the keyword is within the very first body of text and it will keep deleting any contiguous keyword containing paragraphs after that. But, it does nothing if the keyword is not in the first body of text.
----------------------------------
Set range1 = ActiveDocument.Content
Set range2 = ActiveDocument.Content
Set range3 = ActiveDocument.Content
range1.Find.Execute FindText:="Name", _
Forward:=True
range2.Find.Execute FindText:="Location: ", _
Forward:=True
If range1.Find.Found = True And range2.Find.Found = True Then
range3.SetRange (range1.End - 4), (range2.End + 1)
End If

range3.Find.Execute FindText:="Orthopaedic", _
Forward:=True

If range3.Find.Found = True Then
range3.SetRange (range1.End - 4), (range2.End + 1)
range3.Delete
End If
----------------------------------------------------

I would greatly appreciate any help y'all would see fit to offer.

Thanks!

Q

Shasur March 18th, 2013 12:56 PM

Hi

One simple option is to use Regular Expressions (Regex) and find the pattern and then delete it

Else you can place Bookmarks for both words and delete the Range that is between those bookmarks

Cheers
Shasur


All times are GMT -4. The time now is 11:50 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.