Consulting

Results 1 to 7 of 7

Thread: Applying one style to all Headings and Header styles

  1. #1

    Applying one style to all Headings and Header styles

    Hello,

    I need to apply a single style (called "Header titling") to all headers and header styles already in the document.

    At the moment, the authors of the documents I receive create a large number of headings with no consistent style. For example, the document I'm working on now is two pages, but has "Header"; "Header or footer (3)" (twice-once with para changes, other with only font changes); "Heading #1 (2)" (twice-once with para changes, other with only font changes); "Heading 1".

    I would like to apply a single style to all these many variants of headings and headers. I guess there are two stages to this process:
    (1) Finding all the headings and headers
    (2) Applying the style to them

    I do this to replace each individual style:

    With Selection.Find
    .ClearFormatting
    .Style = ActiveDocument.Styles("Header or footer")
    .Replacement.ClearFormatting
    .Replacement.Style = ActiveDocument.Styles("Normal titling")
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

    The trouble is that I don't know how to find what headers and headings are used in the document and couldn't find anything useful for this. Any help would be much appreciated!

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    3,585
    Location
    Is there a good reason for not using Word's heading Styles? And why, if you need to apply a Style named "Header titling", is your replacement expression referencing a Style named "Normal titling"? Similarly, you say there is a Style in use named "Header or footer (3)", but your Find expression is only looking for a Style named "Header or footer".

    Finally, if you don't know what Styles have been used for headings, you can't use Find/Replace to Find them by the Styles' names.
    Cheers
    Paul Edstein
    [MS MVP - Word]

  3. #3
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    5,861
    Location
    This is ugly, and might not catch every case (headers, footers, foot notes, etc.)

    I usually think of 'Headers' and 'Footers' as the MS Word features at the top/bottom of pages, so "Header titling" just feels wrong


    Option Explicit
    
    
    Sub ReplaceHeads()
        Dim oPara As Paragraph
        For Each oPara In ActiveDocument.Paragraphs
            If InStr("HEAD", UCase(oPara.Style.NameLocal)) > 0 Or InStr("FOOT", UCase(oPara.Style.NameLocal)) > 0 Then
                oPara.Style = "Header titling"
            End If
        Next
    End Sub
    Paul

    ------------------------------------------------------------------------------------------------------------------------
    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s)
    (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4

    Hi

    Quote Originally Posted by macropod View Post
    Is there a good reason for not using Word's heading Styles? And why, if you need to apply a Style named "Header titling", is your replacement expression referencing a Style named "Normal titling"? Similarly, you say there is a Style in use named "Header or footer (3)", but your Find expression is only looking for a Style named "Header or footer".

    Finally, if you don't know what Styles have been used for headings, you can't use Find/Replace to Find them by the Styles' names.
    - I chose the labels "Header Titling" and "Normal Titling" quickly but really I could have named them "Coffee" and "Tea" and it would have been the same result.
    - I used "Header or footer" as an example of how I am replacing one of the styles, but not all. Thus I could use the same code but with "Header or footer (3)"
    - Nope, I don't know the names of the various titles that users will be using in their documents.
    - The reason for not using Word's heading Stlyes - I don't know what you mean by this question. Could you please clarify?

  5. #5
    Hi Paul,

    I'm not sure why, but this code doesn't change any of the headers and headings to "Header titling"...
    Does the document have a style by that name?
    Last edited by Paul_Hossler; 09-11-2018 at 10:22 AM.

  6. #6
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    5,861
    Location
    @Davecogz84 -- My (really) bad

    I accidently edited your post, instead of mine

    Sorry



    Hi Paul, I'm not sure why, but this code doesn't change any of the headers and headings to "Header titling"...

    Does the document have a style by that name?
    Paul

    ------------------------------------------------------------------------------------------------------------------------
    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s)
    (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  7. #7
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    3,585
    Location
    Quote Originally Posted by Davecogz84 View Post
    The reason for not using Word's heading Stlyes - I don't know what you mean by this question. Could you please clarify?
    Word has 9 built-in Heading Styles. That you don't know what they are suggests a very limited understanding of Word and what you're proposing would undermine the work of those who have used them properly. The proper use of Styles is foundational to Word; indeed, you can't use Word at all without using or abusing them. See:
    http://www.addbalance.com/usersguide/styles.htm
    https://shaunakelly.com/word/styles/stylesms.html
    https://support.office.com/en-us/art...7-1eb120dec563
    Cheers
    Paul Edstein
    [MS MVP - Word]

Posting Permissions

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