如何将 .find 结果存储在数组中?
这个宏使用通配符找到这之间发生的每根弦<sm>
,以<fin>
Selection.Find.ClearFormatting
With Selection.Find
.Text = "<sm>?<fin>"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
这部分计算<sm>
文本中存在的数量以定义数组长度:
Dim I As Long
Dim J As Long
Dim NumSm As Long
Dim TargetText As String
TargetText = "<sm>"
J = 1
I = 1
While I > 0
I = InStr(J, ActiveDocument.Range.Text, TargetText)
If I > 0 Then
NumSm = NumSm + 1
J = I + 1
End If
Wend
Dim SmArr() As Variant
ReDim SmArr(0 To NumSm)
我想存储在数组中查找结果:SmArr()
。
尝试一些类似的东西:
Sub Demo()
Dim i As Long, SmArr()
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "<sm>*<fin>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Execute
End With
Do While .Find.Found
i = i + 1
ReDim Preserve SmArr(i)
SmArr(i) = Split(Split(.Text, "<fin>")(0), "<sm>")(1)
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
For i = 1 To UBound(SmArr)
MsgBox SmArr(i)
Next
End Sub
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句