Word Macro Help--Should be easy--I hope

ineedadedt

Board Regular
Joined
Jan 7, 2004
Messages
163
Hi all,

I need to clean up this code that I recorded from the Macro Recorder. I need it to basically delete the words "Slide" and the number associated with it. So depending on the file, I might have Slide 1, Slide 2, Slide 3, or I could have Slide 50, Slide 51 etc.

I realized when I run the code, I am deleting some very important numbers in a previous Proprietary Statment that has the number 1 in the code below. Any suggestions are greatly appreciated.

Thanks in advance!

Sub Step2DeleteSlideName()

' Delete the words Slide X
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Slide 1"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "Slide 2"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "2"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "Slide 3"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "Slide 4"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "Slide 5"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "Slide 6"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "Slide 7"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "Slide 8"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "9"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "0"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "1"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.WholeStory
Selection.EscapeKey
Selection.WholeStory
End Sub

Thanks,
Eric
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Have you tried using a for loop? You could loop from, say, "i = 1 to 100" and then do something like this:

Code:
[COLOR=#333333]
' Delete the words Slide i[/COLOR]
[COLOR=#333333]Selection.Find.Replacement.ClearFormatting[/COLOR]
[COLOR=#333333]With Selection.Find[/COLOR]
[COLOR=#333333].Text = "Slide " & i[/COLOR]
[COLOR=#333333].Replacement.Text = ""[/COLOR]
[COLOR=#333333].Forward = True[/COLOR]
[COLOR=#333333].Wrap = wdFindContinue[/COLOR]
[COLOR=#333333].Format = False[/COLOR]
[COLOR=#333333].MatchCase = False[/COLOR]
[COLOR=#333333].MatchWholeWord = False[/COLOR]
[COLOR=#333333].MatchWildcards = False[/COLOR]
[COLOR=#333333].MatchSoundsLike = False[/COLOR]
[COLOR=#333333].MatchAllWordForms = False[/COLOR]
[COLOR=#333333]End With[/COLOR]
[COLOR=#333333]Selection.Find.Execute Replace:=wdReplaceAll
[/COLOR]
 
Upvote 0
Hi Borgem,

This could work great. I ran it, but it obviously didn't take out any of the numbers. I tried to insert i = 1 To 500 after the With Selection.Find, but get "compile error: Syntax Error". I am assuming I am putting this into the wrong place or just missing code. Thanks for they help.
 
Upvote 0
That wasn't a complete code...try

Code:
[COLOR=#333333]' Delete the words Slide i
[/COLOR]for i = 1 to 100
[INDENT]Selection.Find.Replacement.ClearFormatting[/INDENT]
[INDENT]With Selection.Find[/INDENT]
[INDENT=2].Text = "Slide " & i[/INDENT]
[INDENT=2].Replacement.Text = ""[/INDENT]
[INDENT=2].Forward = True[/INDENT]
[INDENT=2].Wrap = wdFindContinue[/INDENT]
[INDENT=2].Format = False[/INDENT]
[INDENT=2].MatchCase = False[/INDENT]
[INDENT=2].MatchWholeWord = False[/INDENT]
[INDENT=2].MatchWildcards = False[/INDENT]
[INDENT=2].MatchSoundsLike = False[/INDENT]
[INDENT=2].MatchAllWordForms = False[/INDENT]
[INDENT]End With[/INDENT]
[INDENT]Selection.Find.Execute Replace:=wdReplaceAll [/INDENT]
[COLOR=#333333]Next i
[/COLOR]

I was saying you should implement a for loop with that code...the above adds in the for loop. You may have to tweak it a bit more, but that should work or be very close to what you need.
 
Upvote 0
Thanks, unfortunately I am not very proficient in understanding VBA. However I can go through and test different lines. I added the code to the bottom and everything ran fine, but the issue still remains it deletes the "1" in the page before the start of the "slide 1....slide X". I tried to delete the code that used the replace "1" with " " but then the number remains after (which I figure it might). Thoughts?
 
Upvote 0
I'm not entirely sure I get why you replace "9", "0" and "1" with "" in your code...if you replace your entire code with what I gave you, it will (should) step through and replace any instance of "Slide 1" through "Slide 100".
 
Upvote 0
Hmm, I just ran your code, but it's leaving the numbers above 10 behind. It's deleting the word Slide 1 through 9, but leaving a 0 for Slide 10. As for the reason I put your code with mine is because I didn't realize there was more code in your previous post and just happened to see it looking on this slide. Sorry about that. Thanks for the continued help! Much appreciated.
 
Upvote 0
I got it...change:

for i = 1 to 100 >> for i = -100 to -1

.Text = "Slide " & i >> .Text = "Slide " & -i

If that gives and error, try: .Text = "Slide " & (-1)*i


This will start with high numbers and go to low. For instance, it will then capture "Slide 10" before "Slide 1", as opposed to just lopping off the leading 1 and leaving the 0 behind.
 
Last edited:
Upvote 0
Far more efficient:
Code:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Text = "Slide [0-9]{1,}"
  .Replacement.Text = ""
  .Forward = True
  .Wrap = wdFindContinue
  .Format = False
  .MatchWildcards = True
  .Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,791
Members
451,589
Latest member
Harold14

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top