{"id":69403,"date":"2005-07-13T20:48:00","date_gmt":"2005-07-13T20:48:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/07\/13\/how-can-i-change-the-user-information-in-microsoft-office\/"},"modified":"2005-07-13T20:48:00","modified_gmt":"2005-07-13T20:48:00","slug":"how-can-i-change-the-user-information-in-microsoft-office","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-change-the-user-information-in-microsoft-office\/","title":{"rendered":"How Can I Change the User Information in Microsoft Office?"},"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 change the user information in Microsoft Office to match the user\u2019s name as stored in Active Directory?<BR><BR>&#8212; SM<\/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, SM. Back in the days when one of the Scripting Guys worked for a local university, every document in one department of 300+ users appeared to have been created by the same person; that\u2019s because a single help desk technician installed Office on every machine and, lacking any other instructions, simply entered his name as the default user name. Because this technician was allegedly the user on every copy of Office, every Office document showed him as the author. Even after he left the university his legacy lived on; that\u2019s because no one knew of a quick and easy way to change that information. JC, you might be gone, but you were never forgotten.<\/P>\n<P>Oh, if only they had had Scripting Guys back in those days! It\u2019s actually pretty easy to change the user information in Office, and to make it match the user\u2019s name as found in Active Directory to boot. Let\u2019s first show you a generic, hard-coded script for changing user information, and then we\u2019ll talk about how to make that script a bit more dynamic.<\/P>\n<P>We should note that we\u2019re going to use Microsoft Word to change the user information. We could also use Microsoft Excel or Microsoft PowerPoint; using any of these applications will change user information for Microsoft Office as a whole. However, Excel and PowerPoint allow you to change only the user\u2019s name; they don\u2019t allow you to change the user\u2019s initials. That could be a problem: in Microsoft Word (the only place &#8211; we think &#8211; that user initials make themselves known) you could end up with a user named Ken Myer who had the initials JC. With Word we can change both the name and the initials. So we decided to use Word.<\/P>\n<P>And yes, this script does require you to have Word installed on the user\u2019s computer. But if you don\u2019t have Word installed then you probably don\u2019t have Office installed, making this a moot point.<\/P>\n<P>Here\u2019s what our script looks like:<\/P><PRE class=\"codeSample\">Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nobjWord.UserName = &#8220;Ken Myer&#8221;\nobjWord.UserInitials = &#8220;KM&#8221;\nobjWord.Quit\n<\/PRE>\n<P>That\u2019s right: just 4 lines of code. We begin by creating an instance of the <B>Word.Application<\/B> object. You might note that in this script &#8211; unlike most of our Microsoft Office scripts &#8211; we don\u2019t set the <B>Visible<\/B> property to True. That\u2019s because we don\u2019t want to view Word onscreen; we just want to start it, change the user information, then dismiss it. There\u2019s no need for anyone to see that process take place.<\/P>\n<P>After starting Word we use these two lines of code to change the value of the <B>UserName<\/B> and <B>UserInitials<\/B> properties:<\/P><PRE class=\"codeSample\">objWord.UserName = &#8220;Ken Myer&#8221;\nobjWord.UserInitials = &#8220;KM&#8221;\n<\/PRE>\n<P>That\u2019s it: we call the <B>Quit<\/B> method to terminate our instance of Word, and we\u2019re done. The next time you start Word &#8211; or any Office application &#8211; the user name will show up as Ken Myer and the user initials as KM.<\/P>\n<P>This script works great as long as you know &#8211; in advance &#8211; the name of the user logged on to the computer. But what if you don\u2019t know the user name in advance, or what if the computer might have multiple users? How can you determine the user name and then change the Office user information on-the-fly? <\/P>\n<P>Why you simply use <I>this<\/I> script, of course:<\/P><PRE class=\"codeSample\">Set objSysInfo = CreateObject(&#8220;ADSystemInfo&#8221;)<\/p>\n<p>strUser = objSysInfo.UserName\nSet objUser = GetObject(&#8220;LDAP:\/\/&#8221; &amp; strUser)<\/p>\n<p>Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nobjWord.UserName = objUser.givenName &amp; &#8221; &#8221; &amp; objUser.SN\nobjWord.UserInitials = Left(objUser.givenName, 1) &amp; Left(objUser.SN, 1)\nobjWord.Quit\n<\/PRE>\n<P>Don\u2019t worry, we\u2019ll explain what\u2019s going on here. Before we do so, we should note that we\u2019re assuming this script is being run as a logon script; that way it will run each time a user logs on to a computer and will then modify the Office user information accordingly. This particular script can\u2019t be run remotely (at least not easily) simply because the <B>ADSystemInfo<\/B> object can only be created locally.<\/P>\n<P>Of course, that leads to an obvious question: what <I>is<\/I> the ADSystemInfo object and why do we even need it? Well, the ADSystemInfo object can be used to grab some useful Active Directory-related information about the logged-on user, including said user\u2019s Distinguished Name. The Distinguished Name is an Active Directory property that provides a direct path to the user account, a path that looks something like this:<\/P><PRE class=\"codeSample\">CN=kenmyer, OU=Finance, DC=fabrikam, DC=com\n<\/PRE>\n<P>If we know the Distinguished Name for a user account we can bind to that account and retrieve all sorts of cool information, including the values of the <B>givenName<\/B> (first name) and <B>SN<\/B> (surname, or last name) attributes. <\/P>\n<P>So how do we bind to the user account? We just use these two lines of code:<\/P><PRE class=\"codeSample\">strUser = objSysInfo.UserName\nSet objUser = GetObject(&#8220;LDAP:\/\/&#8221; &amp; strUser)\n<\/PRE>\n<P>In the first line we store the value of the <B>UserName<\/B> property in a variable named strUser; the UserName property (as derived from the ADSystemInfo object) just happens to be the user\u2019s Distinguished Name. In line 2 we then combine that value with the <B>LDAP:\/\/<\/B> provider, creating an ADsPath similar to this:<\/P><PRE class=\"codeSample\">LDAP:\/\/CN=kenmyer, OU=Finance, DC=fabrikam, DC=com\n<\/PRE>\n<P>By passing the ADsPath to the <B>GetObject<\/B> method we can connect to the logged-on user\u2019s Active Directory user account. And once we\u2019re there, we\u2019re pretty much home free.<\/P>\n<P>In fact, the rest of the script is just a variation of the hard-coded script we showed you at the beginning of today\u2019s column. We create an instance of the Word.Application object, then use this line of code to configure the user name:<\/P><PRE class=\"codeSample\">objWord.UserName = objUser.givenName &amp; &#8221; &#8221; &amp; objUser.SN\n<\/PRE>\n<P>As you can see, we\u2019re concatenating the givenName, a blank space, and the SN, resulting in a name like <B>Ken Myer<\/B>. That\u2019s pretty easy.<\/P>\n<P>Setting the user initials is just a tiny bit trickier; that\u2019s because Active Directory doesn\u2019t have a property for storing user initials. Therefore we have to extract the initials from the user\u2019s first and last names. To do that we use the <B>Left<\/B> function and this line of code:<\/P><PRE class=\"codeSample\">objWord.UserInitials = Left(objUser.givenName, 1) &amp; Left(objUser.SN, 1)\n<\/PRE>\n<P>To grab the first letter of the user\u2019s first name we use this code:<\/P><PRE class=\"codeSample\">Left(objUser.givenName, 1)\n<\/PRE>\n<P>We simply pass the Left function two parameters: objUser.givenName, which contains the user\u2019s first name, and the value 1. The 1 instructs the function to start at the left-hand side of the string and extract 1 character; if the user\u2019s first name is <B>Ken<\/B> this function will return the letter <B>K<\/B>. We then repeat the process to grab the first letter of the last name, then jam the two letters together to form the user initials.<\/P>\n<P>Run the preceding script as a logon script, and each time a user logs on the user information for Microsoft Office will be set accordingly; in turn, that means your Office documents will show the <I>real<\/I> author, not just the person who installed the software. Sorry, JC, but you know what they say: nothing lasts forever.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I change the user information in Microsoft Office to match the user\u2019s name as stored in Active Directory?&#8212; SM Hey, SM. Back in the days when one of the Scripting Guys worked for a local university, every document in one department of 300+ users appeared to have been created by [&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-69403","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 change the user information in Microsoft Office to match the user\u2019s name as stored in Active Directory?&#8212; SM Hey, SM. Back in the days when one of the Scripting Guys worked for a local university, every document in one department of 300+ users appeared to have been created by [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69403","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=69403"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69403\/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=69403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}