如何将.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()
。
答案 0 :(得分:0)
您的问题有点难以理解,但是我会尽力而为。您是否考虑过使用收藏集?使用集合,您不必担心定义数组长度。
Dim newColl as new collection
我会用键将项目添加到集合中,以便您仍然可以像选择数组一样选择要读取的数据。
newColl.add I, NumSm
NumSm是您检索项目的关键:
newColl.item X
或者只是遍历一个集合
Dim collObj as object
for each collObj in newColl
'stuff
next collObj
答案 1 :(得分:0)
尝试以下方法:
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