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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

bold the last line in a WORD paragraph

Status
Not open for further replies.

kaloger

Technical User
Dec 1, 2004
10
US
hi all

i am looking for how to use for next to select and bold the last line in a paragraph. each paragraph has 2 -8 line in it. each line ends with a new line character. any for next experts out there?

thanks
bill
 
Hi Bill,

Firstly it isn’t always possible to bold only the last line of a paragraph. As an example copy this paragraph into Word. Use the default 12-point Times New Roman font on either A4 (210 mm wide) or Letter (8½" wide) paper, in Portrait orientation, with both left and right margins set to 1¼" (3.17 cm) and manually select and bold the last line. You will see that, because the bold font is bigger (wider) than the normal one, that the last line has overflowed and that the last two lines of the paragraph are bold. Try to reduce the quantity of bolded text and you will no longer have enough bolded text to force an extra line so the end result will be part of the last line being bold. It doesn’t matter how much fiddling around you do, you cannot guarantee just a single bold line.

That said, the following code will take the last line of each paragraph and bold it - possibly causing two bold lines at the end of some paragraphs. As a rule I try to avoid selecting document elements in VBA code but, in Word, that isn't always possible and I don't know any way to get at line elements without selecting, so here is one way to do it:

Code:
[blue]For Each p In ActiveDocument.Paragraphs
    p.Range.Select
    Selection.Collapse wdCollapseEnd
    Selection.MoveLeft wdCharacter
    Selection.Bookmarks("\Line").Range.Font.Bold = True
Next[/blue]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at [url=http://www.vbaexpress.
 
Hi Tony...back....long story

You do not need to select the whole paragraph. You just need to move the selection point. You can do that by setting the Selection start to the paragraph end - less 2 to cover the last paragraph problem.

Code:
For Each p In ActiveDocument.Paragraphs
    Selection.Start = p.Range.End - 2
    Selection.Bookmarks("\Line").Range.Font.Bold = True
Next

I agree it is odd that you can get a line COUNT of a range object, but the individual lines are not exposed to it. The predefined bookmark "\line" is not available to a Range.


Gerry
 
Hi Gerry,

Welcome back! All the best stories are long.

Also you can't use wdLine to Expand a Range unless it's the Selection. It's like, when is a Range not a Range?

Oh, and, nice improvement to the code! [wink]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at [url=http://www.vbaexpress.
 
It is because (innately) the Selection object has calculated Start and End integers. The Range object always has defined Start and End integers. As a "line" is always calculated (rather than defined), it is not exposed to the Range object.

Of course the Selection CAN be defined. However, when you are moving the mouse sweeping up space, the Start and End property values are calculated moment to moment. Which is why you and I, and other programatically correct souls use the range object as much as possible.

You can .ComputeStatistics on a range object, and get a line count. However, is calculated, and you still can not redefine the Range to, say, line 3 of the 6 lines computed. There is no way to define the range start and end integers to the line start and end integers without going to the line and calculating them. Going there means...well gosh...there you are. Selection.

When is Range not a Range? ...when it is calculated.

Gerry
 
hi there,

so would it be appropriate to refer to you two as "rangers"? :)

thanks for the code and the tutorial on selections, ranges, etc. very interesting. there is more to this then i had thought.


thanks
bill
 
Well it is nice not to be a Lone one....

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top