View Single Post
 
Old 06-19-2017, 12:25 PM
slaycock slaycock is offline Windows 7 64bit Office 2013
Expert
 
Join Date: Sep 2013
Posts: 255
slaycock is on a distinguished road
Default

I'm afraid your code does have a lot of errors both structural and logical so it would never work as you intended.

What do you want to happen if no bold text with a key term is found in a row?

What do you want to happen if a key term in bold is found?

Some hints are

1. Use F1 to get help on any VBA term
2. In the IDE use Tools.options and tick all the boxes under Code Settings

The code below might help to point you in the right direction

Code:
Sub sbSearchRowsForBold()

' Consolidate into a single string so we can search using instring  to check if the found text is a Key Term
' If Selection.Find.Text = "Organization" Or Selection.Find.Text = "Date" Or Selection.Find.Text = "Description" Or Selection.Find.Text = "Aerospace, Space & Defence" Or Selection.Find.Text = "Automotive" Or Selection.Find.Text = "Manufacturing" Or Selection.Find.Text = "Life Sciences" Or Selection.Find.Text = "Information Communication Technologies / Digital" Or "Natural Resources / Energy" Or Selection.Find.Text = "Regional Stakeholders" Or Selection.Find.Text = "Other Policy Priorities" Then
' Some of the text entries include ',' so + is used as a separator
Const myKeyTerms            As String = _
    "OrganizationDate+Description+Aerospace, Space & Defence+Automotive+Manufacturing+Life Sciences+Information Communication Technologies / Digital+Natural Resources / Energy+Regional Stakeholders+Other Policy Priorities"

Dim myTable                 As Table
Dim myRow                   As Row
Dim myRange                 As Range

    For Each myTable In ActiveDocument.Tables
        For Each myRow In myTable.Rows
            ' If successful myrange is moved to the found text
            Set myRange = myRow.Range
            ' Search parameters are persistent so you only need to change them if the search parameters change
            With myRange.Find
                .Font.Bold = True
                .Text = ""
                .Format = True
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .Wrap = wdFindStop
                .Forward = True
                ' Stop when the range is searched
                ' .Execute returns true if the search is found
                Do While .Execute
                    ' myRange is now the found term
                    Debug.Print "Bold text is found ->" & myRange.Text
                    ' for learning purposes the select statement below shows the new range for myrange
                    myRange.Select
                    If InStr(myKeyTerms, myRange.Text) > 0 Then
                        'Actions to do if the row contained a key term in bold
                        Debug.Print "Found a key term in bold " & myRange.Text
                        ' Continue searching
                        
                    Else
                        'Actions to do is the row does not contain bold text
                        Debug.Print "No key terms found in bold text"
                    End If
                    ' now we need to reset myRange to encompass the rest of the row
                    myRange.Start = myRange.End + 1
                    myRange.End = myRow.Range.End
                    ' again for learning see where myRange has moved
                    myRange.Select
                Loop
            End With
        Next myRow
    Next myTable
End Sub
Good luck with your reading
Reply With Quote