Results 1 to 7 of 7
  1. #1
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Silicon Valley, California, USA
    Posts
    273
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Select Range by two "Finds" (2000 SR1)

    Hi Everybody,

    I'm trying to write a VBA snippet that will use two "Finds" (or reasonable facsimile) to determine the beginning and ending of a block of text, then select that block. (Excel is my forte, I don't know the Word object model very well at all). Thanks in advance!

  2. #2
    4 Star Lounger
    Join Date
    Mar 2001
    Location
    Bismarck, ND, USA
    Posts
    451
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Select Range by two "Finds" (2000 SR1)

    Just a suggestion: could you use one "find" using the asterisk wildcard?
    egghead

  3. #3
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Silicon Valley, California, USA
    Posts
    273
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Select Range by two "Finds" (2000 SR1)

    Hi,

    Thanks for the response. The answer is "no", because my "customer" wants to be able to specify two different words or phrases, find them, and delete them with all the text in between. Meanwhile, by fiddling a little bit, I figured out a way to do it, it is shown below (a quick-and-dirty):

    <pre>Sub SelectBlockForDeletion()

    Dim PosStart As Long, PosEnd As Long
    Dim TextStart As String, TextEnd As String

    TextStart = InputBox("Text to start block: ", "Delete Block")
    TextEnd = InputBox("Text to end block: ", "Delete Block")

    With ActiveDocument
    ' Find starts where current selection is
    With Selection.Find
    ' Set the Find conditions...
    .ClearFormatting
    .Text = TextStart
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchByte = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = False
    .MatchFuzzy = False
    .Execute ' ... execute the find ...
    PosStart = Selection.Start ' ... get starting pos from the result

    End With ' Selection.Find

    .Characters(PosStart).Select ' Start the "end" find at the start position
    With Selection.Find
    ' Set the Find conditions...
    .ClearFormatting
    .Text = TextEnd
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchByte = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = False
    .MatchFuzzy = False
    .Execute ' ... execute the find ...
    PosEnd = Selection.End ' ... get the end pos from the result

    End With ' Selection.Find

    .Range(PosStart, PosEnd).Select ' Select block, leave up to user to delete

    End With ' ActiveDocument

    End Sub ' SelectBlockForDeletion
    </pre>


  4. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Select Range by two "Finds" (2000 SR1)

    Starting from your code, here is the same thing with a few less lines. I liked your method for defining which Sub or With was ending. I might have to start doing that myself.
    <pre>Sub SelectBlockForDeletion()
    Dim TextStart As String, TextEnd As String

    TextStart = InputBox("Text to start block: ", "Delete Block")
    TextEnd = InputBox("Text to end block: ", "Delete Block")
    ' Find starts where current selection is
    With Selection.Find
    ' Set the Find conditions...
    .ClearFormatting
    .text = TextStart
    .Replacement.text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchByte = False 'rem for Word 97
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = False
    .MatchFuzzy = False 'rem for Word 97
    .Execute ' ... execute the first find ...
    Selection.ExtendMode = True
    .text = TextEnd
    .Execute ' ... execute the second find ...
    End With ' Selection.Find
    End Sub ' SelectBlockForDeletion</pre>

    Andrew Lockton, Chrysalis Design, Melbourne Australia

  5. #5
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Silicon Valley, California, USA
    Posts
    273
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Select Range by two "Finds" (2000 SR1)

    Yes, you are right, I had a lot of redundant stuff in there. That's what I get for using the macro recorder in a pinch. Thanks!

    My commenting strategy I came up with just to keep my own head on straight, and others I have shared it with agree it is a good approach. Thanks!

  6. #6
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Select Range by two "Finds" (2000 SR1)

    I think egghead was right: Searching for TextStart*TextEnd with wildcards, replacing with nothing, would work fine, too.

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>Regards, Klaus

  7. #7
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Silicon Valley, California, USA
    Posts
    273
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Select Range by two "Finds" (2000 SR1)

    Oh yeah! I misunderstood the suggestion. Silly me...

Posting Permissions

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