#1
|
|||
|
|||
Selecting text between two key phrases
Hi
I'm using this code to select text between two key phrases ; Code:
Dim myrng As Range, endrng As Range Selection.HomeKey wdStory Selection.Find.ClearFormatting With Selection.Find Do While .Execute(findText:="MORE INFO ABOUT THE ITEM ", MatchCase:=True, Forward:=True, Wrap:=wdFindStop) = True Set myrng = Selection.Range Loop End With Selection.HomeKey wdStory Selection.Find.ClearFormatting With Selection.Find Do While .Execute(findText:="ITEM NUM : ", MatchCase:=True, Forward:=True, Wrap:=wdFindStop) = True Set endrng = Selection.Range Loop End With myrng.End = endrng.End myrng.Select I'd like it to select all of the specific occurences in the passage. If there are several times when the two inputs phrases can be found then it should find and select all occurences discretely. Can someone advise with some modification or alternative code? Grateful for any help. Last edited by macropod; 06-24-2012 at 04:29 PM. Reason: Added code tags & formatting |
#2
|
||||
|
||||
VBA cannot select discontiguous ranges - you can only process discontiguous ranges iteratively.
PS: When posting code, please use the code tags - they're on the Advanced screen.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
HI
Ok thanks for getting back. I'm trying to use VBA to find the text including and between 'MORE INFO ABOUT THE ITEM ' and 'ITEM NUM : ' , and then delete it. My problem is that there are several times when this occurs , so I need each occurrence to be identified and deleted. Either all at once , or one after another. Can this be done? I'd be grateful if you could suggest some code to help. The code would need to run through and find the first section between the string statements and delete. Then search for any more , and delete as it finds them. I'm trying to tidy some text which has these phrases repeated throughout and I'm looking ofr a way to weed them all out at a go. Thanks again |
#4
|
||||
|
||||
You can do it with or without vba, using a wildcard Find/Replace, where:
Find = "MORE INFO ABOUT THE ITEM*ITEM NUM : " Replace = ""
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Hi
Ok thanks. I gave it a go using search replace , and unfortunately it doesn't find the string. I'm not sure why. This is what I'm running : Code:
Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find .Text = "MORE INFO ABOUT THE ITEM*ITEM NUM : " .Replacement.Text = "" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchAllWordForms = False .MatchSoundsLike = False .MatchWildcards = True End With Selection.Find.Execute Replace:=wdReplaceAll Selection.Find.Execute Selection.Find.Execute Replace:=wdReplaceAll Item Format UP € - EUR Qty Availability Item num : 112027bx41 Bowie - Jagger - Dancing In The Street - 12 inch 45 rpm - EX/EX more info about the item [ Inventory : 0 ] 12 inch 45 rpm 5.95 1 Available Item num : 25093bx180 Jagger Mick - Let's Work - 7inch (SP) x 2 - EX/EX more info about the item [ Inventory : 0 ] 7inch (SP) x 2 2.95 1 Available Item num : 116699bx102 Jagger Mick - Just Another Night - 7inch (SP) x 2 - NA/EX more info about the item [ Inventory : 0 ] I can't see why this wouldn't find the sections in the Find/Replace dialogue... |
#6
|
||||
|
||||
Without wanting to place too fine a point on it, your wildcard Find text isn't the same case as what you're trying to find. FWIW, 'Match Case' is of no effect in a wildcard Find.
Also, if you only want to delete the lines starting with 'more info about the item ', use: Find = "more info about the item[!^13]@^13"
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Hi
Ok thanks again for that. I've got it working fine now. Thanks for your time and expertise (and patience!) - I'm grateful. Best Wishes |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Question about selecting text w/mouse | Jazz43 | Word | 3 | 11-29-2011 11:58 PM |
Selecting styled text | Caroline | Word | 5 | 02-15-2011 12:55 PM |
Automatically Hyperlink phrases | TinaIgnatiev | Word | 1 | 12-22-2010 01:42 PM |
Selecting specific text out of a series of columns | speedycorn1 | Word | 3 | 11-01-2010 02:58 PM |
Selecting a Text Box | gajesh | Word | 0 | 09-02-2009 11:45 PM |