{"id":6414,"date":"2021-11-19T07:21:13","date_gmt":"2021-11-19T07:21:13","guid":{"rendered":"https:\/\/lgildv5i97.onrocket.site\/answers\/?post_type=question&#038;p=6414"},"modified":"2021-11-19T07:21:42","modified_gmt":"2021-11-19T07:21:42","slug":"word-vba-autoopen-for-field-updating-not-working","status":"publish","type":"question","link":"https:\/\/computing.net\/answers\/office\/word-vba-autoopen-for-field-updating-not-working\/21509.html","title":{"rendered":"Word VBA AutoOpen For Field Updating &#8211; Not Working"},"content":{"rendered":"<p>I have various word documents that have page numbers in header but also a field with just the number of pages somewhere on page 1.<\/p>\n<p>Header page number format is: PAGE \/ PAGES with the appropriate field codes.<br \/>\nThe number on page 1 is just: PAGES with the appropriate field code { NUMPAGES \\* MERGEFORMAT}<\/p>\n<p>While header page numbers auto update when inserting a new page or removing one, the number of pages on page 1 doesn&#8217;t update.<\/p>\n<p>I know there are keyboard shortcuts for updating, I know I can make a simple vba code linked to a button in the Quick Access Toolbar.<\/p>\n<p>But the end user knows none of this and is interested in the number of pages to auto update so she doesn&#8217;t have to do additional steps. Because she says if additional steps are required, what&#8217;s the point in having the field there that doesn&#8217;t auto update? She could write the number of pages manually.<\/p>\n<p>So I&#8217;ve been trying to update the field via the AutoOpen sub. It&#8217;s not working well. I&#8217;ve tried various codes for updating all fields in a document. They all work when using a button in the QAT, but none of them works well on document open. I&#8217;ve tried both AutoOpen and Document_Open subs. I put them in Normal.dotm, both in a module and in ThisDocument.<\/p>\n<p><b>Working well means<\/b>\u00a0displaying the correct number of pages in the field.<\/p>\n<p>Here are the codes I tried (they work well when linked to a button):<\/p>\n<p>&nbsp;<\/p>\n<pre>    ActiveDocument.Fields.Update\r\n\r\n' ###### END OF CODE 1\r\n\r\nDim aStory As Range\r\nDim aField As Field\r\n\r\n   For Each aStory In ActiveDocument.StoryRanges\r\n      For Each aField In aStory.Fields\r\n         aField.Update\r\n      Next aField\r\n   Next aStory\r\n\r\n' ###### END OF CODE 2\r\n\r\nDim oStory As Range\r\nFor Each oStory In ActiveDocument.StoryRanges\r\n    oStory.Fields.Update\r\n    If oStory.StoryType &lt;&gt; wdMainTextStory Then\r\n        While Not (oStory.NextStoryRange Is Nothing)\r\n            Set oStory = oStory.NextStoryRange\r\n            oStory.Fields.Update\r\n        Wend\r\n    End If\r\nNext oStory\r\nSet oStory = Nothing\r\n\r\n' ###### END OF CODE 3<\/pre>\n<p>I even tried selecting the specific field like I do manually by using a bookmark and the following code:<\/p>\n<pre>    ActiveDocument.Bookmarks(\"PgNum\").Range.Fields(1).Result.Select\r\n    Selection.Fields.Update<\/pre>\n<p><b>Not working well means:<\/b><br \/>\nThe above codes update the field, but instead of updating with the number of pages (which is 7) it just updates to 2. Each above code does the same.<\/p>\n<p>I found a workaround by inserting the number of pages in the bookmark field each time I open a word document containing that bookmark field. But the end user isn&#8217;t going to do bookmark fields in future documents, so I need that fields to update whenever a word document is opened.<\/p>\n<p>Is there something that I am missing?<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"inline_featured_image":false,"iawp_total_views":7},"question-category":[53],"question_tags":[],"class_list":["post-6414","question","type-question","status-publish","hentry","question-category-office"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question\/6414","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question"}],"about":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/types\/question"}],"author":[{"embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/comments?post=6414"}],"wp:attachment":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/media?parent=6414"}],"wp:term":[{"taxonomy":"question-category","embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question-category?post=6414"},{"taxonomy":"question_tags","embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question_tags?post=6414"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}