{"id":65443,"date":"2007-02-24T01:02:00","date_gmt":"2007-02-24T01:02:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/02\/24\/how-can-i-add-the-last-saved-date-to-the-footer-of-a-microsoft-word-document\/"},"modified":"2007-02-24T01:02:00","modified_gmt":"2007-02-24T01:02:00","slug":"how-can-i-add-the-last-saved-date-to-the-footer-of-a-microsoft-word-document","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-add-the-last-saved-date-to-the-footer-of-a-microsoft-word-document\/","title":{"rendered":"How Can I Add the Last-Saved Date to the Footer of a Microsoft Word Document?"},"content":{"rendered":"<p><H2><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\"> <\/H2>\n<P>Hey, Scripting Guy! How can I put the date that a Microsoft Word document was last saved in the footer of that document? Oh: and how can I center that footer while I\u2019m at it?<BR><BR>&#8212; GH<\/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, GH. Well, the best way to go about \u2013 say, wait a second here: that sounds suspiciously like <I>two<\/I> questions, one about putting the last-saved date in the footer, the other about centering that footer. You\u2019re not trying to trick the Scripting Guys and get a two-for-the-price of one deal, are you?<\/P>\n<P>If you are, well, that\u2019s OK; don\u2019t worry about it. After all, now that the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/default.mspx\"><B>2007 Winter Scripting Games<\/B><\/A> are complete the Scripting Guys have nothing else to do anyway.<\/P>\n<P>Well, other than finish testing all the entries and then total up the final results. And draw names and mail out <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/games07\/bobble.mspx\"><B>250 Dr. Scripto bobblehead dolls<\/B><\/A> and 20 copies of <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/msh\/payette1.mspx\"><B>Windows PowerShell in Action<\/B><\/A>. And put together a couple of follow-up articles on the Games and how people went about solving the various challenges. And \u2026.<\/P>\n<P><I>Editor\u2019s Note: Anyone else getting the feeling that the Scripting Guy who writes this column is getting a little whiney these days?<\/I><\/P>\n<P>You know, that <I>does<\/I> sound like a lot of work, doesn\u2019t it? But any time there\u2019s work to be done there\u2019s one thing you can rely on the Scripting Guy who writes this column to do: spend his time answering your questions about Microsoft Word footers and letting Scripting Guy Jean Ross take care of all that Scripting Games stuff. Here\u2019s an answer to your two questions, GH. That takes care of our end of the bargain. And we\u2019re sure that Jean will work day and night in order to complete her responsibilities as well.<\/P>\n<P>Here\u2019s the script:<\/P><PRE class=\"codeSample\">Const wdFieldSaveDate = 22\nConst wdAlignParagraphCenter = 1<\/p>\n<p>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\nobjDoc.Fields.Add objRange, wdFieldSaveDate<\/p>\n<p>objRange.ParagraphFormat.Alignment = wdAlignParagraphCenter<\/p>\n<p>objDoc.SaveAs(&#8220;C:\\Scripts\\Test.doc&#8221;)\n<\/PRE>\n<P>Thanks, and see you tomorrow.<\/P>\n<P>What\u2019s that? Explain how this script works? Oh, come on, GH. After all, we already wrote the script; shouldn\u2019t <I>Jean<\/I> have to do something here, too?<\/P>\n<P>OK, fine. As you can see, we start out by defining a pair of constants: wdFieldSaveDate (with a value of 22), which we\u2019ll use to insert the date that the document was last saved; and wdAlignParagraphCenter (with a value of 1), which we\u2019ll use to center the footer. We then use the following block of code to create an instance of the <B>Word.Application<\/B> object, make this instance visible onscreen, and then create a new document:<\/P><PRE class=\"codeSample\">Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nobjWord.Visible = True<\/p>\n<p>Set objDoc = objWord.Documents.Add()\n<\/PRE>\n<P>Now we\u2019re ready to add the last-saved date to the footer of this new document. To begin with, we use this line of code to create an object reference to the first (and, in this case, only) footer in the document:<\/P><PRE class=\"codeSample\">Set objRange = objDoc.Sections(1).Footers(1).Range\n<\/PRE>\n<P>Once we have an object reference to the footer range we can call the <B>Add<\/B> method of the <B>Fields<\/B> collection and add the last-saved date:<\/P><PRE class=\"codeSample\">objDoc.Fields.Add objRange, wdFieldSaveDate\n<\/PRE>\n<P>In case you\u2019re wondering (and you probably are), adding the last-saved date to a document using a script works very similar to the process of adding that value from within Word itself. If you\u2019re working in Microsoft Word you can add the last-saved date to a document by (in Word 2003) selecting <B>Field<\/B> from the <B>Insert<\/B> menu and then choosing <B>SaveDate<\/B>. Here we\u2019re doing essentially the same thing, calling the Add method and then passing along two parameters: the object reference to the location where we want to add the field; and the constant representing the field we want to add. How did we know that we need to use the constant wdFieldSaveDate, with a value of 22, in order to add the last-saved date to the footer? To tell you the truth, we didn\u2019t; that\u2019s why we looked this up in the <A href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/aa211923(office.11).aspx\" target=\"_blank\"><B>Microsoft Word VBA Language Reference<\/B><\/A> (look for wdFieldType).<\/P>\n<P>So much for question 1; that\u2019s all we had to do to add the last-saved date to the footer. Now let\u2019s tackle question 2, something we can take care of with a single line of code:<\/P><PRE class=\"codeSample\">objRange.ParagraphFormat.Alignment = wdAlignParagraphCenter\n<\/PRE>\n<P>As you can see, we\u2019re again working with the footer range (note the object reference objRange). This time, though, we\u2019re accessing the <B>ParagraphFormat<\/B> object and assigning a value to the <B>Alignment<\/B> property. How did we know that the constant wdAlignParagraphCenter, with a value of 1, would give us centered alignment. That\u2019s right: we didn\u2019t. (Hint: You\u2019ll rarely go wrong by assuming that the Scripting Guys don\u2019t know much of anything.) Once again, however, it\u2019s the <A href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/aa211923(office.11).aspx\" target=\"_blank\"><B>Microsoft Word VBA Language Reference<\/B><\/A> to the rescue (look for wdParagraphAlignment).<\/P>\n<P>In the very last line of the script we then use the <B>SaveAs<\/B> method to save the document as C:\\Scripts\\Test.doc. We do that simply so you can verify that the script worked; after all, if the document has never been saved then it won\u2019t have a last-saved date.<\/P>\n<P>We hope that answers both your questions, GH. Which reminds us: we actually did do <I>two<\/I> things here, didn\u2019t we? Looks like Jean\u2019s not only going to have to finish up with the 2007 Scripting Games, but she\u2019ll have to take care of the 2008 Scripting Games as well. But hey, fair is fair, right?<\/P>\n<P><I>Editor\u2019s Note: <\/I><I>Maybe<\/I><I> with Jean taking over <\/I><I>everything <\/I><I>the 2008 Scripting Games won\u2019t <\/I><I>have another <\/I><A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/games07\/news.mspx\"><B><I>Event 8 episode<\/I><\/B><\/A><I> like we sa<\/I><I>w<\/I><I> this year.<\/I><I> We won\u2019t mention who came up with that one.<\/I><\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I put the date that a Microsoft Word document was last saved in the footer of that document? Oh: and how can I center that footer while I\u2019m at it?&#8212; GH Hey, GH. Well, the best way to go about \u2013 say, wait a second here: that sounds suspiciously like [&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-65443","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! How can I put the date that a Microsoft Word document was last saved in the footer of that document? Oh: and how can I center that footer while I\u2019m at it?&#8212; GH Hey, GH. Well, the best way to go about \u2013 say, wait a second here: that sounds suspiciously like [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65443","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=65443"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65443\/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=65443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}