如果列表包含使用单词VBA的特定文本,如何选择列表

问题描述 投票:-1回答:1

我有一个vba代码,用于查找表中找到的特定字符串,以及如果找到指定的文本,我需要一个vba代码来选择列表。

代码来自这里,

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

同样,如果在任何列表类型中找到文本“图ID:”,则抛出警报消息。

  1. 这是清单
  2. 这是清单
  3. 这是清单
  4. 图Id:
ms-word word-vba
1个回答
1
投票

总的来说,最好使用Range对象而不是Selection。只能有一个选项,但代码可以根据需要使用尽可能多的范围。我相应地修改了原始代码。我还将Find.Wrap更改为wdFindStop,以便代码搜索整个文档,然后停止。

Range对象具有ListParagraphs属性,该属性将返回Range的ListParagraph对象。在这种情况下,如果它属于编号列表,那将是Find项所在的段落。如果是这样,Count将大于0并且代码继续获得Paragraph.Range,从中可以使用Rnage.ListFormat.List.ListParagraphs提取属于列表的所有段落。

为了选择整个列表,需要获得第一个列表条目的Start点和最后一个列表条目的End点。在下面的代码中,找到“图Id”的段落范围扩展到这些点,以便它覆盖整个列表。请注意,一旦你拥有它,你不清楚你想用它做什么,因为代码循环。可能根本不应该选择它,但应该在Range对象上执行操作,而不是......

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
© www.soinside.com 2019 - 2024. All rights reserved.