PDA

View Full Version : [SLEEPER:] Applying one style to all Headings and Header styles



Davecogz84
09-07-2018, 06:58 PM
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!

macropod
09-08-2018, 05:36 AM
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.

Paul_Hossler
09-09-2018, 09:02 AM
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

Davecogz84
09-11-2018, 09:43 AM
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?

Davecogz84
09-11-2018, 09:45 AM
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_Hossler
09-11-2018, 10:25 AM
@Davecogz84 -- My (really) bad

I accidently edited your post, instead of mine :crying:

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?

macropod
09-11-2018, 02:49 PM
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/article/Customize-or-create-new-styles-in-Word-d38d6e47-f6fc-48eb-a607-1eb120dec563