Results 1 to 5 of 5
  1. #1
    New Lounger
    Join Date
    Sep 2016
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Converting heading styles

    Hi folks,

    I have a template which has custom heading styles in it, called "A-Heading 1," "A-Heading 2," etc. For whatever reason, my management has asked me to create a new template with exactly the same formatting but with the names reverted back to the usual Heading 1, Heading 2, etc. Of course I can't just rename the styles because they already exist and are the built-in names. What's the best way to accomplish this?

    tl;dr I need to make my Heading styles match the styles of another document that doesn't use those names.

    Thanks in advance!

    Beej

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    You will need a method of copying all the custom style attributes to the built-in styles and then do a search and replace to reassign the styles where used and finally delete the unwanted style.

    A macro to do all the heavy lifting is below
    Code:
    Sub RunSupercedeStyle()
      SupercedeStyle "A-Heading 1", "Heading 1"
      SupercedeStyle "A-Heading 2", "Heading 2"
      SupercedeStyle "A-Heading 3", "Heading 3"
    End Sub
    
    Function SupercedeStyle(strReplace As String, strTarget As String)
      Dim styReplace As Style, styTarget As Style
      Set styReplace = ActiveDocument.Styles(strReplace)
      Set styTarget = ActiveDocument.Styles(strTarget)
      With styTarget
        .ParagraphFormat = styReplace.ParagraphFormat
        .Font = styReplace.Font
        .Borders = styReplace.Borders
      End With
      With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = ""
        .Replacement.Text = ""
        .Style = styReplace
        .Replacement.Style = styTarget
        .Forward = True
        .Wrap = wdFindContinue
        .Execute Replace:=wdReplaceAll
      End With
      styReplace.Delete
    End Function
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. The Following User Says Thank You to Andrew Lockton For This Useful Post:

    Beej@impact (2016-09-28)

  4. #3
    5 Star Lounger Lugh's Avatar
    Join Date
    Jun 2010
    Location
    Indy
    Posts
    620
    Thanks
    166
    Thanked 77 Times in 68 Posts
    A curiosity:

    I notice you use:
    .Replacement.Text = ""

    I use:
    .Replacement.Text = "^&"

    Is there any difference in practice between the two?
    Lugh.
    ~
    Windows 10 Pro x64 1607; Office 2016 (365 Home) x32; Win Defender, MBAM Pro

    ASRock H97 Anniversary; Xeon E3-1231V3 (like i7)
    Gigabyte GeForce GTX 970; 12GB Crucial DDR3 1600
    Logitech MX Master mouse; Roccat Isku kb

  5. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    Lugh
    Because that code is searching for a style and not content, the value in the .Replacement.Text is irrelevant. If we were actually searching for a specific text string then it WOULD matter so I guess it would be considered good practice to do it your way. It would be an interesting debate for propeller heads though - "Does anything get changed if I find every instance of nothing and replace it with itself?"
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  6. The Following User Says Thank You to Andrew Lockton For This Useful Post:

    Lugh (2016-10-07)

  7. #5
    New Lounger
    Join Date
    Sep 2016
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Andrew, not only do you provide super-useful answers now you're making me laugh too. Thanks for both.

Posting Permissions

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