2016-04-01 1 views
0

上付き文字番号(参照に関連する引用キー番号)のすべてを配列に格納する必要があります。Word VBAを使用して上付き数字全体を取得する方法は?

サンプル:

いくつかの例を引用別の引用31,16,83,9-15 といくつかのより多くの引用期待18,2,30

と出力:

stored_content=[2,31,16,83,9-15,18,2,30] 

上付き数字を見つけると、1桁の数字が検索されます。たとえば、181とし、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

これは、あなたが必要とするものとまったく同じです。

私はFind.Text = "^#"を手に入れていないので、スーパースクリプトフォントを見つけてFoundTextに数字がないと評価して、見つかったテキストをカンマ区切り文字で分割して個々の引用を取得します。

ライブラリには正規表現が必要です。 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 
関連する問題