Underline first sentence in all Heading 2 paragraphs

T

TryingVBA

Hello,

I'm trying to create a macro that will find all paragraphs formatted with
the Heading 2 style, then Underline from the start of the paragraph to the
first period.

As I have been trying to figure this out myself, I think I need to loop
through the paragraphs collection, use an if statement to find Heading 2 and
then use collapse.start to go to the beginning and then selection.extend to
find the period. Can anybody provide a sample of how this is done? Many
thanks.
 
K

Klaus Linke

Hi,

Comments inline...
I'm trying to create a macro that will find all paragraphs formatted with
the Heading 2 style, then Underline from the start of the paragraph to the
first period.

As I have been trying to figure this out myself, I think I need to loop
through the paragraphs collection,

Dim myPara as Paragraph
For each myPara in ActiveDocument.Paragraphs
use an if statement to find Heading 2 and

If myPara.Style = ActiveDocument.Styles(wdStyleHeading2) Then
then use collapse.start to go to the beginning and then selection.extend
to find the period.

' You don't need to do these steps. Just format the first sentence of myPara
directly:

myPara.Range.Sentences(1).Font.Underline = wdUnderlineSingle
End If
Next myPara

Regards,
Klaus
 
F

fumei via OfficeKB.com

It would be better if you are using Styles fully and properly to have a
Character style that is underlined (say MyUnderline). Then apply that style,
as in:

Dim oPara As Paragraph
For Each oPara In ActiveDocument.Paragraphs
If oPara.Range.Style = "Heading 2" Then
oPara.Range.Sentences(1).Style = "MyUnderline"
End If
Next

It does seem a little odd to have multiple sentences on a Heading 2.
 
K

Klaus Linke

You're right... although I make exceptions for bold, italic and underline,
since most people use the toolbar buttons for those, so even if I'd create
character styles for them, next to nobody would use them ;-)

A better solution might be "real" run-in sideheads:
http://sbarnhill.mvps.org/WordFAQs/RunInSidehead.htm

You could automate that by macro also, but the macro would depend on which
of the different ways to do run-in sideheads you choose.

Klaus
 
T

TryingVBA

Thanks! That worked great, what you showed me can be applied to so many
things. However there is one problem that I did not anticipate. It
underlines the period and trailing spaces after the sentence. I need the
underline to stop before the period. This macro is for legal documents where
they have

"1.1 This Is My Underlined Heading. Bla blala bla not underlined bla bla."

They do not underline the period or trailing spaces, just the underlined
heading. We use heading styles to number paragraphs.

Is there any way to fine tune this? I guess we can't use the sentence
object? Please advise, thanks again
 
J

Jean-Guy Marcil

TryingVBA said:
Thanks! That worked great, what you showed me can be applied to so many
things. However there is one problem that I did not anticipate. It
underlines the period and trailing spaces after the sentence. I need the
underline to stop before the period. This macro is for legal documents where
they have

"1.1 This Is My Underlined Heading. Bla blala bla not underlined bla bla."

They do not underline the period or trailing spaces, just the underlined
heading. We use heading styles to number paragraphs.

Is there any way to fine tune this? I guess we can't use the sentence
object? Please advise, thanks again

A sl;ight modification to Klaus' code will do it:

Dim myPara As Paragraph
Dim rngPara As Range

For Each myPara In ActiveDocument.Paragraphs
If myPara.Style = ActiveDocument.Styles(wdStyleHeading2) Then
Set rngPara = myPara.Range.Sentences(1)
With rngPara
.MoveEnd wdCharacter, -2
.Font.Underline = wdUnderlineSingle
End With
End If
Next myPara
 
T

TryingVBA

WORKS PERFECTLY, THANKS!

Jean-Guy Marcil said:
A sl;ight modification to Klaus' code will do it:

Dim myPara As Paragraph
Dim rngPara As Range

For Each myPara In ActiveDocument.Paragraphs
If myPara.Style = ActiveDocument.Styles(wdStyleHeading2) Then
Set rngPara = myPara.Range.Sentences(1)
With rngPara
.MoveEnd wdCharacter, -2
.Font.Underline = wdUnderlineSingle
End With
End If
Next myPara
 
F

fumei via OfficeKB.com

Hi Klaus,

"You're right... although I make exceptions for bold, italic and underline,
since most people use the toolbar buttons for those,"

Sad, but true.

Except...I do NOT use the standard toolbar buttons. I use Styles exclusively,
even for Bold, Underline and Italics. The button code (as well as keyboard
shortcuts) are overwritten so they apply the appropriate character style.

I just hate those:

StyleName + Italics + Underline + whatever

in the Styles dropdown. Hate them. I refuse to have ANY.

Klaus said:
You're right... although I make exceptions for bold, italic and underline,
since most people use the toolbar buttons for those, so even if I'd create
character styles for them, next to nobody would use them ;-)

A better solution might be "real" run-in sideheads:
http://sbarnhill.mvps.org/WordFAQs/RunInSidehead.htm

You could automate that by macro also, but the macro would depend on which
of the different ways to do run-in sideheads you choose.

Klaus
It would be better if you are using Styles fully and properly to have a
Character style that is underlined (say MyUnderline). Then apply that
[quoted text clipped - 9 lines]
It does seem a little odd to have multiple sentences on a Heading 2.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top