Darious
Darious

Reputation: 121

How to select list, if the list contains specific text using word VBA

I have a vba code for find the specific string found in table, as well as i need a vba code for select the list, if specified text found.

The code was got from here,

Microsoft Word VBA - Select table if cell contains specified string,

Sub Find_Text_in_table()
selection.Find.ClearFormatting
    With selection.Find
        .Text = "figure id:"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With

    Do While selection.Find.Execute

        If selection.Information(wdWithInTable) Then

                        MsgBox "Figure ID Found in Table"
                    Exit Sub
            'now you are in table with text you searched
            'be careful with changing Selection Object
            'do what you need here
        End If
    Loop
    Application.ScreenUpdating = True
End Sub

as well, if the text "Figure ID:" found in any list type, throw an alert message.

  1. this is the list
  2. this is the list
  3. this is the list
  4. Figure Id:

Upvotes: 0

Views: 2174

Answers (1)

Cindy Meister
Cindy Meister

Reputation: 25693

On the whole, it's preferable to work with Range objects instead of Selection. There can be only one selection, but code can work with as many Ranges as necessary. I've altered the original code accordingly. I also changed the Find.Wrap to wdFindStop so that the code searches the entire document, then stops.

The Range object has a ListParagraphs property that will return the ListParagraph object(s) of the Range. In this case, that would be paragraph in which the Find term is located if it belongs to a numbered list. If it does, the Count will be greater than 0 and the code continues to get the Paragraph.Range, from which it's possible to extract all paragraphs that belong to the list using Rnage.ListFormat.List.ListParagraphs.

In order to select the entire list it's necessary to get the Start point of the first list entry and the End point of the last list entry. In the code below, the range of the paragraph in which "Figure Id" was found is extended to these points so that it covers the entire list. Note that it's not clear what you want to do with this, once you have it, since the code loops. It may be that it should not be selected at all but that the action should be performed on the Range object, instead...

Sub Find_Text_withList_in_table()
    Dim rngFind As Word.Range, rngFigureList As Word.Range
    Dim lstParas As Word.ListParagraphs
    Dim lFindCounter As Long 'for testing / debugging

    Set rngFind = ActiveDocument.content
    rngFind.Find.ClearFormatting
    With rngFind.Find
        .Text = "figure id:"
        .Replacement.Text = ""
        .Forward = True
        .wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With

    Do While rngFind.Find.Execute
        lFindCounter = lFindCounter + 1
        If rngFind.Information(wdWithInTable) Then
            Debug.Print "Figure ID Found in Table"
            Set lstParas = rngFind.ListParagraphs
            If lstParas.Count > 0 Then
                Set rngFigureList = lstParas.Item(1).Range
                Set lstAllParas = rngFigureList.ListFormat.List.ListParagraphs
                Debug.Print "Nr paragraphs in the list: " & lstAllParas.Count
                rngFigureList.Start = lstAllParas(1).Range.Start
                rngFigureList.End = lstAllParas(lstAllParas.Count).Range.End
                rngFigureList.Select
                MsgBox "Figure Id is in a numbered list, in a table"
            End If
        End If
    Loop
    Debug.Print "Nr Figure ID found: " & lFindCounter
    Application.ScreenUpdating = True
End Sub

Upvotes: 1

Related Questions