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
1 Answer
Reset to default 1You 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