Log in

View Full Version : Solved: Change style for paragraphs starting with "Chapter"



Paul_Hossler
12-29-2008, 08:10 PM
Looking for a piece of code that will look through all paragraphs in a document, and if the paragraph starts with a certain word, change the style to (for ex.) Heading 1

Would like to use it to help re-format/cleanup imported text files

Right now I do the multiple spaces to one space, multiple paragraphs to one, etc., and I'd like to include the code in my Reformat macro.

I can (thanks to the macro recorder) muddle my way though the formatting, etc. but it's the looping and testing.

Here's a before and after sample.

Thanks

Paul

Paul_Hossler
12-30-2008, 06:40 AM
Elegant, and easy to follow -- thanks :clap:

In case you're interested (and even if you're not :) ), here's the "final" result.

So far it seems to be working OK, and making life much easier



Option Explicit
Sub Demo()

Dim oSelection As Object

Application.ScreenUpdating = False

Set oSelection = Selection


Call StyleBasedOnFirstParaWord("chapter", "Heading 1")
Call StyleBasedOnFirstParaWord("topic", "Heading 2")
Call StyleBasedOnFirstParaWord("Part", "Heading 3")
Call StyleBasedOnFirstParaWord("section", "Heading 4")

oSelection.Select

Application.ScreenUpdating = True

End Sub

Sub StyleBasedOnFirstParaWord(sFirstWord As String, sStyle As String)
Dim oPara As Paragraph

With ActiveDocument
For Each oPara In .Paragraphs
With oPara.Range
If LCase(Trim(sFirstWord)) = LCase(Trim(.Words(1))) Then
.Select
Selection.Range.Case = wdLowerCase
Selection.Range.Case = wdTitleWord
.Style = sStyle
End If
End With
Next
End With

End Sub



Thanks again :beerchug:

Paul

fumei
12-30-2008, 11:35 AM
Why are you using oSelection in Demo? It does not do anything.

Paul_Hossler
12-30-2008, 01:02 PM
oSelection was just a piece of left over code that I did not cleanup when I got done trying some different things, and before I modularized the guts of the logic within StyleBasedOnFirstParaWord()

Demo() was just a driver, what I really wanted to do was include calls to StyleBasedOnFirstParaWord() as part of a longer macro

Paul

Paul_Hossler
12-30-2008, 08:46 PM
Thanks, those are good points

If the specific .Words(1) turn out to be relatively unchanigng, that's what I'll do to avoid all that looping.

However, I tried to apply wdTitleWord only, but it wasn't always reliable, so I forced it to wdLowerCase first, and then wdTitleWord. Look at the results without it.

Just using this bit of code, the "Chapter 1 - AAAAAAAAAAAAA AAAAAAAAA" first line doesn't become "Chapter 1 - Aaaaaaaaaaaaa Aaaaaaaaa" without making it LC first. Confusing to me, unless I am missing something.


Sub Demo()
Dim oPara As Paragraph

Application.ScreenUpdating = False

For Each oPara In ActiveDocument.Paragraphs
With oPara.Range
Select Case Trim(LCase(.Words(1)))
Case "chapter"
.Style = "Heading 1"
' .Case = wdLowerCase 'Uncomment this line, and Chapter 1 works as expected
.Case = wdTitleWord
End Select
End With
Next

Application.ScreenUpdating = True

End Sub


Paul