h1

Word: Use StyleRef field to populate header/footer

January 30, 2009

Many corporate documents require you to have a table near the beginning of the document with details such as the author, the date created, a revision number etc. You may also be required to insert some of these details into the header and/or footer of the Word document.

While you can set up all sorts of macros, cross-references, document property fields etc. to do this, here’s a really quick solution using styles and the StyleRef field. This solution works in Word 2003, Word 2007 and Word 2010 at least. Before attempting this, you should know how to create a new style in your version of Word. Note: The StyleRef field will NOT ‘see’ any styles used in text boxes (see the comment from November 2017 below).

  1. Create new styles for the text elements you want to capture. Give these styles names that are unique and are unlikely to be used anywhere else in the document (this is critical).  For example, DocAuthor, DocDate, DocTitle, DocRevNum. Don’t worry about formatting — the header/footer styles will control that.

    Create new styles

    Create new styles

  2. Fill in the details in the document information table (create a table if it doesn’t already exist).
  3. Apply the relevant styles to each cell of the document information table — e.g. apply the DocTitle style to the cell containing the title.  DO NOT apply these styles anywhere else in the document — only one paragraph/cell in the entire document should have this style applied to it otherwise this solution won’t work.

    Apply the new styles to the relevant cells

    Apply the new styles to the relevant cells

  4. Open the header or footer (Word 2003: View > Header and Footer; Word 2007/2010: Insert > Header > Blank then remove the control OR double-click in the header space to open the header).
  5. Insert a StyleRef field for each of the elements you want to populate.  To insert a field: Word 2003: Insert > Field; Word 2007/2010: Insert > Quick Parts > Field. Select StyleRef from the list of field names on the left (1), then select the style (e.g. DocAuthor) from the list of style names on the right (2), and click OK (3).

    StyleRef field

    StyleRef field

  6. Repeat step 5 for the other StyleRef fields you want to add. Add words, spaces, punctuation etc. between the fields, as required.

    Header created from the styles

    Header created from the styles

  7. Voila! Instant header/footer populated with the document details. And even better — as soon as you update any of the data in the cells, the header/footer automatically updates too.

    Instant update

    Instant update

If you want to see what’s going on behind the scenes, you can always toggle the field codes in the header/footer to see how it works:

Raw field codes

Raw field codes

(Thanks to Ken E on the STC Lone Writers discussion list who shared this technique with us.)

48 comments

  1. Thanks! I haven’t had to use many of the “advanced” features yet, so hadn’t looked into this, but it will be great to know when I need it.


  2. This is the best solution among several I’ve tried to automatically populate headers/footers. It is the most elegant and has the added bonus of not having to use code and having to deal with associated security issues. I found out early on that you can’t use bookmarks there like you can in the body of the document, and what would be really simple in Excel (=cell) is much more complicated in Word (as expected).

    Thanks!


  3. thank you very much. you solved my problem simply


  4. Note:
    Additional info: make sure you don’t use Adobe version 8 or 9 to convert your documents to pdf files. These versions add stray characters to your final pdf files in the headers before the styleref field. This problem has been pointed out to Adobe but so far they seem unable to do something about it… so if you are using an older pdf maker version HANG ON TO IT!


  5. This works very well. But my problem is that I am using a Date Picker Content control in Word 2007 on the Title page to standardize the special date formatting, and I need to have that same date in my header. I used the StyleRef trick and it’s fabulous, but the date is now surrounded by . How can these be removed? I have tried using a bookmark and cross-reference, which works well in the body of the text, but not in the header since it does not allow me to have have one font style on the title page, and a different font style in the header.


  6. Very nice Rhonda. Thanks for taking the time to put together this nice explanation.


  7. […] if I reference this TOC Heading style in a field using the StyleRef field code, I get a left arrow character inserted as part of the field in front of the Table of Contents […]


  8. Please help!

    I am editing a manual with about 600 pages and 23 Sections, labeled A through V. I often do changes to several of the pages, which changes the revision number on the those pages. Section A contains a section called “List of Effective Pages.” The List of Effective Pages contains a three column multi row table containing three pieces of data from each page in the document: the page number, the revision number, and the effective date. Is there a way, after I change some of the pages, that each of these pieces of data, which are contained in the header and/or footer, be automatically updated like in a Table of Contents and imported into the table in Section A?


  9. Hi Dan

    I think you’re asking a little too much from Word in this instance! If it was a database, then there’s very likely a way you can do this.

    It’s possible that a macro might do it, but I’d have no idea where to start. I suggest you ask your question over on the Word forums on the Microsoft Answers site: http://answers.microsoft.com/en-us/office/forum/word

    –Rhonda


  10. hi! thanks very much, your article helped me a lot, actually I really had problems with how do deal with these footer/header, finally I found the answer from here. great job!


  11. Hi,
    It is really neat and well illustrated solution on this topic.
    Thank you very much. Keep up the good work.

    Anbu.


  12. I have the issue with Track Changes used in the referenced style not showing in the Header with the StyleRef field. The text appears but, in this case, is not strikethrough. So my setup is the “Date” style at the top of the letter. With Track Changes on the date is changed on page one and shows the new date and the strikethrough on the old date. In the Header on page two it shows both dates but no strikethrough. Any ideas on how to get this formatting to show in the field?


  13. Hi Alan

    I just tried it and I get the same thing as you, except I get both dates in ALL places (none except the original source date field have track changes marked for them). I checked the field code, and there’s just one for the ‘combined’ date so it’s not like it’s showing two fields.

    I don’t have a solution for you, except to suggest you accept the change for this field if you are able to do so. However, corporate requirements may mean you have to leave it on until the changes have been ratified, in which case I’d let the next set of reviewers know about this anomaly and tell them to ignore it.

    –Rhonda


  14. That’s what I had assumed I’d have to do. The client is a law firm so it I’ll just work with the apps person to find a best practice. Thanks


  15. That, in theory does exactly what I want to do. Thanks very much. I only need one field “Version” which I have created as a style. I inserted the field into the centre of the footer (with Document title on the left and Page X of X on the right). The version shows up nicely on page one, but does not display on any other pages. I’m using word for Mac 2011 version 14. Any ideas?


  16. Hi Paul

    I don’t know how different Word for Mac is from Word for Windows, but in Word for Windows there’s a setting in the header/footer tab for ‘Different First Page’. It’s possible that’s selected.

    –Rhonda


  17. I have used STYLEREF many times with no problem, but right now I got an error, stating that there is no text in the document with the requested style (or whatever the English equivalent may be).
    What I tried to do was actually the opposite: I had the text that formed the name of the project, formatted with a style called “Project”, but I had that text piece in the header, and tried to populate the footer with it. It didn’t work. Is there a problem referring to styles that are contained in a Header/Footer?

    Thanks

    Bengt


  18. Hi Bengt

    I just tested this, and I get the error too. The only thing I can assume is that you can’t use a StyleRef field in a header for a unique style that’s in the footer (and vice versa).

    Of course, if the text is already in the header, I’d query why you want to repeat it in the footer anyway (or vice versa).

    –Rhonda


  19. Hi Rhonda,
    Thanks for your answer. Yes, I guess it is a fair question to ask, why you would repeat in the footer if it is in the header. However, this is what my job situation required me to do, and since this doesn’t seem to be possible to do, I guess my only option is to put the text somewhere in the document text. By doing so, I can obvously refer to if both in the header and the footer. Strange limitation though. As I said, I have been using Styleref a lot, but clearly, not in this fashion.

    Bengt


  20. Hi Rhonda,
    I’m trying to add two stylerefs to the header with a tab in between and I’m getting just the first field. I’ve tried adding just a space between, and the word “and” without any luck. Any ideas?
    thanks,
    karen


  21. Hi Karen

    The actual styles should only ever be used once in the document — that means a single use of one of these special styles in ONE paragraph or ONE cell of a table (see Step 3 above). Assuming that’s the case, then you should be able to insert styleref fields that refer to one or more of those special styles in the same paragraph (see Steps 5 and 6).

    Something might be getting messed up when you insert the tab — have you tried toggling field codes on to make sure you are entering the tab AFTER the field code curly braces? (You can show all field codes temporarily by setting this in the Options — for Word 2010, File > Options > Advanced > Show Document Content section > Show field codes instead of their values check box — don’t forget to turn it off when you’re finished)

    –Rhonda


  22. Hi guys, let me ask one additional question. Is there any chance to use a styleref in the footer to automatically put a certain text from the header (not from the “regular” document, where Word seems to be searching only) there?? Word does never find a result of the style selected (for example, to make it easy, “Heading1”) if this only appears in the header, but not in the normal text. Any ideas?? Thank you so much!!


  23. Looking for an answer like this for ages!


  24. Hi Rhonda! I have set up a few templates for my office that use this technique. There is one issue that I cannot find the answer to:

    The table (step 2) is a 3-column layout. In the cell that the StyleRef is pulling information from, the entered info often gets pushed to a second line within the cell.

    Example:
    City of Oceanside Department of
    Recreation

    Users, in an attempt to control how those two lines are split, will hit ‘Return/Enter.’

    Example:
    City of Oceanside[‘Return/Enter’]
    Department of Recreation

    When they do this, only the second line of the entered info gets displayed in the footer even though both lines within the table are the correct referenced style. Ideally, the two lines would appear in the footer on the same line. I doubt that I can get users to stop pressing ‘Return/Enter’ when filling in this information and was wondering if anyone has a hint/solution/macro that could help? This might be a stretch but do you know if there is a way to set a macro for the StyleRef that eliminates the ‘Return/Enter’ line jump before populating the footer?


  25. Have you tried Shift+Enter to add a ‘soft’ line break?


  26. Thanks for the response, Rhonda. That does work – all the information is translated to the footer (now as a two-line footer.) But I’m hoping that there is some way that I, as the administrator of these templates, can take the solution out of the user’s hands. As much as I hope people would use Word properly and learn the tricks, I doubt they will. If I could program the StyleRef (or maybe a macro?) to automatically do the soft break, that would be ideal. Any hints/tips are greatly appreciated.


  27. This is truly a nice function. However, I have run into a very peculiar issue: my reference only applies to the header on the first page. The second page remains unchanged and with no response to field update. Any suggestions?


  28. Hi Roger

    My guess is that you have ‘different first page’ set in the headers, or you have several sections in your doc and don’t have ‘link to previous’ set. You’ll need to set it up for the header in EACH section.

    –Rhonda


  29. I have also encountered an issue here . . . . i am trying out the ‘styleref’ field in a company template and am trying to understand how it works. The reference doesn’t always update on every footer – sometimes it will update on page 3, but not page 1 and 2. Then sometimes page 1 will suddenly update whilst navigating the document – but not page 2. There seems to be no consistency either in when it happens, it is random. It will however update on ‘print preview’ but i’d like to know why it isn’t updating in the first place. I have no ‘section breaks’, ‘link to previous’ or ‘different first page’ set. Any advice would be appreciated. Thanks.


  30. Hi! I’m encountering a funny issue when trying to use StyleRef for a dictionnary-style header, with the first and the last word in each page with a hyphen in between. Everything is fine until make the first letter in each section a dropped cap, then the header turns out to be just the capped letter (that’s OK with me) instead of the first word, a line break, the hyphen and the last word. How can I get rid of the line break?

    Thanks for any help,

    Jorge


  31. Hi Rhonda, Peter here from Red Pony. Do you know if the styleref functionality still works when your text is in a text box? I’ve had to use a text box to place the text on the page, I’ve then inserted a table, and inserted my content into the tables. However, when I apply my styles to the different cells and add my linking fields I am getting an error that there is no text of this style in my document.


  32. Hi Peter

    My personal opinion of text boxes in Word is that they are the spawn of the devil, and I avoid them wherever possible!

    I just did a quick test by adding a text box to an empty Word doc, styling it with one of the default styles (‘Subtitle’ in this case), then adding a table and trying to insert a StyleRef field to ‘Subtitle’ into a table cell.

    Guess what? I got the same result as you — a style used in a text box is NOT listed in the available styles for the StyleRef field.

    I repeated the test, but this time with text outside a text box, styled with Heading 2. It was the only Heading 2 style used in my test doc. When I tried to insert the StyleRef field into the table cell, Heading 2 WAS listed as an available style, as I expected.

    So, it looks like Word ignores any styles in text boxes for the purpose of the StyleRef field. Good pickup on your part, but a pain if you wanted to use it!

    Instead of using a text box, can you use a borderless table instead to emulate the layout you want?

    –Rhonda


  33. Hi Rhonda,

    Thanks for getting back to me so quickly and testing this one. I would normally use a table (I also avoid using text boxes wherever possible) but I need to anchor the text in place as it’s on a cover page, and I was also having problems with the table breaking early across a second page. It seems the answer is to use a frame instead, and put the table in that. I can then apply my styles and insert the field references.

    Cheers,
    Peter.


  34. Hi Rhonda- Thanks for these instructions. Like others I am running into the problem that the field does not populate in headers after the first page. I’d like to have a template that this can be used in when creating new documents. The header settings are “same as first page”. I am using a table as the reference. Even if I enter a StylRef field in the page 2 header, it does not populate. Any advice would be great!
    Thanks- David


  35. Hi David

    In addition to having Different First Page checked, do you also have Different Odd and Even checked? If so, that will result in what you’re getting. If not, then I don’t have any other ideas on what might be happening here.

    –Rhonda


  36. Hi Rhonda- Thanks for the reply. I decided to re-visit this idea today and I don’t know why… but it works fine now! Thanks for the instructions! I may have done something wrong the first time:) Operator Error :)

    thanks!


  37. I realize the original post was from 10 years ago, but there are already built-in field codes you can insert for all these document properties. Click Insert > Quick Parts > Field… and select DocProperty in the Field names: list. The Property: list at right contains the values set in Options, including Author, Title, Revision Number, Company, LastPrinted, and Subject.

    Most of these fields are also available directly from the Categories: (All) list. Type the initial letter to scroll down to that entry more quickly. Edit the field codes or add switches to customize.
    For example, if you want a copyright footer with the date and company name, type “©” and then insert a current date field code. Click the Field Codes button to reveal the formatting, and delete everything but the “yyyy”. It should look like this: DATE \@ “yyyy”
    Then select DocProperty from the Categories: list and “Company” from the Property: list. Click OK to insert.
    Now your footer (or disclaimer or other boilerplate) should read:
    “© 2019 EvilCorp”


  38. This seems like much more work than just placing the options on the front page, then entering those same options into the first header/footer.


  39. Hi Larry

    If you have a plain document with a single section, then yes, it is overkill. But the authors and I work on long, complex documents with many section breaks and a Document Control page where we re-use this text. For it to update automatically (or, in the case of the doc control page, with a field update), saves a lot of time, AND means we don’t need to check that the title, date, etc. in the headers/footers of the doc all match what’s on the cover page. Yes, this mismatch happens more often than you think with the older templates that didn’t use some form of doc automation for this — I would find old rev numbers, doc numbers, titles, dates, etc. in the headers/footers when doing my editing checks and have to change them (either manually or using ‘Link to Previous’). The authors are subject matter experts, not Word experts, so taking away some of their pain points is a no-brainer.

    –Rhonda


  40. Thanks for the reply. So how do I set this up for the headers and footers to update themselves from info on the front page?


  41. Hi Larry

    Follow the steps in the blog post. The critical thing is that the styles you use for the text you want to populate MUST be unique and not used in any other place in the document.

    –Rhonda


  42. Hi, Rhonda

    I’ve tried this method, but the field in the header won’t populate, all I get is the dreaded; Error! No text of specified style in document.

    Does the DocNumberName need to be bookmarked, referenced, or? The procedure did not specify.

    Larry


  43. Hi Larry

    The field is referenced to the StyleRef field and the style you selected for that. Right-click on the error, select Toggle Field Codes, and make sure you have something like { STYLEREF test \* MERGEFORMAT } (where ‘test’ is the name of the style you’re referencing).

    Assuming that’s correct, then make sure you have applied that unique style to a paragraph or table cell. I just did some testing, and found that if I styled the cell or paragraph with another style name, I got the same error message you did.

    –Rhonda


  44. Hi Rhoda,

    I’m having a similar issue to that reported by Bengt Eriksson (May 23, 2014 at 6:41 pm). I am trying to create a template for a client where they want a different header for the first page to subsequent pages, and both versions of the header includes the date. I hope to use the StyleRef function to reference the style I used for the date in the first header to replicate this information on subsequent pages so the user doesn’t have to enter it twice, but it looks like Word won’t allow you to use the StyleRef function in headers and footers at all.

    Peter.


  45. Hi Peter

    Yes, my testing back in 2014 and just now would indicate that a style ONLY used in the header can’t be ref’d using a StyleRef link in the footer (and presumably vice versa).

    If you’re trying to replicate a date, then perhaps another field may be better than StyleRef, such CreateDate (date the document was created), Date (today’s date), PrintDate (date last printed), or SaveDate (date last saved). These are the only built-in date fields that I’m aware of.

    –Rhonda


  46. Umm (disclaimer, I’m using office 365, which may be different) isn’t it easier to click in the header, click on Document Info, click on the item you want (if necessary from the submenu), and cursor right to continue. Yes? Or are you doing something different?
    Mind you, the above is very ingenious. I love making Word sing and dance!
    John


  47. Ah, I realise you want the info in the header to correspond to whatever’s in the table, not the Document Info data as stored by Word.
    And thank-you, because you’ve shown me how to solve my own problem, which is to copy the title I type in my document into the header. (And yes, I’ll set the header to be ‘different first page’ :) )
    Thanks!



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: