Consulting

Results 1 to 3 of 3

Thread: How to find some text within a specific style paragraphs

  1. #1

    How to find some text within a specific style paragraphs

    Hi!

    I need to find some text within a specific style paragraphs. For example, "Telephone" should be find within the paragraphs which are in "ADDRESS" style.

    Already I have done these kind of search within paragraphs enclosed with tags, e.g. <ADDRESS>...</ADDRESS>.

    I have furnished the coding below. I need to change the coding for search within "same style paragraphs".

    Sub a_Address()
    Dim strFind() As String
    Dim oRng As Range, oRngTag As Range, oRngLimit As Range, lngIndex As Long
    Dim bFound As Boolean
      
      strFind = Split("Telephone|Tel.|Fax|fax", "|")
      Set oRng = ActiveDocument.Range
      With oRng.Find
        Do While .Execute(FindText:="\<ADDRESS\>*\<\/ADDRESS\>", MatchWildcards:=True)
          bFound = False
          For lngIndex = 0 To UBound(strFind)
          Set oRngTag = oRng.Duplicate
          Set oRngLimit = oRng.Duplicate
          With oRngTag.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .MatchWholeWord = True
            .MatchCase = True
            Do While .Execute(FindText:=strFind(lngIndex), Forward:=True, Wrap:=wdFindStop) = True
              bFound = True
              With oRngTag
                If Not oRngTag.InRange(oRngLimit) Then Exit Do
                .HighlightColorIndex = wdTurquoise
                .Font.Color = wdColorPink
                .Comments.Add oRngTag, "Telephone/Fax no. found"
                .Collapse wdCollapseEnd
              End With
            Loop
          End With
        Next lngIndex
        If Not bFound Then oRng.Comments.Add oRngTag, "Telephone/Fax no. was not found"
        oRng.Collapse wdCollapseEnd
      Loop
      End With
    lbl_Exit:
      Exit Sub
    End Sub
    Pl HELP! Thanks in advance!

    Regards
    Alex

  2. #2
    I take it that you mean Word styles and not tagged styles? In that case

    Sub b_Address()
    Dim strFind() As String
    Dim oRng As Range
    Dim bFound As Boolean
    Dim i As Long
        strFind = Split("Telephone|Tel.|Fax|fax", "|")
        For i = LBound(strFind) To UBound(strFind)
            Set oRng = ActiveDocument.Range
            With oRng.Find
                Do While .Execute(strFind(i))
                    If oRng.Style = "ADDRESS" Then
                        oRng.HighlightColorIndex = wdTurquoise
                        oRng.Font.Color = wdColorPink
                        oRng.Comments.Add oRng, "Telephone/Fax no. found"
                        bFound = True
                    End If
                    oRng.Collapse wdCollapseEnd
                Loop
            End With
        Next i
        If Not bFound Then
            Set oRng = ActiveDocument.Range(0, 0)
            oRng.Comments.Add oRng, "Telephone/Fax no. was not found"
        End If
    lbl_Exit:
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Thanks GM for coming forward to solve the problem!

    In that case, para-style "ADDRESS" applied, but different char-styles applied. Example:

    <ADDRESS>{addline}Address:{/addline} {dept}Department Name{/dept}, {Institution}Institution Name{/Institution}, {street}Street Address{/street}, {city}City Name{/city}, {state}State Name{/state}, {country}Country Name{/country}

    The name within {} denotes character styles and <ADDRESS> denote para-style.

    In "b_Address()", if we use char-style, it works fine. But, by human error, char-styles may not applied properly. So, we need to check/test it for para-style. Is it possible?

    Thanks in advance!!!

    Regards
    Alex

Posting Permissions

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