Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Other Office > Word VBA
Word VBA Discuss using VBA to program Word.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Word VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old September 14th, 2012, 03:14 PM
Registered User
Join Date: Sep 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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:


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", _
range2.Find.Execute FindText:="Location: ", _
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", _

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

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


Old March 18th, 2013, 12:56 PM
Friend of Wrox
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts


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

C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)

Similar Threads
Thread Thread Starter Forum Replies Last Post
variable space deleting between the tags rajashekhara XSLT 2 December 18th, 2008 12:45 AM
variable space deleting between the tags rajashekhara XSLT 5 December 17th, 2008 07:17 AM
deleting records based on selected checkbox lakshmi_annayappa Javascript 3 September 6th, 2007 02:22 AM
Variable data Ranges dgarcia1128 Excel VBA 3 June 20th, 2003 02:11 PM
Variable data ranges for charts Cclhodges Excel VBA 1 June 12th, 2003 02:18 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.