Page 1 of 2 12 LastLast
Results 1 to 15 of 27
  1. #1
    3 Star Lounger Omega3's Avatar
    Join Date
    Jan 2004
    Location
    Los Angeles, California, USA
    Posts
    343
    Thanks
    2
    Thanked 1 Time in 1 Post
    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?
    You know it's time to diet when you push away from the table and the table moves.

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Quote Originally Posted by Omega3 View Post
    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.

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    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.

  4. #4
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts
    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).

  5. #5
    3 Star Lounger Omega3's Avatar
    Join Date
    Jan 2004
    Location
    Los Angeles, California, USA
    Posts
    343
    Thanks
    2
    Thanked 1 Time in 1 Post
    Quote Originally Posted by HansV View Post
    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”.
    You know it's time to diet when you push away from the table and the table moves.

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Quote Originally Posted by Omega3 View Post
    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.

  7. #7
    3 Star Lounger Omega3's Avatar
    Join Date
    Jan 2004
    Location
    Los Angeles, California, USA
    Posts
    343
    Thanks
    2
    Thanked 1 Time in 1 Post
    Quote Originally Posted by HansV View Post
    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.
    You know it's time to diet when you push away from the table and the table moves.

  8. #8
    3 Star Lounger Omega3's Avatar
    Join Date
    Jan 2004
    Location
    Los Angeles, California, USA
    Posts
    343
    Thanks
    2
    Thanked 1 Time in 1 Post
    Quote Originally Posted by HansV View Post
    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?
    You know it's time to diet when you push away from the table and the table moves.

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    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.

  10. #10
    3 Star Lounger Omega3's Avatar
    Join Date
    Jan 2004
    Location
    Los Angeles, California, USA
    Posts
    343
    Thanks
    2
    Thanked 1 Time in 1 Post
    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.
    You know it's time to diet when you push away from the table and the table moves.

  11. #11
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Could you post a small sample document that demonstrates the problem?

  12. #12
    3 Star Lounger Omega3's Avatar
    Join Date
    Jan 2004
    Location
    Los Angeles, California, USA
    Posts
    343
    Thanks
    2
    Thanked 1 Time in 1 Post
    Quote Originally Posted by HansV View Post
    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". [attachment=87160:Heading Format Problem.doc]
    Attached Files Attached Files
    You know it's time to diet when you push away from the table and the table moves.

  13. #13
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts
    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.

  14. #14
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    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".

  15. #15
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    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

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •