最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

vba - Changing (alternating) the colors of sentences (not highlights) - Stack Overflow

programmeradmin2浏览0评论

I'm looking for a VBA Word macro that assigns a different color to every sentence of a text. This could consist of three or four different colors that alternate. I want it to color the words themselves, the font; in other words, I do NOT want text to be highlighted.

I researched the topic already. The only macros I find are those that highlight.(I already asked this question before. And someone on stackoverflow had given me the perfect answer! But unfortunately the macro is no longer stored in my VBA (not sure what happened) and the thread of my former question was deleted.

I'm looking for a VBA Word macro that assigns a different color to every sentence of a text. This could consist of three or four different colors that alternate. I want it to color the words themselves, the font; in other words, I do NOT want text to be highlighted.

I researched the topic already. The only macros I find are those that highlight.(I already asked this question before. And someone on stackoverflow had given me the perfect answer! But unfortunately the macro is no longer stored in my VBA (not sure what happened) and the thread of my former question was deleted.

Share Improve this question edited Mar 15 at 17:04 braX 11.8k5 gold badges22 silver badges37 bronze badges asked Mar 15 at 16:03 Two CentsTwo Cents 34 bronze badges 5
  • For sentence you mean Subject + Verb + Object? So isolate each substructure containing a verb, or anything up to a period? – Oran G. Utan Commented Mar 15 at 16:08
  • IIRC, this is at least the third time you've asked this question and on at least one occasion you were given an answer you said met your needs. – macropod Commented Mar 15 at 21:52
  • @macropod it looks like the answer for that question was deleted, probably after the question was closed for not having enough details? – braX Commented Mar 15 at 23:05
  • AFAIK, it wasn't closed. Regardless, the OP had an answer that supposedly addressed the problem. So why isn't he/she even acknowledging that and posting the code already supplied? – macropod Commented Mar 16 at 8:00
  • The answer was deleted by someone who was not the OP, nor the answerer. Not sure why. It seems the OP was not able to copy the answer before it was deleted. – braX Commented Mar 16 at 8:24
Add a comment  | 

1 Answer 1

Reset to default 1

You should be able to modify this to get something that does it the way you want.

Sub ChangeSentenceColors()
    Dim sentence As Range
    Dim randColor As Long

    For Each sentence In ActiveDocument.Sentences
        randColor = RGB(Int((255 - 0 + 1) * Rnd + 0), _
                        Int((255 - 0 + 1) * Rnd + 0), _
                        Int((255 - 0 + 1) * Rnd + 0))
        sentence.Font.Color = randColor
    Next
End Sub

Here is the previous answer that was deleted (not my answer).

Sub ColorSentences()
Dim doc As Document
Dim sentence As Range
Dim colors As Variant
Dim colorIndex As Integer
' Define the colors (change these if needed)  
colors = Array(wdColorRed, wdColorBlue, wdColorGreen, wdColorDarkYellow, wdColorViolet)  
  
Set doc = ActiveDocument  
colorIndex = 0  
  
' Loop through each sentence in the document  
For Each sentence In doc.Sentences  
    sentence.Font.Color = colors(colorIndex Mod 5)  
    colorIndex = colorIndex + 1  
Next sentence  
  
MsgBox "Sentences have been colored!", vbInformation  
End Sub
发布评论

评论列表(0)

  1. 暂无评论