• Change Format of All Heading 1 Styles

    Author
    Topic
    #464781

    I have the following code (shortened) that will change the font for all Heading-1 styles in the document.

    ActiveDocument.Styles(“Heading 1”).AutomaticallyUpdate = True
    ActiveDocument.Styles(“Heading 1”).Font.Name = “Arial”

    All Heading-1 styles now have their fonts changed. But if the style name is “Heading 1 + 12 pt” etc. it will not be changed. Is there a way to change all Heading-1 styles regardless of what format modifications comes after the name?

    Viewing 5 reply threads
    Author
    Replies
    • #1191321

      I have the following code (shortened) that will change the font for all Heading-1 styles in the document.

      ActiveDocument.Styles(“Heading 1”).AutomaticallyUpdate = True
      ActiveDocument.Styles(“Heading 1”).Font.Name = “Arial”

      I don’t think you need to turn on AutomaticallyUpdate in order to change the font using VBA. I believe that feature is used to allow you to change the font by applying direct formatting in the document.

      In order to find all the style names that start with “Heading 1” you may have to loop through the styles and examine their names. Not sure if there is a faster way.

    • #1191322

      The line

      ActiveDocument.Styles(“Heading 1”).AutomaticallyUpdate = True

      shouldn’t be necessary if you want to change a style globally. The effect will be that if you change the formatting of one bit of Heading 1 text, that formatting will be applied to all Heading 1 text. In most situations, this is not desirable.

      When I try it, the line

      ActiveDocument.Styles(“Heading 1”).Font.Name = “Arial”

      affects pseudo-styles such as Heading 1 + 12 pt too. Perhaps you want to reset the font and/or paragraph formatting to the Heading 1 style definition? If so, you could use this code:

      Code:
      Sub ResetHeading1Paragraphs()
        Selection.HomeKey Unit:=wdStory
        With Selection.Find
          .Text = ""
          .ClearFormatting
          .Replacement.Text = ""
          .Replacement.ClearFormatting
          .Style = "Heading 1"
          Do While .Execute
            Selection.Font.Reset
            Selection.ParagraphFormat.Reset
          Loop
        End With
      End Sub
      

      BTW, I always turn off “Keep track of formatting” in the Edit tab of Tools | Options…, that gets rid of all those pseudo styles. The only disadvantage is that you can’t select all text with a certain style, but I can live with that.

      • #1191325

        When I try it, the line

        ActiveDocument.Styles(“Heading 1”).Font.Name = “Arial”

        affects pseudo-styles such as Heading 1 + 12 pt too.

        Thanks for the reply Hans and Jefferson. I was curious why Han’s got his to work on pseudo styles. I ran another similar test. This time I had two styles: Heading 1 – Batang and another Heading 1 – Harrington, 16 pt. Then I ran this code: ActiveDocument.Styles(“Heading 1”).Font.Size = 48. Only the Batang style got changed. Is that because the Harrington style already had a point size and the Batang did not?

        I tried going back to my Arial example and retested that and neither one had their fonts changed. Maybe, once again, the pseudo name had a font name already so it did not change it. If this is the case then I will need to try your reset code.

        Thanks for the tip on turning off “Keep track of formatting”.

        • #1191329

          Only the Batang style got changed. Is that because the Harrington style already had a point size and the Batang did not?

          Yes, indeed. If you change the point size of a style, text whose point size has been changed from that of the style will not be affected.
          Similarly, if you change the font name of a style, text to which a different font than that of the style has been applied, will not be affected.

          • #1191333

            Yes, indeed. If you change the point size of a style, text whose point size has been changed from that of the style will not be affected.
            Similarly, if you change the font name of a style, text to which a different font than that of the style has been applied, will not be affected.

            Thanks for the confirmation. I will now resort to using the additional code to reset the heading and thank you for that additional code.

      • #1191420

        Perhaps you want to reset the font and/or paragraph formatting to the Heading 1 style definition? If so, you could use this code:

        Code:
        Sub ResetHeading1Paragraphs()
          Selection.HomeKey Unit:=wdStory
          With Selection.Find
            .Text = ""
            .ClearFormatting
            .Replacement.Text = ""
            .Replacement.ClearFormatting
            .Style = "Heading 1"
            Do While .Execute
              Selection.Font.Reset
              Selection.ParagraphFormat.Reset
            Loop
          End With
        End Sub
        

        Hans, I tried running your reset macro and it worked on one heading style but not on the other. The other one had a pseudo style name of “Style Heading 1 + Algeria” Is there a way to reset all the Heading1 styles?

        • #1191421

          I don’t know why it doesn’t work for you – when I tested it, the code applied to all Heading 1 + … styles.

          But if you turn off “Keep track of formatting”, the problem should be gone because there will be no Heading 1 + … styles.

      • #1192131
        Code:
        Sub ResetHeading1Paragraphs()
          Selection.HomeKey Unit:=wdStory
          With Selection.Find
            .Text = ""
            .ClearFormatting
            .Replacement.Text = ""
            .Replacement.ClearFormatting
            .Style = "Heading 1"
            Do While .Execute
              Selection.Font.Reset
              Selection.ParagraphFormat.Reset
            Loop
          End With
        End Sub
        

        When I run the reset macro, this time for normal style, and it encounters a table in the document, it seems to halt on the: Selection.Font.Reset line. Is there a way to modify the find method to skip any tables in the document?

        • #1192140

          Try this version:

          Code:
          Sub ResetHeading1Paragraphs()
            Selection.HomeKey Unit:=wdStory
            With Selection.Find
              .Text = ""
              .ClearFormatting
              .Replacement.Text = ""
              .Replacement.ClearFormatting
              .Style = "Kop 1"
              Do While .Execute
                  Selection.Font.Reset
                  Selection.ParagraphFormat.Reset
                  ' Collapse the selection
                  Selection.Collapse Direction:=wdCollapseEnd
              Loop
            End With
          End Sub
          • #1192154

            Try this version:

            Code:
            Sub ResetHeading1Paragraphs()
              Selection.HomeKey Unit:=wdStory
              With Selection.Find
                .Text = ""
                .ClearFormatting
                .Replacement.Text = ""
                .Replacement.ClearFormatting
                .Style = "Kop 1"
                Do While .Execute
                    Selection.Font.Reset
                    Selection.ParagraphFormat.Reset
                    ' Collapse the selection
                    Selection.Collapse Direction:=wdCollapseEnd
                Loop
              End With
            End Sub

            I got a debug error on .Style = “Kop 1” so I changed that back to Normal and ran it again. It still halted on the same line.

            What I did was create a new document with only normal text throughout and the macro works. But when I insert a table the code never ends. When I control break it, it always halts on that line of code.

            • #1192160

              Sorry about the “Kop 1”, it’s the Dutch name of the Heading 1 style, I forgot to change it back to English.

              Does this work? If not, I’m out of ideas – it works for me.

              Code:
              Sub ResetHeading1Paragraphs()
                Selection.HomeKey Unit:=wdStory
                With Selection.Find
                  .Text = ""
                  .ClearFormatting
                  .Replacement.Text = ""
                  .Replacement.ClearFormatting
                  .Style = wdStyleHeading1
                  Do While .Execute
                    If Selection.Information(wdWithInTable) = False Then
                      Selection.Font.Reset
                      Selection.ParagraphFormat.Reset
                    End If
                    Selection.Collapse wdCollapseEnd
                  Loop
                End With
              End Sub
          • #1192156

            You can tell from Hans’ post that he is using a non-English version of word. I suggest you replace
            .Style = “Kop 1”
            with
            .Style=wdStyleHeading1

            Which will work for all languages

    • #1191324

      Also note that this code will only run correctly in English installations of Word, where the top level Heading is called “Heading 1”, if you want to make changes to this style regardless of regional settings then you should refer to ActiveDocument.Styles(wdStyleHeading1).

    • #1191447

      I ran some more tests. I had “Keep track of formatting” ON and changed some Heading 1 styles to create some pseudo style names. Then “Keep track of formatting” was turned OFF and the pseudo names were all changed to “Heading 1” except the “Style Heading 1 + Algeria”. Instead of “Heading 1” it has “Style Heading 1 + Algeria”. I don’t know why this one pseudo style behaves differently. The style itself is based on “Heading 1”. I tried changing the base to “No Style” like the others and that did not help.

      I tried modifying just the Style name itself from “Style Heading 1 + Algeria” to “Heading 1 + Algeria” and the Reset code still did not work even with “Keep track of formatting” turned OFF. I can change the Style to another style and then I can run a macro to change the font size and it works but not on “Heading 1 + Algeria”. I can change it back and run my change font size macro and it will not be changed. Something does not add up right.

      • #1191450

        Could you post a small sample document that demonstrates the problem?

        • #1191458

          Could you post a small sample document that demonstrates the problem?

          Here is the document I was using to test with. You will see that it still has “Heading 1 + Algerian”.

          • #1191459

            The style in your document that is called “Heading 1 + Algerian” is NOT based on Heading 1, it is based on (nostyle). So it is not affected by changes to Heading 1.

            I wonder how this has been created / modified.

          • #1191460

            The styles you described previously are pseudo-styles that Word displays if “Keep track of formatting” is turned on and when you apply direct formatting to some text.
            The “Heading 1 + Algerian” style is a “real” style; there are two ways you can see this:
            – The style doesn’t disappear when you turn off “Keep track of formatting”.
            – The style name is not descriptive of the formatting – in fact, the font for “Heading 1 + Algerian” is not Algerian but Batang.

            The code that I posted looks for Heading 1 and pseudo-styles caused by direct formatting of Heading 1. It will ignore styles that have explicitly been created, even if their names begin with “Heading 1”.

          • #1191462

            You could run this little macro before the one I posted earlier:

            Code:
            Sub ModifyHeading1()
              Dim par As Paragraph
              For Each par In ActiveDocument.Paragraphs
                If par.Style Like "Heading 1*" Then
                  par.Style = ActiveDocument.Styles(wdStyleHeading1)
                End If
              Next par
            End Sub
            • #1191473

              You could run this little macro before the one I posted earlier:

              Code:
              Sub ModifyHeading1()
                Dim par As Paragraph
                For Each par In ActiveDocument.Paragraphs
                  If par.Style Like "Heading 1*" Then
                    par.Style = ActiveDocument.Styles(wdStyleHeading1)
                  End If
                Next par
              End Sub

              Hans, your new macro worked great. I can work with this. It still leaves me some unanswered questions like why does “Heading 1 + Algerian” still show up in the Styles Panel when the Show box is set to “Styles in use”. Even after I close and reopen the file it still shows up even though no where is it used in the document. But I can live with this. Thanks for your help.

              Stuart, it was AT ONE TIME based on “Heading 1” in earlier testing. In my last testing it was changed to “No Style” and was left that way. I was not trying to mislead anyone. If I understand it correctly, if the style is a built-in word style the “Style base on box” will show (no style).

            • #1191505

              I suspect that user-defined styles will be listed in “Styles in use” even if they haven’t actually been used in the document. The “Heading 1 + Algerian” style must have been created by you or another user at some point.

    • #1192178

      Are you testing it with “heading 1” style? I am now using the “normal” style.
      The macro does reset the Normal style even within the table itself. Each time I break the execution it highlights the code after the: Do While .Execute. which this time is: If Selection.Information(wdWithInTable) = False. Something is preventing the Find.Execute from ending.

      • #1192184

        It doesn’t work with the Normal style for me either. Looping through a document with tables in VBA is very wacky.

        Do you really need to do this style by style? You could reset paragraph formatting in the entire document by pressing Ctrl+A, Ctrl+Q, or character formatting by pressing Ctrl+A, Ctrl+spacebar.

        The VBA equivalents are

        ActiveDocument.Content.ParagraphFormat.Reset

        and

        ActiveDocument.Content.Font.Reset

        • #1192207

          It doesn’t work with the Normal style for me either. Looping through a document with tables in VBA is very wacky.

          Do you really need to do this style by style? You could reset paragraph formatting in the entire document by pressing Ctrl+A, Ctrl+Q, or character formatting by pressing Ctrl+A, Ctrl+spacebar.

          The VBA equivalents are

          ActiveDocument.Content.ParagraphFormat.Reset

          and

          ActiveDocument.Content.Font.Reset

          Thanks Hans. That worked. I will do some more testing tomorrow. Thank you for the work around.

    • #1193714

      When I run your work-around macro I’ve notice that it removes bullets and numbering. So I went back to your ResetHeading1Paragraph macro and tested it some more using the Normal style. I added this line: Debug.Print at the following location…

      Selection.HomeKey Unit:=wdStory
      With Selection.Find
      .Text = “”
      .ClearFormatting
      .Replacement.Text = “”
      .Replacement.ClearFormatting
      .Style = “Normal” ‘intStyle
      Do While .Execute
      If Selection.Information(wdWithInTable) = False Then
      Selection.Font.Reset
      Selection.ParagraphFormat.Reset
      Debug.Print Selection.Range.Information(wdActiveEndAdjustedPageNumber) ‘ADDED
      End If
      Selection.Collapse wdCollapseEnd
      Loop
      End With

      If that’s the correct code, I’ve found is that the find method is resetting all the formatting on every page until it gets to the last page when it keeps looping through the last page without ever stopping. And yet it works okay for Heading 1 and 2.

      I also discovered that when I change the last paragraph in the last page to something other than Normal style, the macro ends correctly.

      • #1193716

        If the macro removes bullets or numbering, that is because the bullets or numbering are not part of the style definition. This is one of the reasons for never applying bullets or numbering as direct formatting, but always by applying a style.

    Viewing 5 reply threads
    Reply To: Change Format of All Heading 1 Styles

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: