Hi Geoff
The following will find all paragraphs containing red text that are preceded
*and* followed by an empty paragraph, apply style Heading 1 and delete the
offending empty paragraphs.
Note that this will:
- not find the very first paragraph in the document (because it has no
previous paragraph)
- not find the very last paragraph in the document (no next paragraph)
- not find two consecutive paragraphs containing red text (no empy
paragraphs)
- not find workds outside the main body of the document; it does not work in
headers, footers, comments, footnotes, endnotes etc.
If this is for real production code, you'll need to add suitable error
checking.
If the lines break up in transmission, you might have to glue them back
together.
Public Sub MakeHeadingsFromRedText()
Dim oRng As Word.Range
Dim paraPrevious As Word.Paragraph
Dim paraNext As Word.Paragraph
Set oRng = ActiveDocument.Range
With oRng.Find
.Font.Color = wdColorRed
Do While .Execute
'Get the next and previous paragraphs
On Error Resume Next
Set paraPrevious = oRng.Paragraphs.First.Previous
Set paraNext = oRng.Paragraphs.Last.Next
On Error GoTo 0
'If *both* the next and previous paragraphs exists
'and are empty then apply style Heading 1 to the
'paragraph containing the red text
If Not paraPrevious Is Nothing And Not paraNext Is Nothing Then
If Len(paraPrevious.Range) = 1 And Len(paraNext.Range) = 1 Then
'The paragraphs before and after the para
'containing the red text are empty.
'Make this a heading.
oRng.Paragraphs(1).Style = wdStyleHeading1
'And delete the extraneous paragraphs
paraPrevious.Range.Delete
paraNext.Range.Delete
End If
End If
'Clean up for next time
Set paraPrevious = Nothing
Set paraNext = Nothing
Loop
End With
End Sub
Hope this helps.
Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word