Find and Delete all in the same page

S

Steved

Hello from Steved

The below macro works exactly as I require however I would like it to find
every instance on the same page and delete, at the moment it is only deleting
only one.

I have up to 20 per page what is reqiured please to make the below macro
carry out this function

Sub FindReplaceOnePage()
Dim rng As Word.Range

Set rng = ActiveDocument.Bookmarks("\Page").Range
With rng.Find
.Text = "R [0-9]{1,}:[0-9]{1,}:[0-9]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
Selection.Find.Execute
Selection.Delete Unit:=wdCharacter, Count:=1
End With
End Sub

Thankyou
 
M

macropod

Hi Steve,

Try:
Set rng = ActiveDocument.Bookmarks("\Page").Range
With rng.Find
.Text = "R [0-9]{1,}:[0-9]{1,}:[0-9]"
.Replacement.Text = ""
.Forward = True
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub

Note: I've deleted the line 'Selection.Delete Unit:=wdCharacter, Count:=1' as it shouldn't be within the With .. End With routine
and I'm not sure if you'll still need it with the above mods.
 
T

Tony Jollans

I'm sorry to say that your code works by accident. You set up all the
details of the Find using your Range, rng, and then you execute it using the
Selection, instead. You do define rng to be the page where the Selection is,
so there is some relation between the two but they do not, automatically,
look for the same thing, and you must have done something manually to have
it working at all.

Instead of:

Selection.Find.Execute

you should have, more simply:

.Execute

The delete also needs changing, but it would be better to replace with an
empty string rather than explicitly deleting, so remove the delete and
change the execute to:

.Execute Replace:=wdReplaceOne

Then, to make it change all occurrences on the page, requires a simple
change:

.Execute Replace:=wdReplaceAll



To sum up: replace:

Selection.Find.Execute
Selection.Delete Unit:=wdCharacter, Count:=1

with:

.Execute Replace:=wdReplaceAll
 
S

Steved

Hello Macropod

Thankyou

macropod said:
Hi Steve,

Try:
Set rng = ActiveDocument.Bookmarks("\Page").Range
With rng.Find
.Text = "R [0-9]{1,}:[0-9]{1,}:[0-9]"
.Replacement.Text = ""
.Forward = True
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub

Note: I've deleted the line 'Selection.Delete Unit:=wdCharacter, Count:=1' as it shouldn't be within the With .. End With routine
and I'm not sure if you'll still need it with the above mods.

--
Cheers
macropod
[MVP - Microsoft Word]


Steved said:
Hello from Steved

The below macro works exactly as I require however I would like it to find
every instance on the same page and delete, at the moment it is only deleting
only one.

I have up to 20 per page what is reqiured please to make the below macro
carry out this function

Sub FindReplaceOnePage()
Dim rng As Word.Range

Set rng = ActiveDocument.Bookmarks("\Page").Range
With rng.Find
.Text = "R [0-9]{1,}:[0-9]{1,}:[0-9]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
Selection.Find.Execute
Selection.Delete Unit:=wdCharacter, Count:=1
End With
End Sub

Thankyou
 
S

Steved

Hello Tony

Thankyou for the expanded explanation




Tony Jollans said:
I'm sorry to say that your code works by accident. You set up all the
details of the Find using your Range, rng, and then you execute it using the
Selection, instead. You do define rng to be the page where the Selection is,
so there is some relation between the two but they do not, automatically,
look for the same thing, and you must have done something manually to have
it working at all.

Instead of:

Selection.Find.Execute

you should have, more simply:

.Execute

The delete also needs changing, but it would be better to replace with an
empty string rather than explicitly deleting, so remove the delete and
change the execute to:

.Execute Replace:=wdReplaceOne

Then, to make it change all occurrences on the page, requires a simple
change:

.Execute Replace:=wdReplaceAll



To sum up: replace:

Selection.Find.Execute
Selection.Delete Unit:=wdCharacter, Count:=1

with:

.Execute Replace:=wdReplaceAll

--
Enjoy,
Tony

www.WordArticles.com

Steved said:
Hello from Steved

The below macro works exactly as I require however I would like it to find
every instance on the same page and delete, at the moment it is only
deleting
only one.

I have up to 20 per page what is reqiured please to make the below macro
carry out this function

Sub FindReplaceOnePage()
Dim rng As Word.Range

Set rng = ActiveDocument.Bookmarks("\Page").Range
With rng.Find
.Text = "R [0-9]{1,}:[0-9]{1,}:[0-9]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
Selection.Find.Execute
Selection.Delete Unit:=wdCharacter, Count:=1
End With
End Sub

Thankyou
 

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