-
Mar 22nd, 2004, 07:15 AM
#1
Thread Starter
Member
VBA Replace only within a RANGE in Word
Hi all,
I have a replace sub working, but need it to disregard the first page of this word document. The document could be any length, but just the first page needs to be ignored.
VB Code:
Selection.MoveDown Unit:=wdScreen, Count:=3
Selection.EndKey Unit:=wdStory, Extend:=wdExtend
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ".00"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.HomeKey Unit:=wdLine
Selection.HomeKey Unit:=wdStory
This selects everything from page 2 down, replaces the ".00" with nothing, and then takes the cursor home. BUT - when you run this macro (from on the toolbar), it does indeed replace these instances of ".00" from the first page.
Any help or suggestions???
-
Mar 22nd, 2004, 07:19 AM
#2
Thread Starter
Member
I have just tried it again and it works... as per usual, when you are actually SHOWING someone...
So - must have had my cursor in the wrong spot when I ran the macro. is there a 'cleaner' way - regardless of where the cursor is at the point of running this?
-
Mar 22nd, 2004, 07:29 AM
#3
Thread Starter
Member
... and now it IS replacing the ".00"s on the first page again. Honestly, I am not an idiot. At least, that's what my therapist tells me.
Please... help?
-
Mar 22nd, 2004, 07:47 AM
#4
you need to get the selection right, no matter where the cursor is at the start.
The first thing you should do is to move the cursor to the start of the document (I think the last line of your code does that ), then move it down a page.
You could also split the document into sections (I think you do it in the "Page Setup" menu), with page 1 being a section on its own, then do the replace in any sections after that.
-
Mar 22nd, 2004, 08:15 AM
#5
Thread Starter
Member
Fantastic!
[Highlight=VB]
Sub ReplaceDecimal()
ActiveDocument.Sections(2).Range.Select
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ".00"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.HomeKey Unit:=wdStory
End Sub
[/vbode]
THANK YOU for suggesting sections, and after I played a little with how to select a section - voila. Thanks again!
-
Mar 22nd, 2004, 09:47 AM
#6
Thread Starter
Member
Small alteration, and it works perfectly. the 'Wrap:=wdfindStop' makes the replace function only within the selection, and not continuing on through the rest of the document once finished with that selection.
VB Code:
Sub ReplaceDecimal()
ActiveDocument.Sections(2).Range.Select
With Selection.Find
.ClearFormatting
.Text = ".00"
.Replacement.ClearFormatting
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll, Forward:=True, _
Wrap:=wdFindStop
End With
Selection.HomeKey Unit:=wdStory
End Sub
Pretty simple in the end, huh?
-
Mar 22nd, 2004, 10:03 AM
#7
that looks much nicer
-
Jun 9th, 2012, 01:22 PM
#8
New Member
Re: VBA Replace only within a RANGE in Word
[QUOTE=Scott_MacKenzie;1653059]Small alteration, and it works perfectly. the 'Wrap:=wdfindStop' makes the replace function only within the selection, and not continuing on through the rest of the document once finished with that selection.
With Selection.Find
.Execute Forward:=True, _
Wrap:=wdFindStop
End With
Thanks for this veritable PEARL OF WISDOM, SCOTT!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|