Reset font on built-in heading 1 formatted paragraphs

A

andreas

Dear Experts:

below macro executes a font reset on paragraphs formatted with built-
in heading 1 style. It works fine (i.e. it clears manually formatting)
with the exception of the following cases:

If the paragraph (formatted with the built-in heading 1 style) has
subsequently been applied a user-defined character style, the macro
does not reset these applied character styles. How do I have to change
the code so that it also clears these character styles?

Help is much appreciated. Thank you very much in advance.

Regards, Andreas



Sub RmvManFoHead1()
Dim para As Word.Paragraph
Dim rng As Word.range


For Each para In ActiveDocument.Paragraphs
If para.range.Style = ActiveDocument.Styles(wdStyleHeading1)
Then
Set rng = para.range
rng.StartOf Unit:=wdParagraph
With rng
.MoveEndUntil Cset:=vbCrLf, count:=wdForward
.MoveEnd Unit:=wdCharacter, count:=1
End With
rng.Select
rng.Font.Reset
End If
Next para

End Sub
 
L

Lene Fredborg

If you use:
para.Range.Style
to find the style of a paragraph and if a character style has been applied
to the entire paragraph, VBA will return the name of the character style,
i.e. the character style “covers†the paragraph style behind.

I made some experiments to find a safe way to detect whether Heading 1 is
used and I found that if you use Find instead of iterating through all
paragraphs, you will be able to find Heading 1 no matter whether a character
style has been applied to the entire paragraph or not. In addition, the Find
method is faster.

I think the macro below will do what you want. Note that I have included
“oRange.Paragraphs.Resetâ€
In order to remove any direct paragraph formatting that may have been
applied. Remove that line if you wish.

Sub RmvManFoHead1()

Dim oRange As Range

Set oRange = ActiveDocument.Range

'Use Find to find all Heading 1
With oRange.Find
.ClearFormatting
.Style = ActiveDocument.Styles(wdStyleHeading1)
.Text = ""
.Forward = True
.Wrap = wdFindStop
Do While .Execute
With oRange
'Reset font settings (font, size, font color, etc.)
.Font.Reset
'Reset paragraph settings (alignment, indent, space before,
etc.)
.Paragraphs.Reset
End With
Loop
End With

'Clean up
Set oRange = Nothing
End Sub

Sub RmvManFoHead1_OLD()
Dim para As Word.Paragraph

-----------------------------
Note: In your current macro, you have some code that is not needed. You
could reduce the code to the following and it would do the same:

Sub RmvManFoHead1()
Dim para As Word.Paragraph

For Each para In ActiveDocument.Paragraphs
If para.Range.Style = ActiveDocument.Styles(wdStyleHeading1) Then
para.Range.Font.Reset
End If
Next para

End Sub

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
L

Lene Fredborg

By mistake, the following two lines were included in my previous post:

Sub RmvManFoHead1_OLD()
Dim para As Word.Paragraph

Please just ignore them.

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
A

andreas

If you use:
para.Range.Style
to find the style of a paragraph and if a character style has been applied
to the entire paragraph, VBA will return the name of the character style,
i.e. the character style “covers” the paragraph style behind.

I made some experiments to find a safe way to detect whether Heading 1 is
used and I found that if you use Find instead of iterating through all
paragraphs, you will be able to find Heading 1 no matter whether a character
style has been applied to the entire paragraph or not. In addition, the Find
method is faster.

I think the macro below will do what you want. Note that I have included
“oRange.Paragraphs.Reset”
In order to remove any direct paragraph formatting that may have been
applied. Remove that line if you wish.

Sub RmvManFoHead1()

    Dim oRange As Range

    Set oRange = ActiveDocument.Range

    'Use Find to find all Heading 1
    With oRange.Find
        .ClearFormatting
        .Style = ActiveDocument.Styles(wdStyleHeading1)
        .Text = ""
        .Forward = True
        .Wrap = wdFindStop
        Do While .Execute
            With oRange
                'Reset font settings (font, size, font color, etc.)
                .Font.Reset
                'Reset paragraph settings (alignment, indent, space before,
etc.)
                .Paragraphs.Reset
            End With
        Loop
    End With

    'Clean up
    Set oRange = Nothing
End Sub

Sub RmvManFoHead1_OLD()
    Dim para As Word.Paragraph

-----------------------------
Note: In your current macro, you have some code that is not needed. You
could reduce the code to the following and it would do the same:

Sub RmvManFoHead1()
    Dim para As Word.Paragraph

    For Each para In ActiveDocument.Paragraphs
        If para.Range.Style = ActiveDocument.Styles(wdStyleHeading1) Then
            para.Range.Font.Reset
        End If
    Next para

End Sub

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmarkwww.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word












- Show quoted text -

Dear Lene,

Thank you very much for your detailed explanation of the problem I
encountered. Yes, it did the trick, the macro does exactly what I
wanted. Great job, terrific help from you.

Regards, Andreas
 
L

Lene Fredborg

Thank you for the feedback. I am glad I could help.

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 

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