0
votes

I need to change all of the headings in the books I am working on to "Heading 1".

So far, I've got this from recording changing two find and replaces:

'
' fixHeadings Macro
'
'
    Selection.Find.ClearFormatting
    With Selection.Find.ParagraphFormat
        .SpaceBeforeAuto = False
        .SpaceAfterAuto = False
    End With
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Style = ActiveDocument.Styles("Heading 1")
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindAsk
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    Selection.Find.ClearFormatting
    With Selection.Find.ParagraphFormat
        .SpaceBeforeAuto = False
        .SpaceAfterAuto = False
    End With
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Style = ActiveDocument.Styles("Heading 1")
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindAsk
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub

I tried this, but I'm kinda lost when it comes to vba

Dim objDoc As Document
'
' test Macro
' test
'
If objStyle <> "Normal" Or "" Then Set objStyle = "Heading 1"

End Sub

This is sorta what I'm trying to do: "If style doesn't equal Normal or has no style then set the style to Heading 1"

Any ideas?

2
Is there anything else about the headings that can be used to differentiate? Like font size, bold, etc.?jclasley
Thank you for the response. They are bold and the font size is 14 or higher (there are multiple different heading styles I am trying to change)majortom
Are only the headings bold? Or are there other bold parts of the document?jclasley
Other parts are bold as wellmajortom

2 Answers

0
votes

Try:

Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = ""
    .Replacement.Text = ""
    .Format = True
    .Font.Bold = True
    .Forward = True
    .Wrap = wdFindStop
    .Execute
  End With
  Do While .Find.Found
    If .Font.Size >= 14 Then .Paragraphs(1).Range.Style = wdStyleHeading1
    If .End = ActiveDocument.Range.End Then Exit Do
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub
0
votes

Pending your response to my comment, here's something that will work. Depending on the length of your document, there may be faster ways to do this.

You were on the right track with your if statement, but both sides of the Or have to be complete statements. So instead of just "" you needed Or objStyle = "". I actually used vbNullString because it's faster, but they are essentially equivalent.

For Each para In ActiveDocument.Paragraphs
    If para.Style <> "Normal" Or para.Style = vbNullString Then
        If para.Range.ListFormat.ListType = 0 Then     
            para.Style = "Heading 1"
        End If
    End If
Next para