{"id":3168,"date":"2016-03-20T04:58:08","date_gmt":"2016-03-20T04:58:08","guid":{"rendered":"http:\/\/www.ericwhite.com\/home2\/bm8qcmjy\/public_html\/blog\/?page_id=3168"},"modified":"2016-03-20T09:42:31","modified_gmt":"2016-03-20T09:42:31","slug":"updating-the-toc-in-a-wordprocessingml-document-using-an-autoopen-macro","status":"publish","type":"page","link":"https:\/\/www.ericwhite.com\/blog\/updating-the-toc-in-a-wordprocessingml-document-using-an-autoopen-macro\/","title":{"rendered":"Updating the TOC in a WordprocessingML Document using an AutoOpen Macro"},"content":{"rendered":"<p><span class=\"Back\"><a class=\"Back\" href=\"https:\/\/www.ericwhite.com\/blog\/introduction-to-wordprocessingml-series\/\">Return to the<br \/>WordprocessingML Screen-Cast<br \/>Series<\/a><\/span><\/p>\n<p>I had thought that there were only going to be four screen-casts in this series on adding \/ updating tables-of-contents in OpenXML WordprocessingML documents.  However, I can see that there are now going to be at least six.<\/p>\n<p>In the screen-cast that I present in this post, I show how you can add a TOC using the Open XML SDK, and then modify the normal.dotx, adding a macro, so that whenever you open a document that contains a TOC, Word repaginates the document and updates the TOC.  This is an approach that would be useful if, for instance, you have a department of sales engineers who prepare proposals that need a TOC inserted at the beginning of the proposal.  You could write a small program that would install the AutoOpen macro, and distribute this program to all members of the department.  Alternatively, you could post instructions on an internal web site showing how to add the macro.  It is important to note that nothing will go wrong if one of the salespeople does not have the macro.  If the macro is not there, then it simply means that the user needs to click on the table of contents and update it by clicking on the content control tab at the top of the TOC.<\/p>\n<p><iframe loading=\"lazy\" title=\"TocAdder5.wmv\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/GtVB3VKdUqk?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>Here is the complete list of screen-casts in this series.<\/p>\n<table style=\"border-collapse:collapse;border:none;\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td style=\"border:solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt;font-weight:bold;\" valign=\"top\">\n<p>Link<\/p>\n<\/td>\n<td style=\"border:solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt;font-weight:bold;\" valign=\"top\">\n<p>Summary<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"border:solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt;\" valign=\"top\">\n<p><a href=\"https:\/\/www.ericwhite.com\/blog\/screen-cast-exploring-tables-of-contents-in-open-xml-wordprocessingml-documents\/\">Screen-cast #1<\/a><\/p>\n<\/td>\n<td style=\"border:solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt;\" valign=\"top\">\n<p>Explains the markup of tables-of-contents. TOCs use field markup.<br \/>See <a href=\"https:\/\/www.ericwhite.com\/blog\/2011\/04\/25\/deep-dive-into-open-xml-wordprocessingml-fields-and-hyperlinks\/\">Deep dive into OpenXML Fields<\/a> for more info.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"border:solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt;\" valign=\"top\">\n<p><a href=\"https:\/\/www.ericwhite.com\/blog\/exploring-tables-of-contents-in-open-xml-wordprocessingml-documents-part-2\/\">Screen-cast #2<\/a><\/p>\n<\/td>\n<td style=\"border:solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt;\" valign=\"top\">\n<p>Presents some sample code that shows how to insert TOC markup into a document.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"border:solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt;\" valign=\"top\">\n<p><a href=\"https:\/\/www.ericwhite.com\/blog\/exploring-tables-of-contents-in-open-xml-wordprocessingml-documents-part-3\/\">Screen-cast #3<\/a><\/p>\n<\/td>\n<td style=\"border:solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt;\" valign=\"top\">\n<p>Shows how to use Word Automation to update the TOC.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"border:solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt;\" valign=\"top\">\n<p><a href=\"https:\/\/www.ericwhite.com\/blog\/exploring-tables-of-contents-in-openxml-wordprocessingml-documents-part-4\/\">Screen-cast #4<\/a><\/p>\n<\/td>\n<td style=\"border:solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt;\" valign=\"top\">\n<p>Shows how to use Word Automation Services to update the TOC.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"border:solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt;\" valign=\"top\">\n<p><a href=\"https:\/\/www.ericwhite.com\/blog\/updating-the-toc-in-a-wordprocessingml-document-using-an-autoopen-macro\/\">Screen-cast #5<\/a><\/p>\n<\/td>\n<td style=\"border:solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt;\" valign=\"top\">\n<p>Shows how to use an AutoOpen macro to update the TOC whenever any document that contains a TOC is opened.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Following is the listing for the AutoOpen macro:<\/p>\n<pre class=\"prettyprint\"><span class=\"typ\">Sub<\/span><span class=\"pln\"> <\/span><span class=\"typ\">AutoOpen<\/span><span class=\"pun\">()<\/span><span class=\"pln\"><br><\/span><span class=\"str\">'<br>'<\/span><span class=\"pln\"> <\/span><span class=\"typ\">AutoOpen<\/span><span class=\"pln\"> <\/span><span class=\"typ\">Macro<\/span><span class=\"pln\"><br><\/span><span class=\"str\">'<br>'<\/span><span class=\"pln\"><br>&nbsp; &nbsp; <\/span><span class=\"str\">' Update the entire first table of contents.<br>&nbsp; &nbsp; '<\/span><span class=\"pln\"> <\/span><span class=\"typ\">The<\/span><span class=\"pln\"> TOC must exist <\/span><span class=\"kwd\">or<\/span><span class=\"pln\"> <\/span><span class=\"kwd\">this<\/span><span class=\"pln\"> produces an error<\/span><span class=\"pun\">.<\/span><span class=\"pln\"><br>&nbsp; &nbsp; <\/span><span class=\"typ\">On<\/span><span class=\"pln\"> <\/span><span class=\"typ\">Error<\/span><span class=\"pln\"> <\/span><span class=\"typ\">GoTo<\/span><span class=\"pln\"> <\/span><span class=\"typ\">DontUpdate<\/span><span class=\"pln\"><br>&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span class=\"typ\">ActiveDocument<\/span><span class=\"pun\">.<\/span><span class=\"typ\">TablesOfContents<\/span><span class=\"pun\">(<\/span><span class=\"lit\">1<\/span><span class=\"pun\">).<\/span><span class=\"typ\">Update<\/span><span class=\"pln\"><br>&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span class=\"typ\">ActiveDocument<\/span><span class=\"pun\">.<\/span><span class=\"typ\">TablesOfContents<\/span><span class=\"pun\">(<\/span><span class=\"lit\">1<\/span><span class=\"pun\">).<\/span><span class=\"typ\">UpdatePageNumbers<\/span><span class=\"pln\"><br>&nbsp; &nbsp; <br><\/span><span class=\"typ\">DontUpdate<\/span><span class=\"pun\">:<\/span><span class=\"pln\"><br>&nbsp; &nbsp; <\/span><span class=\"typ\">On<\/span><span class=\"pln\"> <\/span><span class=\"typ\">Error<\/span><span class=\"pln\"> <\/span><span class=\"typ\">GoTo<\/span><span class=\"pln\"> <\/span><span class=\"lit\">0<\/span><span class=\"pln\"><br>&nbsp; &nbsp; <br><\/span><span class=\"typ\">End<\/span><span class=\"pln\"> <\/span><span class=\"typ\">Sub<\/span><\/pre>\n<p>One key point about this approach: you don&#8217;t want to set the &lt;w:updateFields val=&#8217;true&#8217;&gt; element in the settings part. &nbsp;If this element is there, then even though there is a macro that will update the TOC, Word will still put up a modal dialog box indicating, &#8220;This document contains fields that may refer to other files. &nbsp;Do you want to update the fields in this document.&#8221; &nbsp;In addition, you do not want to set the w:dirty attribute to true on the&nbsp;<span style=\"white-space:pre;\">&lt;w:fldChar&nbsp;w:fldCharType=&#8217;begin&#8217;\/&gt; element.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Return to theWordprocessingML Screen-CastSeries I had thought that there were only going to be four screen-casts in this series on adding \/ updating tables-of-contents in OpenXML WordprocessingML documents. However, I can see that there are now going to be at least six. In the screen-cast that I present in this post, I show how you [&hellip;]<\/p>\n","protected":false},"author":10567,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"_s2mail":"","footnotes":""},"class_list":["post-3168","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/3168","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/users\/10567"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/comments?post=3168"}],"version-history":[{"count":2,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/3168\/revisions"}],"predecessor-version":[{"id":3176,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/3168\/revisions\/3176"}],"wp:attachment":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/media?parent=3168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}