2017-10-20 6 views
0

文書内の各単語の左側に数字を配置するには、どうすればよいですか?ランダムに色付けされた各単語に番号を付けます

私は、次のコードを使用して、各単語に番号を付けるために管理している:

Sub IndividualMacros() 
    Dim i&, w As Range 
    For Each w In ActiveDocument.Words 
     If w.Text Like "*[A-Z,a-z]*" Then 
      i = i + 1 
      w.InsertBefore i & " " 
     End If 
    Next 
End Sub 

しかし、どのように私は、各番号の色がランダム色に変更できますか?

+0

を把握する必要があります。この

を試してみてください? 'ActiveDocument.Words'とは何ですか? –

+1

@LeopoldJoy 'ActiveDocument.Words'はWord VBA内のコレクションです([MSDN documention](https://msdn.microsoft.com/en-us/vba/word-vba/articles/words-object-word) 「Words」オブジェクトは、「選択、範囲、または文書内の単語の集合です.Wordsコレクションの各項目は、1語を表すRangeオブジェクトです」。 – YowE3K

+0

@LeopoldJoy、それはすべて私のコードです:( –

答えて

1

は、あなたがより多くのコードを共有でした乱数のもの

Option Explicit 

Sub IndividualMacros() 
    Dim i As Long, w As Range 
    i = 1 

    Dim aaa As Range 
    For Each w In ActiveDocument.Words 
     If w.Text Like "*[A-Z,a-z]*" Then 
      w.Collapse wdCollapseStart  ' move pointer to before word 
      w.InsertBefore i & " "   ' w range contains number and space 
     ' w.select      ' you can use this to see the range 
      w.Font.Color = wdColorBlue  ' two ways to color text 
      w.Font.ColorIndex = 4 
      i = i + 1 
     End If 
    Next 
End Sub 
+0

ありがとう、兄。あなたのプログラムは完璧に動作します! –

関連する問題