VBA首先发现一个字符串

时间:2011-09-02 13:37:02

标签: vba ms-word find word-vba

我有以下数据

]
Word1
Word2
Word3
Word4[ Data1
]
Word1
Word2
Word3
Word4[ Data2
]

基本上我的宏搜索]*[查找[*]上方的数据 - 然后进行一些检查。然后我想找到下一部分([*])并对内容进行更多检查,然后再转到下一个]*[

目前它基本上找到了第一个]*[然后找到[*]但不是下一个]*[

headerSearch.Find.ClearFormatting
With headerSearch.Find
    .text = "(\])(*)(\[)"
    .Replacement.text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = True
End With

itemCount = 0
multipleRespoErrors = 0

Do While headerSearch.Find.Execute = True

Dim contentSearch As Object
Set contentSearch = Application.Selection


'find the item content
contentSearch.Find.ClearFormatting
With contentSearch.Find
    .text = "(\[)(*)(\])"
    .Replacement.text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = True
End With
contentSearch.Find.Execute
    findContent = lcase(Selection)
loop

任何想法?

2 个答案:

答案 0 :(得分:1)

我认为以下代码可以解决您的问题。但是,由于我们需要使用交替搜索模式,因此Word的搜索功能是不够的。为了补偿我创建了一个书签(Word中的一个不可见的标记,可以通过VBA代码访问),无论括号在哪里,并使用此系统来交替搜索模式。

第一步是生成书签。两个单独的搜索 - 左右括号各一个 - 完成此操作。每个书签的名称都为“书签”,并附加一个数字,以便按照外观的顺序以数字方式标记。

Public Sub PlaceBookmarks()

Dim SearchRange As Range
Dim BookmarkRange As Range
Dim x As Integer

x = 1
Set SearchRange = ActiveDocument.Range

SearchRange.Find.ClearFormatting
With SearchRange.Find
    .Text = "(\])"
    .Forward = True
    .Wrap = wdFindStop 'end bookmark creation at end of document
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = True
    While .Execute
        If .Found = True Then
            .Parent.Select
            Selection.Collapse
            Selection.MoveRight unit:=wdCharacter, Count:=1 'place bookmark to right of bracket
            Set BookmarkRange = Selection.Range
            ActiveDocument.Bookmarks.Add "Bookmark" & x, BookmarkRange
            x = x + 2
        End If
    Wend
End With

x = 2
Set SearchRange = ActiveDocument.Range
SearchRange.Find.ClearFormatting
With SearchRange.Find
    .Text = "(\[)"
    .Forward = True
    .Wrap = wdFindStop 'end bookmark creation at end of document
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = True
    While .Execute
        If .Found = True Then
            .Parent.Select
            Selection.Collapse
            Selection.MoveRight unit:=wdCharacter, Count:=1 'place bookmark to right of bracket
            Set BookmarkRange = Selection.Range
            ActiveDocument.Bookmarks.Add "Bookmark" & x, BookmarkRange
            x = x + 2
        End If
    Wend
End With

End Sub

接下来是添加代码以调整书签的位置,定义所需文本的范围,以及执行必要的操作。我调整了书签的位置,以便搜索期间所需文本的范围不包括括号。如果您需要在经过上述检查的文本中包含括号,请根据需要进行调整。

Public Sub FindRange()

Dim BookmarkCount As Integer
Dim x As Integer
Dim BookmarkRange As Range
Dim FirstPatternRange As Range

BookmarkCount = ActiveDocument.Bookmarks.Count

With ActiveDocument
    For x = 1 To BookmarkCount
        If .Bookmarks.Exists("Bookmark" & x + 1) Then

            'Move end bookmark to exclude bracket
            .Bookmarks("Bookmark" & x + 1).Select
            Selection.MoveLeft unit:=wdCharacter, Count:=1
            Set BookmarkRange = Selection.Range
            .Bookmarks.Add "Bookmark" & x + 1, BookmarkRange 'this moves the bookmark by re-adding it

            Set FirstPatternRange = .Range(.Bookmarks("Bookmark" & x).Range.Start, .Bookmarks("Bookmark" & x + 1).Range.End)
            'Perform checks on data between ][

            'Move leading bookmark to exclude bracket
            .Bookmarks("Bookmark" & x + 1).Select
            Selection.MoveRight unit:=wdCharacter, Count:=1
            Set BookmarkRange = Selection.Range
            .Bookmarks.Add "Bookmark" & x + 1, BookmarkRange 'this moves the bookmark by re-adding it

            'Move trailing bookmark to exclude bracket
            .Bookmarks("Bookmark" & x + 2).Select
            Selection.MoveLeft unit:=wdCharacter, Count:=1
            Set BookmarkRange = Selection.Range
            .Bookmarks.Add "Bookmark" & x + 2, BookmarkRange 'this moves the bookmark by re-adding it

            Set FirstPatternRange = .Range(.Bookmarks("Bookmark" & x + 1).Range.Start, .Bookmarks("Bookmark" & x + 2).Range.End)
            'Perform checks on data between []

            'Reset trailing bookmark for next iteration
            .Bookmarks("Bookmark" & x + 2).Select
            Selection.MoveRight unit:=wdCharacter, Count:=1
            Set BookmarkRange = Selection.Range
            .Bookmarks.Add "Bookmark" & x + 2, BookmarkRange 'this moves the bookmark by re-adding it

            x = x + 1
        End If
    Next
End With
End Sub

如果您计划将来对文本执行VBA操作,您可能希望编写For Each / Next以删除所有创建的书签。希望这会有所帮助。

答案 1 :(得分:0)

您可以根据以下内容使用某些内容。通过修改通配符查找表达式,该过程非常简单。

With headerSearch
  With .Find
    .ClearFormatting
    'Find ranges bounded by ] & [, excluding the final [
    .Text = "\][!\[\]]{1,}"
    .Replacement.Text = ""
    .Forward = True
    .Format = False
    .Wrap = wdFindStop
    .MatchWildcards = True
    .Execute
  End With
  Do While .Find.Found = True
    'Confirm that the next character is a [, just
    'in case we've gone beyond the last ] & [ pair
    If .Characters.Last.Next = "[" Then
      'Exclude the leading ], so all we're left with is the range between ] & [
      .Start = .Start + 1
      MsgBox "Text bounded by ] & [:" & vbCr & .Text
      With .Duplicate
        With .Find
          'Find the next range bounded by [ & ], excluding the final ]
          .Text = "\[[!\[\]]{1,}"
          .Forward = True
          .Format = False
          .Wrap = wdFindContinue
          .MatchWildcards = True
          .Execute
        End With
        If .Find.Found = True Then
          'Confirm that the next character is a ], just
          'in case we've gone beyond the last [ & ] pair
          If .Characters.Last.Next = "]" Then
            'Exclude the leading [, so all we're left with is the range between [ & ]
            .Start = .Start + 1
            MsgBox "Text bounded by [ & ]:" & vbCr & .Text
          End If
        End If
      End With
    End If
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With