VBA和MSWord:在Find / Execute例程中使用find参数的多个值

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

我有一个查找/执行例程,它查找我的自定义样式中的段落Bullet_Type_1_Level_1,这是一个自定义的项目符号列表样式,并处理段落。 (它检查给定范围内的每个段落,看它是否在一段时间内终止,但这对于这个问题并不重要)。例程当前工作正常,但我想扩展它以搜索我的大纲列表的其他级别 - 转换为其他样式 - 以及在另一个列表中搜索样式。是否有一种紧凑的方式让我的代码也在Bullet_Type_1_Level_2numlist_Level_1中寻找段落(并处理它们)当它在它的时候?这是我现有代码的内容:

For Each para In RangeToCheck.Paragraphs
With Selection.Find
    .Text = ""
    .Style = "Bullet_Type_1_Level_1"
    .Wrap = wdFindStop
    .Execute
    Do While .Found = True 'Look for the specified style
        strSentence = Selection.Text
        'Test the string using a block of code that I'm omitting, for brevity.
        'Finally, depending on what happened, put or don't a period at the end of the original range.        
End With
Next para
vba replace word-vba
2个回答
1
投票

您可以添加另一个循环。

声明i(或更有意义的变量名),并循环遍历。

Dim i As Long
For Each para In RangeToCheck.Paragraphs
    For i = 1 To 3
        With Selection.Find
            .Text = ""
            Select Case i
            Case 1
                .Style = "Bullet_Type_1_Level_1"
            Case 2
                .Style = "Bullet_Type_1_Level_2"
            Case 3
                .Style = "numlist_Level_1"
            End Select
            .Wrap = wdFindStop
            .Execute
            Do While .Found = True 'Look for the specified style
                strSentence = Selection.Text
                'Test the string using a block of code that I'm omitting, for brevity.
                'Finally, depending on what happened, put or don't a period at the end of the original range.
        End With
    Next i
Next para

可能不是那里最漂亮的解决方案 - 单词不是我的强项☺。


0
投票

如果有段落不是那些样式,可能会更快的替代方法:

Dim i As Long
For i = 1 To 3
  With RangeToCheck
    With .Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = ""
      .Replacement.Text = ""
      .Forward = True
      .Format = True
      .Wrap = wdFindStop
      .Style = "Bullet_Type_1_Level_" & i
      .Execute
    End With
    Do While .Find.Found = True
      If .InRange(RangeToCheck) = False Then Exit Do
      Select Case i
        Case 1 'Do something for Bullet_Type_1_Level_1
        Case 2 'Do something for Bullet_Type_1_Level_2
        Case 3 'Do something for Bullet_Type_1_Level_3
      End Select
      If ActiveDocument.Range.End = RangeToCheck.Range.End Then Exit Do
      .Collapse wdCollapseEnd
      .Find.Execute
    Loop
  End With
Next
© www.soinside.com 2019 - 2024. All rights reserved.