縮寫搜索Range.Find - 在wdInFieldResult用;實測值麻煩
當findRng.Find成功地發現了一個域結果中搜索文本(如目錄)下一個.Execute開始在TOC的開始而不是在之前的findRng.Find文檔的開始位置。這可以通過選擇findRng.select來直觀地顯示。每findRng的開始和結束的屬性,選擇不應該包括TOC的開始,但它確實,這似乎是什麼.Find方法也使用,因爲它成爲蘋果公司總部地址;即一個無限循環。 ;)
,你可以看它是否符合預期,直到附近的子程序底部的.Execute聲明findRng.Start和.END的值。
除非有人能解決的範圍內復位的問題,我很樂意只是尋找一種方法來快速確定觸發wdInFieldResult產生真正與生活運動場上的.END位置。
Sub findAcronyms()
Dim findRng As Range, tempRng As Range
Dim oFld As Field
Dim findStr As String, acroStr As String
Dim acroTbl As Table
'################# test code
Dim testMode As Boolean
Dim testIdx As Long, testSize As Long, i As Long
testMode = True
testIdx = 0
testSize = 25
If testMode Then
ThisDocument.ShowRevisions = True
ThisDocument.TrackRevisions = True
End If
Quiet (Not testMode)
'#################
'set acroTbl to ThisDocument's Acronym table
Set findRng = ThisDocument.Content
findStr = "ACRONYMS"
With findRng.Find
.ClearFormatting
.Style = WdBuiltinStyle.wdStyleHeading1
.Text = findStr
.Forward = False
.Wrap = wdFindStop
.Format = False
.Execute
If Not .Found Then
MsgBox findStr & ": not found!", vbExclamation
Stop
Debug.Print "Debug the issue..."
Else
findRng.MoveStart wdTable
findRng.Expand wdTable
Set acroTbl = findRng.Tables(1)
End If
End With
'find occurrences of "(" and if closing parens "(" is within 7 characters then add to end of Acronym table
Set findRng = ThisDocument.Content
findStr = "("
With findRng.Find
.ClearFormatting
.Text = findStr
.Forward = True
.Wrap = wdFindStop
.Format = False
.Execute
Do While .Found 'until Find finds other than itself or EOD
'################# test code
If testMode Then
findRng.Select
Debug.Print findRng.Start
testIdx = testIdx + 1
If testIdx > testSize Then
Stop 'and Debug if necessary
Exit Sub
End If
End If
'################
i = findRng.MoveEndUntil(")", 7)
If i > 2 And Not findRng.Text Like Left(findStr & "#######", _
Len(findRng.Text)) Then
'check for pre-existence of acronym before adding to table
Set tempRng = ThisDocument.Range(acroTbl.Columns(1).Cells(2).Range.Start, _
acroTbl.Columns(1).Cells(acroTbl.Columns(1).Cells.Count).Range.End)
tempRng.Find.ClearFormatting
With tempRng.Find
.Text = Mid(findRng.Text, 2, i)
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
If Not .Found Then 'proceed with adding new acronym to table
With acroTbl.Rows
.Add
With .Last
.Cells(1).Range.Text = Mid(findRng.Text, 2, i)
i = findRng.Start
findRng.Collapse wdCollapseStart
findRng.MoveStart wdCharacter, -1
findRng.MoveStart wdWord, _
-.Cells(1).Range.Characters.Count
.Cells(2).Range.Text = Trim(findRng.Text)
findRng.Start = i + 1
Debug.Print .Cells(1).Range.Text, .Cells(2).Range.Text
End With
End With
End If
End With
Else: findRng.MoveStart wdWord 'skip over 2 letter acronyms
End If
If findRng.Information(wdInFieldResult) Then
findRng.MoveStart wdParagraph 'in lieu of a better solution I need to determine how to get out of the field result
ElseIf findRng.Information(wdWithInTable) Then
If findRng.InRange(findRng.Tables(1).Range.Cells(findRng.Tables(1).Range.Cells.Count).Range) Then 'test if in last cell
findRng.Expand wdTable
findRng.Collapse wdCollapseEnd
Else
findRng.MoveStart wdCell
End If
Else
findRng.MoveStart wdWord
End If
'################# test code
If testMode Then findRng.Select
'################
findRng.Collapse wdCollapseEnd
findRng.End = ThisDocument.Content.End
.Execute
Loop
End With
Stop
End Sub
在使用遞歸過程進行進一步調查之後,其中只有一部分Field.Result被反饋到Sub也會導致相同的行爲。也就是說,在執行.Execute語句時,整個Field.Result將從頭開始重新處理。我認爲包含字段結果信息會使.Find函數被破壞。 – IronX