none
Method to select entire text between "Find" parameters RRS feed

  • Question

  • Hello,

    I was trying to think of a way to select entire blocks of text between "Find" search parameters, not just the particular searched word or respective line,

    and then reorganize based on alphabetical ordering of the first line.

     

    For example, if my document was:

    Item : Oranges

    TEXT IN ORANGES

    Item : APPLES

    TEXT IN APPLES

    Then my code would select from Item : Oranges to ORANGES, and then move that below APPLES, because A is before O.

    Code I have so far is this, but I cant figure out the rest of the algorithm.

    Thanks much!

    Sub Findselection()
    
      Selection.Find.ClearFormatting
      With Selection.Find
        .Text = "item:"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
      End With
      Selection.Find.Execute
    End Sub
    

     

     

    Monday, June 20, 2011 4:35 PM

Answers

  • The Find command is very flexible and the text it is searching for does not have to be hard coded into the .Text property but it has to search for something and that something has to be specified somewhere.  You can declare a variable such as: "Dim str as String" and the the text property of the Find command looks like this ".Text = str" but again somewhere what is being searched for has to be specified and inserted programatically into the string.  If it's not a string of text, but a style or a bookmark and other things, those too can be searched and found.

    As for selecting starting and ending locations that's easy, you would change just one statement in the code example I supplied.  rStart = rng.End  would be changed to rStart = rng.Start

     

     


    Kind Regards, Rich ... http://greatcirclelearning.com
    Monday, June 20, 2011 7:48 PM

All replies

  • Here's one way you can do it. It captures only the range between the two searched items.

    Sub FindRange()
      Dim doc As Word.Document
      Dim rng As Word.Range
      Dim rStart, rEnd As Long
      
      Set doc = Word.ActiveDocument
      Set rng = doc.Range
      
      rng.Find.ClearFormatting
      With rng.Find
        .Text = "Apple"
        .Wrap = wdFindStop
        .Forward = True
        .Execute
      End With
      If rng.Find.Found Then
        rStart = rng.End
      End If
      
      rng.MoveEnd unit:=wdStory
      rng.Find.ClearFormatting
      With rng.Find
        .Text = "Orange"
        .Wrap = wdFindStop
        .Forward = True
        .Execute
      End With
      If rng.Find.Found Then
        rEnd = rng.Start
      End If
      
      Set rng = doc.Range(rStart, rEnd)
      
      Debug.Print rng.Text
      
    End Sub
    

     


    Kind Regards, Rich ... http://greatcirclelearning.com
    Monday, June 20, 2011 6:21 PM
  • Hmm, this may work for the example, but the method should be flexible and not dependent on parameters such as .Text = "Orange".

    I was looking for a way to select text from one "Item: " all the way to right before the next "Item: ", and thought that the Find tool would somehow be useful.

    This way, any hard coding is avoided. Also, the alphabetical organizing of "Item: Oranges " and "Item: Apples" seems tricky.

    Maybe this is too complex for Word *scratches head*

    Monday, June 20, 2011 7:14 PM
  • The Find command is very flexible and the text it is searching for does not have to be hard coded into the .Text property but it has to search for something and that something has to be specified somewhere.  You can declare a variable such as: "Dim str as String" and the the text property of the Find command looks like this ".Text = str" but again somewhere what is being searched for has to be specified and inserted programatically into the string.  If it's not a string of text, but a style or a bookmark and other things, those too can be searched and found.

    As for selecting starting and ending locations that's easy, you would change just one statement in the code example I supplied.  rStart = rng.End  would be changed to rStart = rng.Start

     

     


    Kind Regards, Rich ... http://greatcirclelearning.com
    Monday, June 20, 2011 7:48 PM