Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 36

Thread: search for one word when found search another

  1. #1
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location

    search for one word when found search another

    lets say I have words:

    Start
    my name is saban
    End

    Start
    my surname is saulic
    End


    Now I would like to search for word start and when it is found search for word end but this word end should be found before next start word is found (occurs)
    any ideas
    thnx

  2. #2
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Unfortunately there is no easy way to do this. You must search for "Start" and then look, in separate searches, for "Start" and "End", and see which is earlier in the document (assuming both exist).
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    This is the same logic problem as your posts re: <Amend> and </Amend>. No different.

    I am still trying to find a (relatively) simple way to do this. There is an exceedingly involved complex way...and frankly, I do not think there IS a simple way.

  4. #4
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    The basic logic isn't difficult but it does need multiple searches ...

    [VBA]
    Sub FindStartEnd()

    Dim FoundStart As Range
    Dim FoundStartNext As Range
    Dim FoundEnd As Range

    Selection.HomeKey wdStory

    With Selection.Find
    .ClearFormatting
    .Text = "Start"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With

    If Selection.Find.Execute(FindText:="Start") Then
    Set FoundStart = Selection.Range.Duplicate
    Selection.Collapse wdCollapseEnd
    If Selection.Find.Execute(FindText:="Start") Then
    Set FoundStartNext = Selection.Range.Duplicate
    ActiveDocument.Range(FoundStart.End + 1, FoundStartNext.Start - 1).Select
    Else
    ActiveDocument.Range(FoundStart.End + 1, ActiveDocument.Range.End).Select
    End If
    If Selection.Find.Execute(FindText:="End") Then
    Set FoundEnd = Selection.Range.Duplicate
    ActiveDocument.Range(FoundStart.Start, Selection.End).Select
    Else
    Selection.Collapse wdCollapseEnd
    MsgBox """Start .... End"" not found."
    End If
    End If

    End Sub[/VBA]

    (not thoroughly tested)
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  5. #5
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    thnx guys

  6. #6
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    tonny

    It works for one instance of "start" and "end" word but if i have multiple instances it does not

    works:
    start
    blabla
    end

    does not work:
    start
    bla bla
    end
    start
    blabla

  7. #7
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    It finds the first instance - you will then need to do it again with an appropriate start position. If there is another "start" in the document you will already have found it so use that as a basis for the next Find.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  8. #8
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    but how do i determine that it searches from next start not from the same one

  9. #9
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    [VBA]Sub FindStartEnd()

    Dim FoundStart As Range
    Dim FoundStartNext As Range
    Dim FoundEnd As Range

    Selection.HomeKey wdStory

    With Selection.find
    .ClearFormatting
    .Text = "Start"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With

    If Selection.find.Execute(FindText:="Start") Then
    Set FoundStart = Selection.Range.Duplicate
    Selection.Collapse wdCollapseEnd
    If Selection.find.Execute(FindText:="Start") Then
    Set FoundStartNext = Selection.Range.Duplicate
    ActiveDocument.Range(FoundStart.End + 1, FoundStartNext.Start - 1).Select
    Else
    ActiveDocument.Range(FoundStart.End + 1, ActiveDocument.Range.End).Select
    End If
    If Selection.find.Execute(FindText:="End") Then
    Set FoundEnd = Selection.Range.Duplicate
    ActiveDocument.Range(FoundStart.Start, Selection.End).Select
    Else
    Selection.Collapse wdCollapseEnd
    msgbox """Start .... End"" not found."
    End If
    End If
    Selection.Collapse wdCollapseEnd
    Do
    With Selection.find
    .ClearFormatting
    .Text = "Start"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With

    If Selection.find.Execute(FindText:="Start") Then
    Set FoundStart = Selection.Range.Duplicate
    Selection.Collapse wdCollapseEnd
    If Selection.find.Execute(FindText:="Start") Then
    Set FoundStartNext = Selection.Range.Duplicate
    ActiveDocument.Range(FoundStart.End + 1, FoundStartNext.Start - 1).Select
    Else
    ActiveDocument.Range(FoundStart.End + 1, ActiveDocument.Range.End).Select
    End If
    If Selection.find.Execute(FindText:="End") Then
    Set FoundEnd = Selection.Range.Duplicate
    ActiveDocument.Range(FoundStart.Start, Selection.End).Select
    Else
    ' Selection.Collapse wdCollapseEnd
    msgbox """Start .... End"" not found."
    End If
    End If
    Selection.Collapse wdCollapseEnd
    Loop Until FoundStart Is Nothing
    End Sub
    [/VBA]
    I guess this does the trick
    except there is one problem why does the procedure not stop
    I get this infinity loop I guess

    Thnx

  10. #10
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    and one more question

    how could be done if "Start" is missing

  11. #11
    VBAX Regular
    Joined
    May 2004
    Posts
    12
    Location
    Try this, slightly different approach…

    Sub MyTest()
    Dim arr1, arr2, i, rng As Range
    arr1 = Array("(<End>)(*)(<End>)", "(<Start>)(*)(<Start>)", "(<Start>)(*)(<End>)")
    arr2 = Array("<Start>", "<End>", "End,Start", "End,Start")
    With Selection
    .HomeKey Unit:=wdStory
    With .Find
    .ClearFormatting: .Replacement.ClearFormatting
    .Replacement.Text = "": .Forward = True: .Wrap = 0
    .Format = False: .MatchCase = False
    .MatchWholeWord = True: .MatchAllWordForms = False
    .MatchSoundsLike = False: .MatchWildcards = True
    For i = LBound(arr1) To UBound(arr1)
    Selection.HomeKey Unit:=wdStory: .Text = arr1(i)
    Randomize
    While .Execute
    If i = UBound(arr1) Then
    Selection.Range.HighlightColorIndex = Int((16 * Rnd) + 1)
    Else
    Set rng = Selection.Range
    .Text = arr2(i)
    If Not .Execute Then
    rng.Words(IIf(i = LBound(arr1), 1, rng.Words.Count)) = _
    Split(arr2(i + 2), ",")(0) & vbCrLf & vbCrLf & Split(arr2(i + 2), ",")(1)
    End If
    Selection.Collapse wdCollapseEnd: .Text = arr1(i)
    End If
    Wend
    Next i
    End With
    End With
    End Sub

    Tried it with the following sample and it seems to work. The code tries to guess where the missing "start" or "end" tags are and inserts them. It will fail if "End" tag of one block and "Start" tag of the very next block is missing. Also make sure the very first "Start" and the very last "End" tag is present.
    btw how do you use vba tags while posting?


    Start
    my name is saban

    Start
    my name is saban

    Start
    my surname is saulic
    End

    my name is saban
    End

    my surname is saulic
    End

  12. #12
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    just press small square with VBA text inside and you will get
    <CODE>blbblb</CODE> except the brackets will be [
    [VBA]here[/VBA]

  13. #13
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    for your previous example how can i exit sub if no start or end is found
    I mean when it reachs the end of the document the sub should be stoped

  14. #14
    VBAX Regular
    Joined
    May 2004
    Posts
    12
    Location
    Not sure what you mean... As it is the code should stop once it reaches the end (or once it has processed all start/end blocks)
    Is it going in a continuous loop or something?

  15. #15
    VBAX Regular
    Joined
    May 2004
    Posts
    12
    Location
    Like I said, I am not sure if this is what you want but try replacing this:
    rng.Words(IIf(i = LBound(arr1), 1, rng.Words.Count)) = _
    Split(arr2(i + 2), ",")(0) & vbCrLf & vbCrLf & Split(arr2(i + 2), ",")(1)
    with this

    MsgBox arr2(i) & " not found; exiting..."
    Exit For

  16. #16
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    R_Rajesh - interesting approach.

    Your code highlights the chunks, but I am not sure exactly how this fits to the purpose of this post. Could you please redo your code and put comments into it? Just the normal way of doing comments, with the "'". Your comments may make it easier to follow what you are doing.

  17. #17
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    sorry rajesh it was not your example but tonys
    The example tonyjollans posted

    But I have a problem how to avoid an error when sub comes till end of the document also gives me an error "Start... end not found" I must avoid this error somehow

    any ideas
    Thnx

  18. #18
    VBAX Regular
    Joined
    May 2004
    Posts
    12
    Location
    >>Now I would like to search for word start and when
    >>it is found search for word end

    fumei, after reading the above Ii assumed that once the start and end was found, saban wanted to do something with that range so I highlighted them as an example. Looks like I was way off track...

  19. #19
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    how can be done: when sub reaches the end of the document that it calls another sub or exits one

    any ideas
    Thnx

  20. #20
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    just one short question

    How can I refer to last character in document?
    If I have text like this

    blbablablablbalba
    c
    And when sub reaches this c-(and selects it) or any other character at the end of the document it must be stopped

    I am going crazy on this
    thnx for all your help

Posting Permissions

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