{"id":68813,"date":"2005-10-05T08:24:00","date_gmt":"2005-10-05T08:24:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/10\/05\/how-can-i-set-words-default-file-location-to-be-the-users-home-directory\/"},"modified":"2005-10-05T08:24:00","modified_gmt":"2005-10-05T08:24:00","slug":"how-can-i-set-words-default-file-location-to-be-the-users-home-directory","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-set-words-default-file-location-to-be-the-users-home-directory\/","title":{"rendered":"How Can I Set Word\u2019s Default File Location to be the User\u2019s Home Directory?"},"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 set Word\u2019s default file location to be the user\u2019s home directory?<BR><BR>&#8212; LD<\/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, LD. Ah, memories. There was a period of time when it seemed like every question we answered in this column was a two-part question: we\u2019d always find ourselves doing things like showing people how to do task 1 (retrieve the user\u2019s home directory), and then showing them how to combine that with task 2 (set the default file location to the home directory). We don\u2019t seem to answer many two-part questions these days, probably because it\u2019s hard enough to get us to do <I>one<\/I> thing let alone get us to do two things. But we\u2019ll see what we can do, just for old time\u2019s sake.<\/P>\n<P>To begin with, we\u2019re assuming that you want to do this in a logon script. If that\u2019s the case, then it\u2019s very easy to take care of task 1, retrieving the user\u2019s home directory:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Set objSysInfo = CreateObject(&#8220;ADSystemInfo&#8221;)<\/p>\n<p>strUser = objSysInfo.UserName\nSet objUser = GetObject(&#8220;LDAP:\/\/&#8221; &amp; strUser)<\/p>\n<p>Wscript.Echo objUser.homeDirectory\n<\/PRE>\n<P>As you can see, we begin by creating an instance of the <B>ADSystemInfo<\/B> object; that\u2019s an ADSI object that can return a number of properties about the logged-on user. (Unfortunately, though, the ADSystemInfo object can be created only on the local machine; that\u2019s why this script works best as a logon script, which always runs locally.) One of the properties returned from ADSystemInfo is <B>UserName<\/B>, which just happens to be the distinguished name of the logged-on user. That\u2019s going to be something similar to this:<\/P><PRE class=\"codeSample\">CN=kenmyer, ou=Accounting, dc=fabrikam, dc=com\n<\/PRE>\n<P>Is it worth our while to grab the distinguished name and stash it in a variable named strUser? You bet it is; by using the distinguished name along with the LDAP provider we can bind directly to the user\u2019s user account in Active Directory:<\/P><PRE class=\"codeSample\">Set objUser = GetObject(&#8220;LDAP:\/\/&#8221; &amp; strUser)\n<\/PRE>\n<P>All we have to do now is echo the value of the <B>homeDirectory<\/B> attribute. <\/P>\n<P>We\u2019re beginning to recall why we used to do these two-part questions all the time: they\u2019re <I>easy<\/I>. <\/P>\n<P>So much for task 1. Now let\u2019s put this together with task 2 &#8211; setting the default file location for Microsoft Word &#8211; and see if we can wrap this up:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Const wdDocumentsPath = 0<\/p>\n<p>Set objSysInfo = CreateObject(&#8220;ADSystemInfo&#8221;)<\/p>\n<p>strUser = objSysInfo.UserName\nSet objUser = GetObject(&#8220;LDAP:\/\/&#8221; &amp; strUser)<\/p>\n<p>If IsNull(objUser.homeDirectory) OR objUser.homeDirectory = &#8220;&#8221; Then\nElse\n    Set objWord = CreateObject(&#8220;Word.Application&#8221;)\n    Set objOptions = objWord.Options\n    objOptions.DefaultFilePath(wdDocumentsPath) = objUser.homeDirectory\n    objWord.Quit\nEnd If\n<\/PRE>\n<P>As you can see, the first few lines of code are pretty much the same ones we used to bind to the user account in Active Directory. The only addition is this line, which defines a constant named wdDocumentsPath:<\/P><PRE class=\"codeSample\">Const wdDocumentsPath = 0\n<\/PRE>\n<P>We\u2019ll use this constant later on to indicate which file location (the one for the user\u2019s documents) that we want to modify.<\/P>\n<P>Next we retrieve the value of the homeDirectory attribute, and check to see if the value is either Null or empty:<\/P><PRE class=\"codeSample\">If IsNull(objUser.homeDirectory) OR objUser.homeDirectory = &#8220;&#8221; Then\n<\/PRE>\n<P>Why? Well, if the user doesn\u2019t <I>have<\/I> a home directory then we probably shouldn\u2019t try setting the default file location to that non-existent folder. If the user does not have a home directory then we do nothing all; notice that an Else statement immediately follows the If-Then statement.<\/P>\n<P>But what if the user <I>does<\/I> have a home directory? In that case, we run these lines of code:<\/P><PRE class=\"codeSample\">Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nSet objOptions = objWord.Options\nobjOptions.DefaultFilePath(wdDocumentsPath) = objUser.homeDirectory\nobjWord.Quit\n<\/PRE>\n<P>As you can see, we create an instance of the <B>Word.Application<\/B> object, then create an object reference to the <B>Options<\/B> object. Notice that we don\u2019t set the <B>Visible<\/B> property of Microsoft Word to True. That\u2019s intentional: we don\u2019t <I>want<\/I> Word to appear onscreen. Instead, we want all our configuring to take place in the background.<\/P>\n<P>We then assign the user\u2019s home directory to the <B>DefaultFilePath<\/B> property. Because DefaultFilePath can refer to multiple file paths (startup path, templates path, pictures path, etc.) we pass along the constant wdDocumentsPath; that tells Word to configure the default documents path. As you might have guessed, we set the value of the default documents path to the user\u2019s home directory:<\/P><PRE class=\"codeSample\">objOptions.DefaultFilePath(wdDocumentsPath) = objUser.homeDirectory\n<\/PRE>\n<P>We then call the <B>Quit<\/B> method to terminate our instance of Microsoft Word and we\u2019re done.<\/P>\n<P>That <I>was<\/I> fun, wasn\u2019t it? Remind us to do these two-part questions more often.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I set Word\u2019s default file location to be the user\u2019s home directory?&#8212; LD Hey, LD. Ah, memories. There was a period of time when it seemed like every question we answered in this column was a two-part question: we\u2019d always find ourselves doing things like showing people how to do [&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":[7,84,49,3,20,5],"class_list":["post-68813","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-microsoft-word","tag-office","tag-scripting-guy","tag-user-accounts","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I set Word\u2019s default file location to be the user\u2019s home directory?&#8212; LD Hey, LD. Ah, memories. There was a period of time when it seemed like every question we answered in this column was a two-part question: we\u2019d always find ourselves doing things like showing people how to do [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68813","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=68813"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68813\/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=68813"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68813"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68813"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}