{"id":64673,"date":"2007-06-12T23:44:00","date_gmt":"2007-06-12T23:44:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/06\/12\/how-can-i-put-the-file-name-in-the-footer-of-a-microsoft-word-document\/"},"modified":"2007-06-12T23:44:00","modified_gmt":"2007-06-12T23:44:00","slug":"how-can-i-put-the-file-name-in-the-footer-of-a-microsoft-word-document","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-put-the-file-name-in-the-footer-of-a-microsoft-word-document\/","title":{"rendered":"How Can I Put the File Name in the Footer of 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\u2019ve seen Word documents where the file name appears in the footer. Except that this file name is dynamic: if the name of the document changes then the footer changes, too. How can I add that kind of a footer using a script?<BR><BR>&#8212; ZK <\/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, ZK. You know, it\u2019s always tough to come back to work after being gone for a week; there\u2019s just so many things to catch up on. For example, we haven\u2019t had the chance yet to mention the fact that, in a study of the 25 largest metropolitan areas in the U.S., Seattle drivers ranked 23<SUP>rd<\/SUP> out of 25 when it came to the rudest drivers. (In other words, we have very <I>polite<\/I> drivers here. The rudest drivers of all? Those from Miami.) <\/P>\n<P>The Scripting Guy who writes this column finds this interesting, in part because none of his Scripting Siblings ever come over to the Seattle area to visit him. Why not? \u201cNo way am I driving with all those crazy Seattle drivers!\u201d Except, as it turns out, Seattle drivers <I>aren\u2019t<\/I> crazy drivers after all.<\/P>\n<P>Hmmm; wonder why they <I>really<\/I> don\u2019t ever come over to visit him?<\/P>\n<TABLE class=\"dataTable\" id=\"EMD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. Actually there\u2019s a simple, logical explanation why they don\u2019t come over: driving from the Tri-Cities to Seattle is a <I>long<\/I> drive, too long to make for a weekend visit. Apparently, though, it\u2019s way shorter and way easier for the Scripting Guy who writes this column to drive from Seattle to the Tri-Cities. And he\u2019s expected to do so, even if that means driving over, attending some sort of family function, and then turning around and driving back.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>So if Seattle-area drivers are nice does that mean that Seattle-area Scripting Guys are nice, too? And does that also mean that they\u2019d be happy to answer a question about adding a dynamic file name to the footer of a Word document? Heck no. But they\u2019ll answer the question anyway:<\/P><PRE class=\"codeSample\">Const wdFieldFileName = 29<\/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\nSet objField = objDoc.Fields.Add(objRange, wdFieldFileName)<\/p>\n<p>objDoc.SaveAs &#8220;C:\\Scripts\\Test.doc&#8221;\n<\/PRE>\n<P>OK. This script starts out by defining a constant named wdFieldFileName and setting the value to 29. As it turns out, Word includes a number of built-in \u201cfields\u201d that can display information such as file name, user name, author name, last-saved date, last-printed date, etc. The field that displays the file name has a value of 29; hence we assign our constant the value 29.<\/P>\n<TABLE class=\"dataTable\" id=\"E4D\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. Gosh, have the Scripting Guys memorized all these Word fields <I>and<\/I> their corresponding values? No, of course not, we \u2013 uh, yes, yes we have; that\u2019s what makes us the Scripting Guys. For the rest of you, though, those constants \u2013 and their values \u2013 can be found by looking in the Microsoft Word SDK and, in particular, by looking at the <A href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/aa211923(office.11).aspx\" target=\"_blank\"><B>wdFieldType enumeration<\/B><\/A>.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>After defining the constant we next create an instance of the <B>Word.Application<\/B> object and set the <B>Visible<\/B> property to True; that gives us a running instance of Word that we can see onscreen. We then use this line of code to add a new, blank document to our instance of Word:<\/P><PRE class=\"codeSample\">Set objDoc = objWord.Documents.Add()\n<\/PRE>\n<P>Now that we have a document we need to insert the field for the file name in the footer. To do that, we first create a Range object that encompasses the first footer in the first section of the document (which, for a brand-new document, will also be the <I>only<\/I> footer in the document):<\/P><PRE class=\"codeSample\">Set objRange = objDoc.Sections(1).Footers(1).Range\n<\/PRE>\n<P>And then we\u2019re ready to insert the field:<\/P><PRE class=\"codeSample\">Set objField = objDoc.Fields.Add(objRange, wdFieldFileName)\n<\/PRE>\n<P>That\u2019s right: just one line of code. We reference the <B>Fields<\/B> collection and then call the <B>Add<\/B> method. This method requires two parameters: the spot in the document where we want to insert the field (objRange, the object reference to the footer) and the value of the field to be inserted. This, of course, is where we use our constant wdFieldFileName.<\/P>\n<TABLE class=\"dataTable\" id=\"EKF\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. By default, this script left-aligns the footer. If you\u2019d prefer the footer to be center-aligned then just add this bit of code after inserting the field: objRange.ParagraphFormat.Alignment = 1.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Of course, having a footer that displays the file name is somewhat meaningless if you don\u2019t actually <I>have<\/I> a file name. Therefore, we added this line of code to save the file as C:\\Scripts\\Test.doc:<\/P><PRE class=\"codeSample\">objDoc.SaveAs &#8220;C:\\Scripts\\Test.doc&#8221;\n<\/PRE>\n<P>And that, as they say, is all, folks.<\/P>\n<P>You know, it\u2019s funny: usually when we mention a news item in this column we do so only so we can poke fun at that item. In this case, though, we happen to agree with the news: Seattle drivers <I>are<\/I> pretty polite. But, then again, it\u2019s hard to be a rude and aggressive driver when you\u2019re gridlocked over 8 lanes of traffic, none of which ever seem to move. You can still be rude and aggressive, mind you, but it\u2019s awfully hard to call you a <I>driver<\/I> when you aren\u2019t actually going anywhere.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I\u2019ve seen Word documents where the file name appears in the footer. Except that this file name is dynamic: if the name of the document changes then the footer changes, too. How can I add that kind of a footer using a script?&#8212; ZK Hey, ZK. You know, it\u2019s always tough to [&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-64673","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\u2019ve seen Word documents where the file name appears in the footer. Except that this file name is dynamic: if the name of the document changes then the footer changes, too. How can I add that kind of a footer using a script?&#8212; ZK Hey, ZK. You know, it\u2019s always tough to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64673","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=64673"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64673\/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=64673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}