2017-10-20 79 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

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