如果列表包含使用单词VBA

时间:2018-09-01 05:01:52

标签: ms-word word-vba

我有一个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

同样,如果在任何列表类型中均找到文本“ Figure ID:”,则引发警报消息。

  1. 这是列表
  2. 这是列表
  3. 这是列表
  4. 图ID:

1 个答案:

答案 0 :(得分:1)

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

Range对象具有一个ListParagraphs属性,该属性将返回Range的ListParagraph个对象。在这种情况下,该段将是“查找”项所在的段落, if 属于一个编号列表。如果是这样,Count将大于0,并且代码继续获得Paragraph.Range,可以使用{从中提取属于列表的 all 段落。 {1}}。

要选择整个列表,必须获得第一个列表条目的Rnage.ListFormat.List.ListParagraphs点和最后一个列表条目的Start点。在下面的代码中,找到“ Figure Id”的段落的范围已扩展到这些点,从而涵盖了整个列表。注意,一旦有了它,由于代码循环,不清楚要做什么。可能根本不应该选择,而应该对Range对象执行操作,而是...

End
相关问题