Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I set Word’s default file location to be the user’s home directory?

-- LD

SpacerHey, Scripting Guy! AnswerScript Center

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’d always find ourselves doing things like showing people how to do task 1 (retrieve the user’s home directory), and then showing them how to combine that with task 2 (set the default file location to the home directory). We don’t seem to answer many two-part questions these days, probably because it’s hard enough to get us to do one thing let alone get us to do two things. But we’ll see what we can do, just for old time’s sake.

To begin with, we’re assuming that you want to do this in a logon script. If that’s the case, then it’s very easy to take care of task 1, retrieving the user’s home directory:

On Error Resume Next

Set objSysInfo = CreateObject("ADSystemInfo")

strUser = objSysInfo.UserName
Set objUser = GetObject("LDAP://" & strUser)

Wscript.Echo objUser.homeDirectory

As you can see, we begin by creating an instance of the ADSystemInfo object; that’s 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’s why this script works best as a logon script, which always runs locally.) One of the properties returned from ADSystemInfo is UserName, which just happens to be the distinguished name of the logged-on user. That’s going to be something similar to this:

CN=kenmyer, ou=Accounting, dc=fabrikam, dc=com

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’s user account in Active Directory:

Set objUser = GetObject("LDAP://" & strUser)

All we have to do now is echo the value of the homeDirectory attribute.

We’re beginning to recall why we used to do these two-part questions all the time: they’re easy.

So much for task 1. Now let’s put this together with task 2 - setting the default file location for Microsoft Word - and see if we can wrap this up:

On Error Resume Next

Const wdDocumentsPath = 0

Set objSysInfo = CreateObject("ADSystemInfo")

strUser = objSysInfo.UserName
Set objUser = GetObject("LDAP://" & strUser)

If IsNull(objUser.homeDirectory) OR objUser.homeDirectory = "" Then
Else
    Set objWord = CreateObject("Word.Application")
    Set objOptions = objWord.Options
    objOptions.DefaultFilePath(wdDocumentsPath) = objUser.homeDirectory
    objWord.Quit
End If

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:

Const wdDocumentsPath = 0

We’ll use this constant later on to indicate which file location (the one for the user’s documents) that we want to modify.

Next we retrieve the value of the homeDirectory attribute, and check to see if the value is either Null or empty:

If IsNull(objUser.homeDirectory) OR objUser.homeDirectory = "" Then

Why? Well, if the user doesn’t have a home directory then we probably shouldn’t 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.

But what if the user does have a home directory? In that case, we run these lines of code:

Set objWord = CreateObject("Word.Application")
Set objOptions = objWord.Options
objOptions.DefaultFilePath(wdDocumentsPath) = objUser.homeDirectory
objWord.Quit

As you can see, we create an instance of the Word.Application object, then create an object reference to the Options object. Notice that we don’t set the Visible property of Microsoft Word to True. That’s intentional: we don’t want Word to appear onscreen. Instead, we want all our configuring to take place in the background.

We then assign the user’s home directory to the DefaultFilePath 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’s home directory:

objOptions.DefaultFilePath(wdDocumentsPath) = objUser.homeDirectory

We then call the Quit method to terminate our instance of Microsoft Word and we’re done.

That was fun, wasn’t it? Remind us to do these two-part questions more often.