Vba word update fields in header


















The second method simply looks thru all stories and does not treat headers and footers special. WHY are indexes not included here? Sections 1. Headers 1. Update Select Case rngStory. ShapeRange If oShp. HasText Then oShp. Sub UpdateAll ' from GMayor. StoryRanges oStory. Update If oStory. NextStoryRange oStory. Specifically in the TextFrame of a shape located in a header or footer. If there are fields in e. TIP : If you experience that one or more cross-reference fields do not update as expected, see the article Cross-reference Problems - Troubleshooting.

This method is not sufficient if other kinds of fields need to be updated. You can update all fields anywhere in the document in one operation using a macro made for that purpose. This is most often the smartest method, at least in documents where fields may be spread across many layers in the document.

If you make an update field macro available via the Quick Access Toolbar QAT or a keyboard shortcut, it takes only a single click to update all fields in the entire document. I have created a ready-for-use macro that updates all fields in the document no matter where they are found. You can get the Update Fields macro here. If you turn ON the option Update fields before printing , all fields in the document will be updated when you print.

See the illustration below. Note that this option is a global Word option that applies to the individual user's Word. It is not stored in the individual document. This means that you cannot rely on all users having this option set the same way unless it is managed by e. In general, a document you print should reflect the actual content of the document. This also means that you would most often want fields to be updated when printing. There may, however, be situations where you want to print a document without fields being updated.

Maybe they are not set up correctly for the purpose. I have seen examples where more or less random fields had been copied from other documents without the user updating them in the new document. The fields were indeed wrong in the context. When the fields were updated during print, they revealed undesired field results. Instead of fixing the fields, the user wanted to prevent them from being updated when printing.

Instead, the problem should be solved by correcting the fields. There may be situations when you want to prevent the result of a field from being updated. You can obtain this by locking the field. Locked fields will not change even if you update fields:. Another way of retaining the current field result is to convert the field to normal text.

You can insert the entire document or a bookmarked portion of the document. For example, cross-reference fields and other types of fields could be found. A field with the following field code will insert the content from the document "My Document. That is the default behavior. Let's say a cross-reference field in the source document shows "My heading text".

However, the heading it points to has been changed to "My new heading text" without updating the cross-reference field. You can get my Update All Fields macro here.

See the Microsoft article List of field codes in Word for information about the individual types of Word fields, their syntax and how to use them. The Microsoft support article Some fields are updated while other fields are not lists fields that are updated automatically or updated e. However, the lists don't seem to be complete according to my experiments. In the lists above, you will see that more fields are listed in those categories than in the Microsoft article.

By default, Word has a number of keyboards shortcuts related to fields. Knowing those shortcuts can save you a lot of time. See my article about useful shortcuts related to fields. For details about cross-reference fields, see my article How cross-reference fields in Word work on my website wordaddins.

In case of problems with cross-reference fields not being updated as expected, see my article Cross-reference Problems - Troubleshooting. Updating Fields in Word — How it Works. Quick Navigation Fields in headers and footers of documents.

Most fields in headers and footers update automatically when you open a document. Fields in headers and footers are updated when you switch to Print Preview.

Fields in footnotes, endnotes, text boxes and shapes. Fields in the main body of documents. The rules that apply to how different types of fields update - cold, warm and hot fields. Doug's code, because it specifically calls Update on the fields of the target document, doesn't care whether target is active or not. I am deeply grateful that you have tried to explain to me about why it just won't get updated.

That's very helpful as I now know what's going on. There is always something new to learn everyday. Thank you very much. I will keep trying to work with Doug's code. It's just that I have spent lots of time trying out everything that I could think of but none actually works so I am just kind of frustrated.

Office Office Exchange Server. Not an IT pro? Resources for IT Professionals. Sign in. United States English. Ask a question. Quick access. Search related threads. Remove From My Forums. Answered by:. Archived Forums. Word IT Pro Discussions. This forum is for general questions and feedback related to Word all versions as they pertain to the IT Pro community.

Sign in to vote. Hi, I am having difficulty in getting the variable in the footer updated using VBA. The following codes I quoted from Doug from my previous post plus the codes I added to it. That means, closing the document triggers the event that updates all fields. It's slick and easy. The only real problem is deciding what event to use, and Close makes the most sense to me. You can use the Open event, but that won't update fields after you modify the document.

You could trigger both the Open and Close events to cover most situations. In addition, check the option reviewed at the beginning of this article to cover almost every possible scenario:. There is one possibility that isn't covered: The user opens the file, makes modifications, and then sends the file without closing the document, which isn't likely because most of us would close and save the document before sending it.

I've added the procedure in Listing A to the downloadable files. I'm using the demonstration files used in the linked article in the introduction. You can work with any Word document, but you'll want to add a field that doesn't update automatically to the body of the document. There are three pages. On page 2 is a text reference. On page 3, a cross-reference field returns the page number of the text reference on page 2.

If you enter a page between pages 1 and 2, the text reference moves to page 3 and the cross-reference moves to page 4. However, the cross-reference will still return page 2 because it won't update automatically.



0コメント

  • 1000 / 1000