Results 1 to 2 of 2
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Wellington, Wellington, New Zealand
    Posts
    378
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Extend range using Find (Word97 (yep, it's still out there))

    Hi

    Using ranges, I want to be able to extend the end of the current range using a Find.

    For example, if the current range is "I want to" in the above sentence, I want to search for "extend" and have the range extend to "I want to be able to extend".

    Can do?
    Dale

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Extend range using Find (Word97 (yep, it's still out there))

    For the current selection:

    Sub ExtendSelectionTo(strText as string)
    Dim lngStart As Long
    lngStart = Selection.Start
    Selection.Collapse Direction:=wdCollapseEnd
    With Selection.Find
    .ClearFormatting
    .Text = strText
    .Forward = True
    .Wrap = wdFindStop
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .Execute
    End With
    Selection.Start = lngStart
    End Sub

    Use like this:

    ExtendSelectionTo "extend"

    For a range defined in code:

    Sub ExtendRangeTo(rng As Range, strText As String)
    Dim lngStart As Long
    lngStart = rng.Start
    rng.Collapse Direction:=wdCollapseEnd
    With rng.Find
    .ClearFormatting
    .Text = strText
    .Forward = True
    .Wrap = wdFindStop
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .Execute
    End With
    rng.Start = lngStart
    End Sub

    Use like this:

    Sub Test()
    Dim rng As Range
    Set rng = ActiveDocument.Paragraphs(1).Range
    ExtendRangeTo rng, "extend"
    ' code to do something with the extended range goes here
    ...
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •