2017-10-20 42 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文檔](https://msdn.microsoft.com/en-us/vba/word-vba/articles/words-object-word) 「Words」對象爲「選擇,範圍或文檔中的單詞集合,Words集合中的每個項目都是代表一個單詞的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

非常感謝你,兄弟。你的程序工作完美! –