VBA - How to start a selection halfway through the text in a table

前端 未结 1 1109
野趣味
野趣味 2020-12-07 05:50

As an alternative to my previous question regarding range.find, I am trying Selection.find.

I have found the occurrence of an abbreviation

相关标签:
1条回答
  • 2020-12-07 06:29

    Use a (temporary) bookmark to note where you want to restart. Do not try to rely on the Start and End properties. Those are not reliable.

    When Find could end up in a table and the found term will continue to exist in that position, searching from that point onwards to the end (or beginning) of the document will automatically include the entire row. You can see this as a user if you click in a cell then hold Shift and press right-arrow until the selection moves beyond the table.

    In such a situation you need to test whether the found Range is in a table. If it is, you need to continue the Find loop cell-by-cell until the found Range is no longer in a table.

    The following code demonstrates the principle. It uses a Range object, not Selection, as that's easier and more predictable to control. It's also very simplified in order to concentrate on the principle of looping a table cell-by-cell which can be a bit mind-boggling. (The Debug.Print is just in there for keeping track while testing.)

    Whether the Find is successful is stored in the boolean variable bFound. In the case of success the found Range is tested for being in a table. (Note that you could also use rngFind.Information(wdWithinTable).) If it is, the Range is collapsed so that the found term is "outside" it, then the Range extended to the end of the cell.

    Find is repeated in the loop until no more "hits" are found in that cell. The Range is then moved to the next cell and Find repeated until the found Range is no longer in a table. Then Find reverts to the "normal" process, in a loop, until no more instances of the search term are found.

    Sub FindLoopThroughTables()
        Dim sFindTerm As String
        Dim doc As Word.Document
        Dim rngFind As Word.Range
        Dim cel As Word.Cell
        Dim bFound As Boolean
    
        Set doc = ActiveDocument
        Set rngFind = doc.content
        rngFind.Find.wrap = wdFindStop
        sFindTerm = "the"
    
        bFound = rngFind.Find.Execute(sFindTerm)
        Do While bFound
                Debug.Print rngFind.Start
                If rngFind.Tables.Count > 0 Then
                    Do While bFound And rngFind.Tables.Count > 0
                        Set cel = rngFind.Cells(1)
                        rngFind.Collapse wdCollapseEnd
                        rngFind.End = cel.Range.End - 1
                        bFound = rngFind.Find.Execute(sFindTerm)
                        If bFound Then
                            Debug.Print rngFind.Start & "in table"
                        Else
                            rngFind.MoveStart wdCell, 1
                            Set cel = rngFind.Cells(1)
                            rngFind.End = cel.Range.End
                            bFound = rngFind.Find.Execute(sFindTerm)
                       End If
                    Loop
                Else
                    rngFind.Collapse wdCollapseEnd
                    rngFind.End = doc.content.End
                    bFound = rngFind.Find.Execute(sFindTerm)
                End If
        Loop
    End Sub
    
    0 讨论(0)
提交回复
热议问题