Selecting text between 2 found words in Word

R

Richy

Hi i want to select some text throughout a document to be deleted. The
test will look like this......

'Christmas : 2 x 5" Fruit & Cone Candle Rings Product: Christmas 2 x
5" Fruit & Cone Candle Rings Custom Label: 213878'

I want to select the word 'Product' and everything up to (but NOT
including) the word Label and then delete that selection.

I want this to loop through the document until there are no more
Products to be found

Any help is appreciated
 
G

Greg Maxey

Richy,

You don't need a macro for this. You could do it directly from the
find and replace dialog:
Sub Scratchmacro2()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Content
With oRng.Find
.Text = "Product*Label"
.MatchWildcards = True
.Replacement.Text = "Label"
.Execute Replace:=wdReplaceAll
End With
End Sub
 
J

John Doue

Greg said:
Richy,

You don't need a macro for this. You could do it directly from the
find and replace dialog:
Sub Scratchmacro2()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Content
With oRng.Find
.Text = "Product*Label"
.MatchWildcards = True
.Replacement.Text = "Label"
.Execute Replace:=wdReplaceAll
End With
End Sub
Greg,

Your answer makes me realize once again how little I know about the
Search&Replace function. The sites I know about this are neither
exhaustive nor user friendly (meaning clear to understand for a
beginner). Hence my first question, can you suggest a fairly
comprehensive yet clear site ?

Second, to elaborate on the above macro, what if the user wanted a macro
prompting him for the first and then the last words, as here, "Product"
and "label" and then 2hich would display the number of replacement?

Regards
 
G

Greg Maxey

John,

Again you don't need a macro to do this either. After reviewing Graham's
article I think you will see why. Here is the code that would do that for
you:

Sub Scratchmacro()
Dim oRng As Word.Range
Dim sFind1 As String
Dim sFind2 As String
Dim Count As Long
Set oRng = ActiveDocument.Content
sFind1 = InputBox("Type leading word to find.", "Leading Word")
sFind2 = InputBox("Type trailing word to find.", "Trailing Word")
With oRng.Find
.Text = sFind1 & "*" & sFind2
.MatchWildcards = True
While .Execute
oRng = sFind2
oRng.Collapse wdCollapseEnd
Count = Count + 1
Wend
End With
MsgBox Count
End Sub
 
J

John Doue

Greg said:
John,

Again you don't need a macro to do this either. After reviewing Graham's
article I think you will see why. Here is the code that would do that for
you:

Sub Scratchmacro()
Dim oRng As Word.Range
Dim sFind1 As String
Dim sFind2 As String
Dim Count As Long
Set oRng = ActiveDocument.Content
sFind1 = InputBox("Type leading word to find.", "Leading Word")
sFind2 = InputBox("Type trailing word to find.", "Trailing Word")
With oRng.Find
.Text = sFind1 & "*" & sFind2
.MatchWildcards = True
While .Execute
oRng = sFind2
oRng.Collapse wdCollapseEnd
Count = Count + 1
Wend
End With
MsgBox Count
End Sub
Thanks Greg!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top