{"id":64713,"date":"2007-06-06T23:30:00","date_gmt":"2007-06-06T23:30:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/06\/06\/how-can-i-add-a-page-x-of-y-footer-to-a-microsoft-word-document\/"},"modified":"2007-06-06T23:30:00","modified_gmt":"2007-06-06T23:30:00","slug":"how-can-i-add-a-page-x-of-y-footer-to-a-microsoft-word-document","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-add-a-page-x-of-y-footer-to-a-microsoft-word-document\/","title":{"rendered":"How Can I Add a Page X of Y Footer to a Microsoft Word Document?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! I saw your article on <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/may06\/hey0510.mspx\"><B>adding page numbers to the footer of a Word document<\/B><\/A>. However, I need my page numbers to be in the format &#8220;Page X of Y,&#8221; where X is the current page number and Y is the total number of pages. How can I do that?<BR><BR>&#8212; JS <\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, JS. You know, several years ago the grocery store by the Scripting House included one of those claw-type arcade games; you know, the kind where you maneuver this big claw around and try to grab a prize. Back then the Scripting Son was infatuated with the idea of winning one of the big prizes. For awhile the Scripting Dad indulged his son, but it wasn\u2019t long before Dad got tired of wasting a dollar each time they went to the store; after all, despite his best efforts the Scripting Son never even came close to winning <I>any<\/I> prize, let alone a really cool prize.<\/P>\n<P>\u201cThis game is impossible,\u201d the Scripting Dad told the Scripting Son. \u201cI\u2019m not wasting any more money on it. You can\u2019t win;<I> there\u2019s no way<\/I>.\u201d<\/P>\n<P>\u201cNo, Dad, there\u2019s a trick to it, and I figured out what the trick is,\u201d replied the Scripting Son. \u201cPlease, let me have just one dollar? I know I can do it.\u201d<\/P>\n<P>But the Scripting Dad remained resolute. After all, if the Scripting Dad says something is impossible then it <I>must<\/I> be impossible. End of discussion.<\/P>\n<P>We mention this simply because the Scripting Dad also believed it was impossible to programmatically add a Page X of Y footer to a Word document. This question has come up before, and the Scripting Guy who writes this column couldn\u2019t figure out how to do. And if the Scripting Guy who writes this column can\u2019t figure out how to do something then it <I>must<\/I> be impossible. End of discussion.<\/P>\n<P>Except, as it turns out, there\u2019s a trick to it. And once you figure out the trick, well, guess what? You really <I>can<\/I> add a Page X of Y footer to a Word document:<\/P><PRE class=\"codeSample\">Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nobjWord.Visible = True<\/p>\n<p>Set objDoc = objWord.Documents.Add()<\/p>\n<p>Set objRange = objDoc.Sections(1).Footers(1).Range<\/p>\n<p>Set objTemplate = objDoc.AttachedTemplate\nobjTemplate.AutoTextEntries(&#8220;Page X of Y&#8221;).Insert objRange\n<\/PRE>\n<P>So what <I>is<\/I> this trick? We\u2019ll explain that in just a second. Before we do that, however, let\u2019s talk about the rest of the script. We start out by creating an instance of the <B>Word.Application<\/B> object and setting the <B>Visible<\/B> property to True; that gives us a running instance of Microsoft Word that we can see onscreen. After using the <B>Add<\/B> method to add a new, blank document to our instance of Word, we then use this line of code to create a <B>Range<\/B> object corresponding to the first footer in the first section of our document:<\/P><PRE class=\"codeSample\">Set objRange = objDoc.Sections(1).Footers(1).Range\n<\/PRE>\n<P>And you\u2019re right: because we don\u2019t have any other sections in the document (let alone any other footers) that means this Page X of Y footer will apply to the entire document.<\/P>\n<P>Here comes the tricky part. Our next step is to create an instance of the <B>Template<\/B> object; we do that by binding to the document\u2019s <B>AttachedTemplate<\/B> property:<\/P><PRE class=\"codeSample\">Set objTemplate = objDoc.AttachedTemplate\n<\/PRE>\n<P>And then we insert one of the template\u2019s <B>AutoTextEntries<\/B>; to be a more specific, we insert the <B>Page X of Y<\/B> entry:<\/P><PRE class=\"codeSample\">objTemplate.AutoTextEntries(&#8220;Page X of Y&#8221;).Insert objRange\n<\/PRE>\n<P>Needless to say, this is way easier than we thought. We bind to the AutoTextEntries collection and call the <B>Insert<\/B> method, specifying Page X of Y as the autotext to be entered. We also have to specify where we want the text entered. Where <I>do<\/I> we want this text entered? You got it: objRange, the object reference specifying the document footer.<\/P>\n<P>And that, believe it or not, is all we need to do.<\/P>\n<P>OK, that\u2019s not true; with this kind of footer you probably want the text center-aligned. If that\u2019s the case, then just tack this line of code onto the end of the script:<\/P><PRE class=\"codeSample\">objRange.ParagraphFormat.Alignment = 1\n<\/PRE>\n<P>And <I>that<\/I> is all we need to do.<\/P>\n<P>Incidentally, you might be interested in knowing how the Scripting Guy who writes this column finally solved this riddle. He knew it was possible to do a Page X of Y footer using the Word GUI. Therefore, he started a macro recording and went ahead and used the GUI to add the desired footer. When he was done he edited the macro in order to view the macro code. After wading through the VBA code he found this:<\/P><PRE class=\"codeSample\">NormalTemplate.AutoTextEntries(&#8220;Page X of Y&#8221;).Insert Where:=Selection. _\n        Range, RichText:=True\n<\/PRE>\n<P>That was the hint he was looking for. That\u2019s something to remember when writing scripts that interact with Microsoft Word: any time you get stuck try recording a macro that carries out the desired action. The VBA code will often be a little obtuse, but it will usually point you in the right direction.<\/P>\n<P>So does the Scripting Dad feel bad that he didn\u2019t give the Scripting Son a dollar and let him try to win a really cool prize? No, not really. For one thing, the Scripting Son has found plenty of other ways to pry money from the Scripting Dad. For another, the prizes you could win back then really weren\u2019t all that cool. For example, did the claw game offer you the chance to win a Dr. Scripto bobblehead doll or a Scripting Guys gift pack? What, are you kidding? No, there are only two ways you can win a really cool prize like <I>those<\/I>:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>If you\u2019re attending <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/teched07\/default.mspx\"><B>TechEd 2007<\/B><\/A> swing by the CMP Media Booth (booth number 1301), find the Scripting Guys, get a copy of Dr. Scripto\u2019s Fun Book, and enter the drawing for a Dr. Scripto bobblehead doll. If you aren\u2019t sure who the Scripting Guys are, just look for a cheapskate in a baseball hat. That\u2019s the Scripting Dad.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Even if you\u2019re not attending TechEd 2007 you have a shot at winning a cool prize by entering the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/teched07\/challenge.mspx\"><B>TechEd Challenge<\/B><\/A>. Don\u2019t delay, though; that contest ends June 30th.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>So is there a trick to winning the TechEd Challenge? You bet there is. However, the Scripting Editor always gets a little nervous when we say things like, \u201cSend us $100 and we\u2019ll rig the contest and make sure you win.\u201d Therefore, we won\u2019t say that.<\/P>\n<P>But it <I>is<\/I> something to think about, isn\u2019t it? <I>(<\/I><I>Ed<\/I><I>itor\u2019s Note: No.)<\/I><\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I saw your article on adding page numbers to the footer of a Word document. However, I need my page numbers to be in the format &#8220;Page X of Y,&#8221; where X is the current page number and Y is the total number of pages. How can I do that?&#8212; JS Hey, [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[84,49,3,5],"class_list":["post-64713","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-word","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I saw your article on adding page numbers to the footer of a Word document. However, I need my page numbers to be in the format &#8220;Page X of Y,&#8221; where X is the current page number and Y is the total number of pages. How can I do that?&#8212; JS Hey, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64713","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=64713"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64713\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=64713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}