View Full Version : Formatting line of text following a "!"
fluoronator
07-06-2007, 06:00 PM
I sometimes import snippets of programming code into word from TXT documents. I would like to use VBA in Word to format all of the comments in the code to GREEN. All text after a "!" is a comment, therefore, I would like to find each "!", select the text to the end of the line, then format the color. Here's my attempt...
Do While Selection.Find.Execute(findtext:="!") = True
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Font.Color wdColorGreen
loop
This doensn't work... it selects the same portion of text repeatedly.
mdmackillop
07-07-2007, 04:19 AM
Hi Fluo,
Welcome to VBAX
Sub ColourAfterText()
With Selection
.HomeKey Unit:=wdStory
Do While .Find.Execute(findtext:="!") = True
.Find.Forward = True
.Find.Wrap = wdFindStop
.EndOf Unit:=wdLine, Extend:=wdExtend
.Font.Color = wdColorGreen
.Collapse wdCollapseEnd
Loop
End With
End Sub
BTW, when you post code, select it and click the VBA button to format it as shown.
Regards
MD
fumei
07-08-2007, 04:02 AM
Just a question. Is this only EVER going to be for more than one "line"? You are never going to have comments longer than one line?
mdmackillop
07-08-2007, 04:11 AM
Hi Gerry,
How would you do this with Range? From what I've read wdLine is not an available unit.
fumei
07-09-2007, 09:50 AM
wdLine is NOT an option for a Range.Sub MakeGreen()
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
Do While (.Execute(Findtext:="!", Forward:=True) _
= True) = True
With r
.Expand Unit:=wdSentence
' you could also use Paragraph, as in
' .Expand Unit:=wdParagraph
.Font.Color = wdColorGreen
.Collapse Direction:=wdCollapseEnd
End With
Loop
End With
End SubYou may use Sentence, or Paragraph with a Range.
This may, or may not, be useful depending on:
1. what is terminating the "line"
2. if there are other sentences on that "line"
BTW: you notice I always use "line" - with quotation marks. It is a very slippery term, and people do not use it accurately very often.
So...IF the comment lines are terminated by a paragraph mark, then absolutely, use Range and expand it to the paragraph.
OR...if it is the only Sentence, you can still use Range.
mdmackillop
07-09-2007, 11:00 AM
Thanks Gerry,
I see what you mean by slippery. In this case, the "line" will depend upon the page layout view.
Regards
Malcolm
fumei
07-09-2007, 11:28 AM
Yes, and the other thing - especially dealing with text coming from other applications - is that the "line" may be terminated by a 'soft" return - Chr(11) - NOT a paragraph mark - Chr(13).
In other words, the line is terminated by a manual line break. This is very, very often the case.
This is "line 1" with some comment!(^l)
text now on "line 2" with some other(^l)
crap on the next line(^p)
Making a paragraph range on the above - searching for the "!" - would take everything. As it is ONE paragraph.
This is "line 1" with some comment!(^p)
text now on "line 2" with some other(^p)
crap on the next line(^p)
Making a paragraph range on the above - searching for the "!" - would take the first "line". As it is ONE paragraph.
fumei
07-09-2007, 12:18 PM
Interestingly enough, you can use one of the predefined bookmakrs ("\line") which returns the "line"...as a range.
However, it can only be used for the "line" the Selection is on, so there you go. Right back again. "line" is only really applicable (at least within VBA) to a Selection object.
That being said, if you are sneaky...you can use logic to figure out some information. That is because a range does have a .ComputeStatistics(wdStatisticsLines) property.
Further, you can use a second Range object to do some checking. Dim r As Range
Dim r2 As Range
Set r = ActiveDocument.Range
With r.Find
Do While (.Execute(Findtext:="!", Forward:=True) _
= True) = True
Set r2 = ActiveDocument.Range( _
Start:=r.End, End:=r.End)
Select Case Asc(r2.Words.Last)
Case 11
' terminates with soft return
Case 13
' terminates with paragraph mark
Case Else
' the "!" is NOT last character
End SelectThis searches for the "!" then checks what follows it.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.