0
votes

I have some word documents that have custom heading styles.

I would like to iterate through all custom headings in a document, and replace the custom style with the standard heading style.

For example:

Custom Style                Standard Style
===================         ==============
Heading 1. Numbered   -->   Heading 1
Heading 2. Numbered   -->   Heading 2
Heading 3. Numbered   -->   Heading 3

and so on up to Heading 5 ...

I am using MS Word 2007.

Question: how can I do this with VBA?

1

1 Answers

0
votes

This worked for me:

Sub Macro1()

    Dim DocPara As Paragraph

    For Each DocPara In Application.ActiveDocument.Paragraphs
        If DocPara.Range.Style Is Nothing Then
            ' do nothing
        Else
            Dim I As Integer
            Dim H As String
            For I = 1 To 5
                H = "Heading " + CStr(I) + ". Numbered"
                If Left(DocPara.Range.Style, Len(H)) = H Then
                    DocPara.Range.Style = "Heading " + CStr(I)
                End If
            Next I
        End If
    Next

End Sub

Code adapted from: https://stackoverflow.com/a/276397/1033422