Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Colour lines of text in word

Status
Not open for further replies.

JasonEnsor

Programmer
Sep 14, 2010
193
GB
Hi,

After reading replies to another question i posted on here i decided to use word to store my snippets of code, is it possible that when i paste the code in to a word document that it automatically colours the comments a different colour? if so how?

My thought is i could check the text and anything that starts with ' and ends with ' could be assumed to be a comment as that is what i would normally do to my VBA comments. however i wouldnt know where to start in order to do this. i know in excel that you can use Like "*@*" for email addresses but how/can i do the same in word?

Any help would be appreciated
 
Hi Jason,

You could do this with a wildcard Find/Replace in Word, using:
Find = '[!^l"]{1,}^l
or, if each line is a separate paragraph:
Find = '[!^13"]{1,}^13
and:
Replace = ^&
and formatting the replacement font to your desired comment colour. What the above does is to find line with a ' character and replace from there to the end of the line with the same text in the nominated colour.

You could, of course, turn the above into a macro - even the macro recorder does a tolerably good job in this case.

Note: If you have tick marks (ie ') in the code, those code lines may likewise be coloured - I've tried to minimise this possibility by stipulating in the Find expression that there can't be a double-quote character after the '. I imagine it would take a fair bit of programming effort to properly handle all possible scenarios, but the above should handle most.


Cheers
[MS MVP - Word]
 
If I decided to store macros as pure text, I'd avoid word. I would rather save them as 'txt' files and change extension to 'bas'. There are text editors for programmers, I prefer free Notepad++ ( It allows to select language (to VB here) and the code is automatically coloured.

combo
 
The issue of trying to get nicely colored/formatted text of VBA code in Word has come up time and time again. It is messy and a pain.

If it is only the comments you want colored, then sure, use macropod's suggestion. However, if you want to duplicate the color formatting the VBE can do...do not do it in Word.


unknown
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top