【问题标题】:VBA and MSWord: Use multiple values of a find parameter in Find/Execute routineVBA 和 MSWord:在查找/执行例程中使用查找参数的多个值
【发布时间】:2019-02-12 04:32:14
【问题描述】:

我有一个查找/执行例程,它以我的自定义样式Bullet_Type_1_Level_1(一种自定义项目符号列表样式)查找段落,并处理这些段落。 (它检查给定范围内的每个段落,看看它是否在一个句点内终止,但这对于这个问题并不重要)。该例程目前工作正常,但我想扩展它以搜索我的大纲列表的其他级别(这会转化为其他样式)并在另一个列表中搜索样式。有没有一种compact 的方式让我的代码同时在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

【问题讨论】:

  • 为了清楚起见,您想对三种样式中的每一种都做同样的事情(省略的代码)?
  • 对任何采用三种样式中的任何一种的段落,都可以。
  • 我想我知道该怎么做了。首先,删除 Selection.Find 和相关代码。然后,因为无论如何我都在处理每一段,所以只需有一个 If/Then 循环来检查三件事中的一件是否为真。如果其中至少有一个为真,那么我处理该段落: If para.Style = "A" Or para.Style = "B" Or para.Style = "C" Then...Else 'do nothing End If。
  • @JoshG 如果您已经解决了自己的问题,请随时回答(此处允许)并将其标记为已接受的答案,让其他人知道您不再需要帮助。谢谢!
  • 那么,您真的不需要指定.Wrap 标准吗?

标签: vba replace ms-word


【解决方案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

可能不是最漂亮的解决方案 - 文字不是我的强项☺。

【讨论】:

  • 非常好。我喜欢!我也想出了一个解决方案,但我认为你的也一样好。
【解决方案2】:

如果有段落不是这些样式,另一种方法可能会更快:

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

【讨论】:

    猜你喜欢
    • 2011-08-21
    • 2017-02-01
    • 1970-01-01
    • 2014-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-18
    • 2014-08-22
    相关资源
    最近更新 更多