Paragraph formatting help

L

LEU

I have documents with the following formattingâ€

Heading 1 Paragraph
Normal Hard return
Normal Paragraph
Normal Hard return
Table
Heading 2 Paragraph
Normal Hard return
Normal Paragraph
Normal Hard return
and so on…..

What I want the formatting to look like this:

Heading 1 Paragraph
Hd1 Hard return
Hd1 Paragraph
Hd1 Hard return
Table
Heading 2 Paragraph
Hd2 Hard return
Hd2 Paragraph
Hd2 Hard return
and so on…..


Heading 1, Heading 2, Hd1 and Hd2 are styles. I have a total of 7 Headings
and Hds

I have the following macro that almost works. It looks for all the Heading
1s and reformats the paragraphs below it to Hd1 but it also changes Heading 1
to Hd1. How do I keep this form happing?

Dim rTmp As Range
Dim rTmp1 As Range
Set rTmp = ActiveDocument.Range
With rTmp.Find
.Style = "Heading 1"
While .Execute
Set rTmp1 = rTmp.Duplicate
rTmp1.Select ' for testing
Do
While Not (rTmp1.Paragraphs.Last.Next Is Nothing)
If rTmp1.Paragraphs.Last.Next.Style = "Normal" Then
rTmp1.End = rTmp1.Paragraphs.Last.Next.Range.End
rTmp1.Select ' for testing
Else
Exit Do
End If
If rTmp1.Paragraphs.Last.Next Is Nothing Then
Exit Do
End If
Wend
Loop
With Selection.ParagraphFormat
.Style = "Hd1"
End With
rTmp.Start = rTmp1.End
rTmp.End = ActiveDocument.Range.End
If rTmp1.Paragraphs.Last.Next Is Nothing Then
Exit Sub
End If
Wend
End With
Selection.HomeKe
 
G

Graham Mayor

Dim sStyle As Integer
sStyle = 0
With ActiveDocument
For i = 1 To .Paragraphs.Count
With .Paragraphs(i)
If Left(.Style, 7) = "Heading" Then
sStyle = Right(.Style, 1)
End If
If .Style = "Normal" And sStyle <> 0 Then
.Style = "HD" & sStyle
End If
End With
Next i
End With

should do the trick
provided all styles HD1 to HD7 are available to the document.
Only paragraphs formatted with normal style are changed.
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
F

Fuzzhead

Graham,

Thanks for the help. I'm out of the office till Thursday, so I can't try
this until then. I'll let you know how it went.

Larry
 
L

LEU

Graham,

Your macro worked great. It takes some time to run through a document and
makes it look like nothing is happening when it runs. Some end users may
think Word has lockup and will try to cancel or close the document. Is there
a way to show that the macro is working on the document while it runs?

LEU
 
G

Graham Mayor

Not without slowing the macro further. A simple solution might be to add a
message to users at the start of the macro eg

MsgBox "This macro may take some time to complete" & vbCr & _
"with no obvious sign of activity." & vbCr & vbCr & _
"Please be patient :)", vbInformation, "Table formatting"


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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