Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-24-2012, 02:51 PM
Chayes Chayes is offline Selecting text between two key phrases Windows XP Selecting text between two key phrases Office 2003
Advanced Beginner
Selecting text between two key phrases
 
Join Date: May 2012
Posts: 79
Chayes is on a distinguished road
Default 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
Unfortunately this only selects the first occurence and no others in the text. If there is more than one occurence the code selection spans whole tract of text.

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
Reply With Quote
  #2  
Old 06-24-2012, 04:32 PM
macropod's Avatar
macropod macropod is offline Selecting text between two key phrases Windows 7 64bit Selecting text between two key phrases Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 06-24-2012, 04:46 PM
Chayes Chayes is offline Selecting text between two key phrases Windows XP Selecting text between two key phrases Office 2003
Advanced Beginner
Selecting text between two key phrases
 
Join Date: May 2012
Posts: 79
Chayes is on a distinguished road
Default

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
Reply With Quote
  #4  
Old 06-24-2012, 04:50 PM
macropod's Avatar
macropod macropod is offline Selecting text between two key phrases Windows 7 64bit Selecting text between two key phrases Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #5  
Old 06-24-2012, 05:40 PM
Chayes Chayes is offline Selecting text between two key phrases Windows XP Selecting text between two key phrases Office 2003
Advanced Beginner
Selecting text between two key phrases
 
Join Date: May 2012
Posts: 79
Chayes is on a distinguished road
Unhappy

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
This is a section of the text I'm trying to affect ;

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...

Reply With Quote
  #6  
Old 06-24-2012, 06:40 PM
macropod's Avatar
macropod macropod is offline Selecting text between two key phrases Windows 7 64bit Selecting text between two key phrases Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #7  
Old 06-24-2012, 06:54 PM
Chayes Chayes is offline Selecting text between two key phrases Windows XP Selecting text between two key phrases Office 2003
Advanced Beginner
Selecting text between two key phrases
 
Join Date: May 2012
Posts: 79
Chayes is on a distinguished road
Default

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
Reply With Quote
Reply



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
Selecting text between two key phrases Automatically Hyperlink phrases TinaIgnatiev Word 1 12-22-2010 01:42 PM
Selecting text between two key phrases 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

Other Forums: Access Forums

All times are GMT -7. The time now is 09:25 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft