我認爲下面的代碼將解決您的問題。但是,由於我們需要使用交替搜索模式,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 /下一頁刪除創建的所有書籤。希望這可以幫助。
你的第二次搜索可能不起作用,因爲'contentSearch'包含第一個'find'語句的結果,而不是文檔的整個結尾 – JMax