I create, copy, and use a lot of macro routines, mainly in Excel. Often I want to pass these on to others, so I try to comment all the macros, the copy them to Word documents with explantory text before and after the macro. When I do this, I'd like to be able to have all of my comment lines within the macros come out in green. Since every comment line begins with an apostrophe, it seems like it would be simple to have a Word macro that colors all lines beginning with an apostrophe green, but I don't know how to do it. I'd appreciate some help.
Thanks,
John
Last edited by jomili; 01-25-2012 at 05:37 PM.
jomili,
Word macro that will detect an apostrophe and color the text green:
Sub tgr() Dim p As Paragraph For Each p In ActiveDocument.Paragraphs If Left(Trim(p), 1) = "'" Then p.Range.Font.Color = 25600 Next p End Sub
Last edited by tigeravatar; 01-25-2012 at 05:25 PM.
Hope that helps,
~tigeravatar
Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble
Tigeravatar,
Thanks for your help. It appears to work on my samples, but looking at the code it seems it would color the whole paragraph green. Since I just want the macro comment lines to be colored, would there be a case where Word would look at multiple lines of macro code as all being in the same paragraph? Would we want to change it to look at the line instead of the paragraph?
In the visual basic editor, the only way to go from a comment line to the next code line is to use the Return key. Word sees that as a new paragraph, so there should never be any overlap. Let me know if you do find any examples where it doesn't work as intended though.
Hope that helps,
~tigeravatar
Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble
Thanks for the explanation. I'll look for it, and if I spot any I'll let you know. Thanks so much for your help.
Tigeravatar,
I have a case where the macro doesn't appear to be working. I've attached my example. What needs to be done to make it work?
I ran the macro on that doc and only the comment lines were set to green text. How is it not working?
Hope that helps,
~tigeravatar
Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble
Sorry, I should have given more details. Here's what's happening;
I put the macro in Normal, in Module 1.
I open the Word document, go to Tools>Macro>Macros>then click on the macro and hit Run. Nothing happens on this document. I added a line to the macro:When I rerun the macro, the message box pops up, but no change happens on the document.MsgBox "Macro DID run"
Do I need to put the macro directly into each document? I was thinking Normal worked kind of like the PERSONAL.xls, where I'm used to storing my macros.
I've attached another example. In this one, the macro works on the line beginning with " 'Makes every number " but doesn't work on the line beginning with " 'Remove Subtotals " or any of the lines further down. Visually, it appears that the apostrophe differs from the first one, but I don't know how to determine that for sure. A lot of these are copied from the web and pasted into Word, so maybe I've got a different character than an apostrophe in some. If that's the case, can we adapt this to handle the different variations?
Thanks for your help on this.
John
TigerAvatar,
Please forgive me; my mother gave birth to an idiot.
Apparently I never closed out Word and saved my Normal. After my last post I shut down Word and got onto other business, then reopened Word for another task and tried the macro again, and it wasn't there! I figured out what I'd done and replaced the macro, saved, shut down Word, reopened, and now it appears to be working like a charm. Sorry to get you alarmed. Thanks for all your help!
No worries, and anytime![]()
Hope that helps,
~tigeravatar
Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble
TigerAvatar,
I've got one more condition I'd like to overcome. I've attached the document to this post. The macro works fine, but when it gets to the code below it doesn't color the comments green. I know this isn't a condition I originally mentioned, but is there a way to capture these types of comments as well?TopRow = 1 iCol = 7 'number of columns in the table strCol = "A" ' column to check for last row
jomili,
Give this a try. If there is an apostrophe in the line, everything from the first apostrophe to the end of the line is colored green:
Sub tgr() Dim p As Paragraph Dim myRng As Range For Each p In ActiveDocument.Paragraphs If InStr(p, "'") > 0 Then Set myRng = p.Range myRng.Start = myRng.Start + InStr(p, "'") - 1 myRng.Font.Color = 25600 End If Next p End Sub
Hope that helps,
~tigeravatar
Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble
Sweet! Exactly what was needed. Thank you so much!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks