Solved

How to nest find ...

Posted on 2011-09-15
5
271 Views
Last Modified: 2012-05-12
I am trying to use vba to find some text in a word doc and hight it, but I need the search to be nested.  

I need to find Text1 and then find Text2.


'this function works fine ...
Public Function FindAndHL(txt As String, clr As Long)
    Dim bFound As Boolean

    With ActiveDocument.Content.Duplicate
    .MoveStart wdCharacter, 0
    Do
        'bFound = .Find.Execute(Findtext:=Chr(13) & "EVN")
        bFound = .Find.Execute(Findtext:=txt)
        If bFound Then
            Debug.Print .Start
            '.MoveStart wdCharacter, 0
            '.MoveEnd wdCharacter, 0
            '.Font.Color = wdColorBlueGray
            .HighlightColorIndex = clr
        
        End If
    Loop Until bFound = False

    End With

End Function

Open in new window

'I this function, tx2 is never found ...

Public Function FindField(txt As String, tx2 As String, field As Long, clr As Long)
    With ActiveDocument.Content.Duplicate
    
    Dim bFoundSeg As Boolean
    Dim bFoundFld As Boolean
    Dim bNextDel As Boolean
    Dim bFldNotFnd As Boolean
    Dim I As Long
    
    .MoveStart wdCharacter, 0
    Debug.Print .Start
    bFldNotFnd = False
    Do
        
        bFound = .Find.Execute(Findtext:=txt)
        If bFound Then
        
            .MoveStart wdCharacter, 0
            Debug.Print .Start
            .Find.ClearFormatting


            For I = 1 To field
            
                Debug.Print .Start
                bNextDel = .Find.Execute(Findtext:=tx2)
                'bNextDel = .Find.E
                                
                .MoveStart wdCharacter, 0
                .MoveEnd wdCharacter, 0
                Debug.Print .Start
                If bNextDel Then
                    bFldNotFnd = True
                    Exit For
                End If
                
            Next
            
            .MoveStart wdCharacter, 0
            .MoveEnd wdCharacter, 0
            '.Font.Color = wdColorDarkGreen
            .HighlightColorIndex = clr
        
        End If
    Loop Until bFound = False
    
    End With

End Function

Open in new window

0
Comment
Question by:sidwelle
  • 3
  • 2
5 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 36545322
Do you want to find Text1 and then find Text2 later in the document, or do you want to find Text1 and then find Text2 within the found Text1 ?
0
 

Author Comment

by:sidwelle
ID: 36546198
2.  Find text1 and then find text2 before the next occourance of text1

So text1 will the header/identifier of a block of text that will be different everytime, I want to find text2 within the block.

TEXT1:fkjdal;fjadlk;f adjmlfk;ds fjalk;df jkfdal;fj TEXT2fjdkaf;ldas jfkad;f jadk;
TEXT1:fjkdal;sfdkj TEXT2akfjdlkafjdsalk;fkj
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 36546350
This will find the first occurrence of tx2 after the first occurrence of txt:
Public Function FindText2(txt As String, tx2 As String) As Range
    Dim rng As Range
    Set rng = ActiveDocument.Range
    With rng.Find
        .Text = txt
        If .Execute Then
            rng.Collapse
            rng.End = ActiveDocument.Range.End
            .Text = tx2
            If .Execute Then
                Set FindText2 = rng
            End If
        End If
    End With
End Function

Open in new window

0
 

Author Comment

by:sidwelle
ID: 36550221
I get the following attached error at line: "If rg3.Find.Execute Then ..."

What I really want to do is walk across delimeters, but I have to use whats available w/in vba.
Here is kind of what we do outside of vba. (w/o the range stuff)

Sub HiField(fld as long, NextDelimeter as string, SomColor as long)
   doc as string
   doc = ActiveDocument.Text
   
   for I = 1 to field
      i1 = instr(i1+1, Doc, NextDelimeter)
   next
   i2 = instr(i1 +1, Doc, NextDelimeter)     'Find the end of the field.
   MyRng.Start = i1
   MyRng.End = i2 -1

   MyRng.HighLight = SomeColor ...
End Sub

Public Function FindNested01(tx1 As String, tx2 As String) As Range
    Dim rg1 As Range
    Dim rg2 As Range
    Dim rg3 As Range
    
    Set rg1 = ActiveDocument.Range  'used to find block 01
    Set rg2 = ActiveDocument.Range  'used to find the second occourance of block1
    Set rg3 = ActiveDocument.Range  'used to find the block 2 w/in block 1
    
    With rg1.Find
        .Text = tx1
        If .Execute Then    'Found first block
            rg1.Collapse
            rg1.End = ActiveDocument.Range.End
            
            rg2.Start = rg1.Start
            rg2.End = ActiveDocument.Range.End
            rg2.Find.Text = tx1
            If rg2.Find.Execute Then    'Found second block
                rg1.End = rg2.Start     'Now the first range only covers the first block
                
                rg3.Start = rg1.Start
                rg3.End = ActiveDocument.Range.End
                rg3.Find.Text = tx2
                
                If rg3.Find.Execute Then    'Search the fist block for the 2nd block
                    rg3.Collapse
                
                    rg3.HighlightColorIndex = wdGreen   'now highlight the 2nd block...
                
                End If
                
            End If
        
        End If

    End With

End Function

Open in new window

RunTime-5625.GIF
0
 

Author Closing Comment

by:sidwelle
ID: 36816228
thanks
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now