I often get documents that have had the heading styles
manually formatted so that when I drop portions of the
document into my template these headings don't update
properly. I would like to create a macro to loop through
the headings and reapply the heading style to eliminate
any manual formatting.

Here is my attempt. I get an error in the first line
because I don't know how to properly declare which element
in which collection to loop through, and I don't know how
to find out.


Sub UpdateHeaders()

For Each HeadingStyles In ActiveDocument
If Selection.Style = ActiveDocument.Styles("Heading 1")
Then
With Selection
.Paragraphs(1).Range.Select
.Collapse Direction:=wdCollapseStart
.Style = ActiveDocument.Styles("Heading 1")
End With
ElseIf Selection.Style = ActiveDocument.Styles("Heading
2") Then
Selection.Style = ActiveDocument.Styles("Heading 2")
ElseIf Selection.Style = ActiveDocument.Styles("Heading
3") Then
Selection.Style = ActiveDocument.Styles("Heading 3")
ElseIf Selection.Style = ActiveDocument.Styles("Heading
4") Then
Selection.Style = ActiveDocument.Styles("Heading 4")
End If
Next HeadingStyles

End Sub

Thanks for any help,

Blake

Re: for next updating headings by Jay

Jay
Thu Jan 13 14:19:58 CST 2005

Hi Blake,

It's really a lot easier than you're making it. The proper collection is
ActiveDocument.Paragraphs, and the individual items are Paragraph objects.
You can apply the Like operator to each paragraph's style to determine
whether it's a heading.

When you get to each heading paragraph, instead of reapplying the style, you
can use the Reset method. First apply it to the paragraph's Font to remove
any direct font formatting (also any character styles, unfortunately). Then
apply it to the Paragraph itself to remove any direct paragraph formatting.

Public Sub CleanHeadings()
Dim oPara As Paragraph

For Each oPara In ActiveDocument.Paragraphs
If oPara.Style Like "Heading #" Then
oPara.Range.Font.Reset ' font properties
oPara.Reset ' paragraph properties
End If
Next oPara
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org

Blake wrote:
> I often get documents that have had the heading styles
> manually formatted so that when I drop portions of the
> document into my template these headings don't update
> properly. I would like to create a macro to loop through
> the headings and reapply the heading style to eliminate
> any manual formatting.
>
> Here is my attempt. I get an error in the first line
> because I don't know how to properly declare which element
> in which collection to loop through, and I don't know how
> to find out.
>
>
> Sub UpdateHeaders()
>
> For Each HeadingStyles In ActiveDocument
> If Selection.Style = ActiveDocument.Styles("Heading 1")
> Then
> With Selection
> .Paragraphs(1).Range.Select
> .Collapse Direction:=wdCollapseStart
> .Style = ActiveDocument.Styles("Heading 1")
> End With
> ElseIf Selection.Style = ActiveDocument.Styles("Heading
> 2") Then
> Selection.Style = ActiveDocument.Styles("Heading 2")
> ElseIf Selection.Style = ActiveDocument.Styles("Heading
> 3") Then
> Selection.Style = ActiveDocument.Styles("Heading 3")
> ElseIf Selection.Style = ActiveDocument.Styles("Heading
> 4") Then
> Selection.Style = ActiveDocument.Styles("Heading 4")
> End If
> Next HeadingStyles
>
> End Sub
>
> Thanks for any help,
>
> Blake