2011-09-02 37 views
0

请与我这一个裸露的第一次出现了难以解释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 

任何想法?

+1

你的第二次搜索可能不起作用,因为'contentSearch'包含第一个'find'语句的结果,而不是文档的整个结尾 – JMax

回答

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 /下一页删除创建的所有书签。希望这可以帮助。