automating changes to "Style for following paragraph" in Word

J

john gibb

Hi,

I frequently change Outline levels in long documents and have setup
specific, and different, styles for what follows my Headings. (Different
indentation is one reason I do this)

For example, 's2' and 's3' are the names of styles that follow 'Heading 2'
and 'Heading 3' styles, respectively.

Now I change styles with keyboard macros, but have to go through the entire
document and invoke the macros after visually noting where I want the styles
to be changed.

Is there a VBA way to further automate this so, if I change 'Heading 2' to
'Heading 3', the "Style for following paragraph" can be changed from 's2' to
's3'?

My VBA skills are wanting but am wondering if this might be possible an
algorithm like:

<clip>

do while not end-of-file
for i=1 to 9
if (paragraph.style="Heading " + i)
skip-to-next-paragraph
if Left(paragraph.style,8) <> "Heading "
paragraph.style.set ="s"+ i
else
skip-to-next-paragraph
end if
next
end do while

<clip>

??

thanks,

-john
 
P

Peter Hewett

Hi john gibb

Try the following code:

Public Sub FollowingStyles()
Const STYLE_H1Following As String = "s2"
Const STYLE_H2Following As String = "s3"

With ActiveDocument.Styles
.Item(wdStyleHeading2).NextParagraphStyle = STYLE_H1Following
.Item(wdStyleHeading3).NextParagraphStyle = STYLE_H2Following
End With
End Sub

This simply updates the style used by the next paragraph.

HTH + Cheers - Peter
 
J

john gibb

Hi Peter,

This looks promising.

How do I get VBA to automatically go through my document from top to bottom
and invoke this(or any other) function on each paragraph?

thanks,

-john
 
P

Peter Hewett

Hi john gibb

That's the whole point - you don't need to. Whenever you update a style Word will
automatically reflect the changes in your document. The only (apparent) exception to this
is where direct formatting has been applied to a paragraph which masks the change to the
style.

HTH + Cheers - Peter
 
J

john gibb

OK...we're getting closer...

Using yours as a model, I am now working with this code:

<clip>
Public Sub FollowingStyles()
Const STYLE_H1Following As String = "n2"
Const STYLE_H2Following As String = "n3"
Const STYLE_H3Following As String = "n4"
Const STYLE_H4Following As String = "n5"
Const STYLE_H5Following As String = "n6"
Const STYLE_H6Following As String = "n7"

With ActiveDocument.Styles
.Item(wdStyleHeading2).NextParagraphStyle = STYLE_H1Following
.Item(wdStyleHeading3).NextParagraphStyle = STYLE_H2Following
.Item(wdStyleHeading4).NextParagraphStyle = STYLE_H3Following
.Item(wdStyleHeading5).NextParagraphStyle = STYLE_H4Following
.Item(wdStyleHeading6).NextParagraphStyle = STYLE_H5Following
.Item(wdStyleHeading7).NextParagraphStyle = STYLE_H6Following
End With
End Sub

<clip>

but I am not sure where to put this code? (I put it in a NewMacros Module of
the Normal.dot template, but don't see any effect yet).

I also tried putting it in a Module of a test.doc file and the Running it,
but get a 5834 (item with specified name does not exist) error on:

.Item(wdStyleHeading2).NextParagraphStyle = STYLE_H1Following

when I Run it from the Macros menu.

thanks again,

-john
 
P

Peter Hewett

Hi john gibb

What we're trying to achieve here is set the "follow-on" style for a paragraph. So that
when you press enter the next paragraph has a style that is different to the current
paragraph. Obviously, for this to work the follow-on styles (n2 - n7) must already exist
in the document as you cant assign a reference to something that does not exist.

Obviously, the style "n2" does *not* exist in your document. This is what you need to fix
up.

HTH + Cheers - Peter
 

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