PDA

View Full Version : Apply Style all Heading 2 first sentence Before Period



jec1
11-13-2013, 07:44 AM
Hi, I would like to add a macro to global ribbon that adds a style to all heading 2 to first sentence (period) before period (fullstop is excluded) for TOC inclusion.

Any ideas without doing a userform and don't want to use style separator.

James

fumei
11-13-2013, 01:25 PM
I am not quite following. Are you saying:

1. find all Heading 2 styles
2. apply a CHARACTER style to the first sentence.

I do not know why you mention a userform. And do you know that doing this will NOT affect how the TOC looks?

jec1
11-15-2013, 04:40 PM
I am not quite following. Are you saying:

1. find all Heading 2 styles
2. apply a CHARACTER style to the first sentence.

I do not know why you mention a userform. And do you know that doing this will NOT affect how the TOC looks?

Hi Fumei

It will affect the TOC.

The document is already set up - I want to run through and apply a character style to all heading 2 levels named "Heading 2 TOC" which applies attributes to the text in the first sentence. That run in heading is included in my TOC - otherwise I would apply it as I go automatically which is what I do using linked paragraph styles (character styles) and autoformat.

Heading 2 is numbered and linked Heading 2 TOC is a linked character style that needs to be applied to the first sentence. This is an American format. This allows my TOC to be

Table of Contents

1. Sunset
1.1 Sunset Date

2 Determination
2.1 Run in heading

{ TOC /t "Heading 1,1,Heading 2 TOC,1" /h}

So I need to run through the whole document and apply the named style to Level 2 first sentence.
Thanks anyone who can assist!

1. Sunset
1.1 Sunset Date. This is not working forme.
1.2 Disclosing the facts. This works for me.

2. Determination
2.1 Run in heading. Text following.

Regards
James

macropod
11-15-2013, 05:42 PM
Heading 2 is numbered and linked Heading 2 TOC is a linked character style that needs to be applied to the first sentence. This is an American format.
Huh? A multi-national, two-continent-dependent format? I think someone's having a lend of you ... or you're trying to have a lend of us!

Try something based on:

Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Style = "Heading 2"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
Do While .Find.Found
.Sentences.First.Style = "Emphasis"
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
Application.ScreenUpdating = True
End Sub

fumei
11-16-2013, 12:03 AM
I meant if the TOC is updated. Oh wait, you have linked styles.

gmaxey
11-17-2013, 05:33 PM
What Paul (Macropod) has posted will probably get you close enough and work with your examples, but it won't be air tight. The problem is that Word only has a vague idea of what a sentence is. Consider this:

1. Cartoon characters
1.1. Bugs Bunny, Mr. Magoo and Daffy Duck. This will never work for you.

Word will see the first sentences as "Bugs Bunny, Mr."

See: http://gregmaxey.mvps.org/word_tip_pages/deduced_sentences.html

macropod
11-17-2013, 05:57 PM
Word only has a vague idea of what a sentence is.
From a VBA standpoint, how very true! Vague indeed. Although, that said, the grammar checker seems to have a fairly good idea of what constitutes a sentence. Pity the logic it uses isn't properly exposed to VBA.