VBA Replace only within a RANGE in Word-VBForums
Results 1 to 8 of 8

Thread: VBA Replace only within a RANGE in Word

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2002
    Posts
    61

    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:
    1. Selection.MoveDown Unit:=wdScreen, Count:=3
    2.     Selection.EndKey Unit:=wdStory, Extend:=wdExtend
    3.     Selection.Find.ClearFormatting
    4.     Selection.Find.Replacement.ClearFormatting
    5.     With Selection.Find
    6.         .Text = ".00"
    7.         .Replacement.Text = ""
    8.         .Forward = True
    9.         .Wrap = wdFindAsk
    10.         .Format = False
    11.         .MatchCase = False
    12.         .MatchWholeWord = False
    13.         .MatchWildcards = False
    14.         .MatchSoundsLike = False
    15.         .MatchAllWordForms = False
    16.     End With
    17.     Selection.Find.Execute Replace:=wdReplaceAll
    18.     Selection.HomeKey Unit:=wdLine
    19.     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???

  2. #2

    Thread Starter
    Member
    Join Date
    Apr 2002
    Posts
    61
    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?

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2002
    Posts
    61
    ... 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?

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    37,114
    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.

  5. #5

    Thread Starter
    Member
    Join Date
    Apr 2002
    Posts
    61
    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!

  6. #6

    Thread Starter
    Member
    Join Date
    Apr 2002
    Posts
    61
    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:
    1. Sub ReplaceDecimal()
    2.     ActiveDocument.Sections(2).Range.Select
    3.     With Selection.Find
    4.         .ClearFormatting
    5.         .Text = ".00"
    6.         .Replacement.ClearFormatting
    7.         .Replacement.Text = ""
    8.         .Execute Replace:=wdReplaceAll, Forward:=True, _
    9.             Wrap:=wdFindStop
    10.     End With
    11.     Selection.HomeKey Unit:=wdStory
    12. End Sub


    Pretty simple in the end, huh?

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    37,114
    that looks much nicer

  8. #8
    New Member
    Join Date
    Jun 2012
    Posts
    1

    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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.