Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-05-2022, 05:38 AM
Harvi007 Harvi007 is offline VBA Marco to change font colour of every other word Windows 11 VBA Marco to change font colour of every other word Office 2019
Novice
VBA Marco to change font colour of every other word
 
Join Date: Aug 2022
Posts: 3
Harvi007 is on a distinguished road
Question VBA Marco to change font colour of every other word

Hi Everyone,

I wonder if anyone can help me out with a VBA macro.

I have a word document and I'd like to change the font colour of every other word, so either all the odd words are say Red or all the even words are Red, the remaining words stay the default colour.

So I initially started with a word count in the hope of changing this to only count odd or even words, but this didn't work out for me.

I then went on to loading all the words into an array with the hope of picking out the odd or even indexed words and the somehow change the colour, but again I'm stuck and can't work out how to do it.

I'm not an expert but can do the basics, I've looked around on the web and found something that came close, it changed the colour of every letter but even then it was based on case, so if it was A it would be Red and B would be Blue. It worked, but this didn't help me as I couldn't work out how to get it to look at the words.

For the record, a word I'm referring to as text in-between 2 spaces. As I'm working with a different language, sometimes you with get numbers which are not words but are in-between spaces and thus would be treated as a 'word'.

Is there anyone that could help and guide me on how I can attain my goal?

Any help would be much appreciated.



Thank you
Reply With Quote
  #2  
Old 08-05-2022, 01:47 PM
Harvi007 Harvi007 is offline VBA Marco to change font colour of every other word Windows 11 VBA Marco to change font colour of every other word Office 2019
Novice
VBA Marco to change font colour of every other word
 
Join Date: Aug 2022
Posts: 3
Harvi007 is on a distinguished road
Cool A potential solution... it works..

So after messing about, and finding bits of code online, i've managed to come up with this...

I hope it helps others looking to do the same thing.
----------------------------------------------------------------------------------------
Sub wordslistChange()

Dim avar As Variant

avar = Split(Replace(ActiveDocument.Range.Text, vbCr, " "))

For i = LBound(avar) To UBound(avar)

i = i + 1

'MsgBox avar(i)

With Selection.Find
.ClearFormatting
.Text = avar(i)
On Error Resume Next
.MatchCase = True
.MatchWholeWord = True
.Replacement.ClearFormatting
.Replacement.Text = avar(i)
.Replacement.Font.Color = wdColorRed 'change colour as required
.Forward = True
.Wrap = Word.WdFindWrap.wdFindContinue
.Execute Replace:=Word.WdReplace.wdReplaceOne
End With

Next i

End Sub
------------------------------------------------------------------------------------------

This basically populates the array and then does the odd even colour change magic.

Have fun people and feel free to mod the code to make it even more efficient.

Thank you

Last edited by Harvi007; 08-05-2022 at 10:32 PM.
Reply With Quote
  #3  
Old 08-05-2022, 02:01 PM
Harvi007 Harvi007 is offline VBA Marco to change font colour of every other word Windows 11 VBA Marco to change font colour of every other word Office 2019
Novice
VBA Marco to change font colour of every other word
 
Join Date: Aug 2022
Posts: 3
Harvi007 is on a distinguished road
Exclamation Stop Press

Stop press...

so I've just checked my text and although it should work as expected, there is a slight issue...

It seems to colour the last word on a line and the first word on the line immediately after in the same colour when it should be true Odd Even colouring.

Any help on this would be much appreciated..

So close yet so far

Edit::

I put a msgbox in the code to see what the array was outputting and saw that the array is adding the newline return in the middle of the 2 words making them show as one. A sample of this is 'close.□Then'

The □ is the new line or carriage return.

For now I can get around this by changing the page size to that the next word goes to the next line without adding a carriage return, but this messes up the formatting.

Please help

Edit Again::

So I've narrowed it down to the way the array is being split with the code avar = Split(Replace(ActiveDocument.Range.Text, vbCr, " "))

I've tried to replace the vbCr with both vbCrLf and vbLf with no avail.

The carriage return used in the text is shift+enter if that helps.

Last edited by Harvi007; 08-05-2022 at 10:38 PM.
Reply With Quote
Reply

Tags
macro font colour, vba change colour

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Word Macro Recorder does not record font colour selection Ramkay Word 1 12-02-2020 07:48 AM
VBA Marco to change font colour of every other word VBA - Word how to globally change the font and font size in footnotes thomasoj Word VBA 3 01-15-2020 06:26 AM
VBA Marco to change font colour of every other word VBA to change font colour in email body on send RealmOfCOnfusion Outlook 1 06-30-2016 09:55 PM
VBA Marco to change font colour of every other word Change font colour when tasks are completed meileetan Project 3 09-12-2012 07:09 AM
VBA Marco to change font colour of every other word Change Colour Theme in Word with VBA Davidoff78 Word VBA 1 06-28-2012 05:23 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:17 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2022, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2022 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft