我需要将所有出现的上标数(这是与引用相关的引文键数)存储到数组中。
示例:
一些示例引用 2 另一个引用 31,16,83,9-15 还有一些引用 18,2,30
预期输出:
stored_content=[2,31,16,83,9-15,18,2,30]
当我找到上标数字时,它会搜索为单个数字。例如,它会18
分别找到1
和8
。我尝试如下:
Sub Sample()
Dim c As Range
Set c = ActiveDocument.Content
c.Find.ClearFormatting
With c.Find
.Forward = True 'move forward only
.Highlight = False
.Font.superscript = True
.Text = "^#"
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
c.Find.Execute
While c.Find.Found
Debug.Print c.Text
c.Find.Execute
Wend
End Sub
答案 0 :(得分:0)
这应该完全符合您的要求。
我取消Find.Text = "^#"
,然后找到任何SuperScript字体,然后评估FoundText
没有数字,然后我用逗号分隔符拆分找到的文本以获得个别引用。< / p>
请注意,您的图书馆需要正则表达式。在VBA中选择工具,然后选择参考。
Sub SomeSub()
Dim RegExpFind As Object
'You will need Microsoft VBScript Regular Expressions added to your Library
Set RegExpFind = New RegExp
Dim DocumentContent As Range
Set DocumentContent = ThisDocument.Content
Dim FoundText As String
Dim FoundTextArray As Variant
Dim ArrayPosition As Long
With DocumentContent.Find
.Font.Superscript = True
.Text = ""
.Replacement.Text = ""
.Forward = True
'Stops the Find after reaching the end of the document
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Do While .Execute
If .Found = True Then
'Because your citations do not have spaces between the numbers and only a comma
FoundText = DocumentContent.Text
With RegExpFind
.Pattern = "[a-zA-z]"
.IgnoreCase = True
'If there are no letters
If Not .Test(FoundText) Then
If Len(FoundText) <= 2 Then
Debug.Print FoundText
Else
FoundTextArray = Split(FoundText, ",")
For ArrayPosition = 0 To UBound(FoundTextArray)
'Here is where can then do other things with the numbers between the commas
Debug.Print FoundTextArray(ArrayPosition)
Next ArrayPosition
End If 'Ending Len(FoundText) <= 2
End If 'Ending .Test(FoundText)
End With 'Ending RegExpFind
End If 'Ending .Found = True
Loop
End With 'Ending DocumentContent.Find
End Sub