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

The code below is closer to your intent.

Code:
Sub sbSearchRowsForBold()

' Consolidate into a single string so we can search using instring  to checkif 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 seperator
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 myFirstRange            As Range
Dim mySecondRange           As Range
Dim myRemoveRange           As Range

    For Each myTable In ActiveDocument.Tables
        Set myFirstRange = Nothing
        Do
            If myFirstRange Is Nothing Then
                Set myFirstRange = fnFindBold(mySearchRange:=myTable.Range.Rows(1).Range)
            Else
                Set myFirstRange = fnFindBold(mySearchRange:=myFirstRange.Next(unit:=wdRow))
            End If
            ' two possible cases for myFirstrange
            ' 1. a found range
            ' 2. nothing - which means we have searched the whole table.
            If Not myFirstRange Is Nothing Then
                If InStr(myKeyTerms, myFirstRange.Text) = 0 Then
                    ' Found bold text that is not a defined category (key term)
                    Set mySecondRange = myFirstRange.Duplicate
                    Do
                        Set mySecondRange = fnFindBold(mySecondRange.Next(unit:=wdRow))
                    Loop Until InStr(myKeyTerms, mySecondRange) > 0
                    'We have now found text that is a defined category key term
                    Set myRemoveRange = myFirstRange.Duplicate
                    myRemoveRange.End = mySecondRange.Previous(unit:=wdRow).End
                    Set myFirstRange = mySecondRange
                    myRemoveRange.Select
                    myRemoveRange.Cut
                    ' the destiny of the cut text is left to the requirements of the user
                End If
            End If
        Loop Until myFirstRange Is Nothing
            
    Next myTable
    
End Sub

Function fnFindBold(ByVal mySearchRange As Range) As Range

    If mySearchRange Is Nothing Then
        Set fnFindBold = Nothing
        Exit Function
    End If
    
    With mySearchRange.Find
            .Font.Bold = True
            .Text = ""
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .Wrap = wdFindStop
            .Forward = True
            .Execute
            If .Found Then
                Set fnFindBold = mySearchRange
            Else
                Set fnFindBold = fnFindBold(mySearchRange.Next(unit:=wdRow))
            End If
    End With
End Function
Key things to note are

1. Ranges are pointers to objects so 'set range2=range1' does not create a new range object, it just creates a second pointer to the same object as pointed to by range1. if you want to create a second object for range2 then you must use the duplicate property 'set range2=range2.duplicate'. You might think that you could also say 'set range2 = new range1' but this is not the case.

2. Searching for bold text is a repeated task so it is delegated to a function.

3. fnFindBold is written as a recursive function. You could rewrite this using a do loop.

4. If we ask for the next row in a table and there isn't a next row then 'nothing' is returned and not the range corresponding to the first row of the next table.

5. The code above searches by row, but this possibly isn't necessary.

I tested the code above on the table

OrganizationDate
2 Data row
3 Data row
4 Data row
User defined category
6 Data row
7 Data row
8 Data row
Life Sciences
10 Data row
11 Data row
12 Data row


and it correctly identifies and cuts the text from 'User defined category' to '8 Data row' from the table.

I've left it to your imagination as to what you do with the rows that have been cut from the table.
Reply With Quote