{"id":70683,"date":"2005-01-11T19:03:00","date_gmt":"2005-01-11T19:03:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/01\/11\/how-can-i-put-the-user-name-into-the-footer-of-a-microsoft-word-document\/"},"modified":"2005-01-11T19:03:00","modified_gmt":"2005-01-11T19:03:00","slug":"how-can-i-put-the-user-name-into-the-footer-of-a-microsoft-word-document","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-put-the-user-name-into-the-footer-of-a-microsoft-word-document\/","title":{"rendered":"How Can I Put the User Name into the Footer of a Microsoft Word Document?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I put the user name into the footer of a Microsoft Word document?<BR><BR>&#8212; RR<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, RR. That\u2019s an interesting question, and probably the first how-do-I-put-the-user-name-in-the-footer-of-a-Word-document question we\u2019ve ever received. (No, really!) But what the heck; let\u2019s see if we can find an answer for you. Besides, this gives us a chance to shamelessly plug the upcoming <A href=\"http:\/\/null\/technet\/scriptcenter\/webcasts\/default.mspx\"><B>Scripting Week 2<\/B><\/A> webcast series. Among the 10 webcasts scheduled for that week: If You Want Something Done Right, Let Microsoft Office Do It For You, a webcast that will provide an introduction to scripting Microsoft Office applications. If you\u2019re interested in scripting Word, Excel, and\/or PowerPoint, this might be 90 minutes well spent.<\/P>\n<P>Of course, right <I>now<\/I> all you want to do is get the user name into the footer of a Word document. No problem; in fact, we\u2019ll show you two different ways to do this.<\/P>\n<P>As the Microsoft Scripting Guys, we spend most of our time helping system administrators write VBScript scripts. Because of that, let\u2019s first show you a way to do carry out this task from a .vbs file. The following script determines the name of the logged-on user, starts an instance of Word, creates a new document, and then sets the document footer text to the name of the logged-on user:<\/P><PRE class=\"codeSample\">Set objNetwork = CreateObject(&#8220;Wscript.Network&#8221;)\nstrUser = objNetwork.UserName<\/p>\n<p>Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nobjWord.Visible = True\nSet objDoc = objWord.Documents.Add()<\/p>\n<p>objDoc.Sections(1).Footers(1).Range.Text = strUser\nobjDoc.Sections(1).Footers(1).Range.ParagraphFormat.Alignment = 1\n<\/PRE>\n<P>What, you expected big, long, and complicated? Actually, scripting Microsoft Word is pretty easy, as this particular sample demonstrates. In this script, the first two lines of code use the WSH Network object to grab the name of the logged-on user and then store that name in the variable strUser. The next three lines of code create an instance of Microsoft Word, make that instance visible on screen, and then give us a blank document to work with.<\/P>\n<P>At that point, we need only one line of code to set the footer text:<\/P><PRE class=\"codeSample\">objDoc.Sections(1).Footers(1).Range.Text = strUser\n<\/PRE>\n<P>To clarify a bit, footers are tied to document sections; because this document has only a single section, we specify <B>Sections(1)<\/B>. <B>Footers(1)<\/B>, which means we want to add the text to the primary footer in the section, while; <B>Range.Text<\/B> indicates that we want to set the Text property of the footer. And strUser, of course, is the name of the logged-on user. That\u2019s it; that\u2019s all it takes.<\/P>\n<P>Just for the heck of it, though, we did add a final line of code that centers the footer on the page. Hey, why not?<\/P>\n<P>Of course, it\u2019s quite possible that you don\u2019t want to perform this trick from a .vbs file; instead, you\u2019d like to have this code available as a Word macro. In that case, use a macro script similar to this:<\/P><PRE class=\"codeSample\">Sub FooterAdder()<\/p>\n<p>Set objNetwork = CreateObject(&#8220;Wscript.Network&#8221;)\nstrUser = objNetwork.UserName<\/p>\n<p>ThisDocument.Sections(1).Footers(1).Range.Text = strUser\nThisDocument.Sections(1).Footers(1).Range.ParagraphFormat.Alignment = 1\nEnd Sub\n<\/PRE>\n<P>Here we have a subroutine named <B>FooterAdder<\/B> that gets the name of the logged-on user and makes that the document footer. Notice that we don\u2019t need to create an instance of Word; if you\u2019re running a Word macro, we\u2019re gonna assume that Word is already running. In addition, this code doesn\u2019t create a new document; instead, it simply adds the footer to the current document. That\u2019s what <B>ThisDocument<\/B> indicates.<\/P>\n<P>Incidentally, we know there is a lot of interest in scripting Office applications, and &#8211; hopefully &#8211; that interest will only increase after Scripting Week 2. So keep an eye on the Script Center during the month of February. Sometime during that month, we plan to unveil a new section (<B>Office Space<\/B>) devoted to scripting Microsoft Office. In the meantime, let us know what other Office-related questions you might have. We\u2019ll try to answer a few of those between now and the debut of Office Space.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I put the user name into the footer of a Microsoft Word document?&#8212; RR Hey, RR. That\u2019s an interesting question, and probably the first how-do-I-put-the-user-name-in-the-footer-of-a-Word-document question we\u2019ve ever received. (No, really!) But what the heck; let\u2019s see if we can find an answer for you. Besides, this gives us a [&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,395],"class_list":["post-70683","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-word","tag-office","tag-scripting-guy","tag-vbscript","tag-word-application"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I put the user name into the footer of a Microsoft Word document?&#8212; RR Hey, RR. That\u2019s an interesting question, and probably the first how-do-I-put-the-user-name-in-the-footer-of-a-Word-document question we\u2019ve ever received. (No, really!) But what the heck; let\u2019s see if we can find an answer for you. Besides, this gives us a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70683","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=70683"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70683\/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=70683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}