How Can I Change the User Information in Microsoft Office?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I change the user information in Microsoft Office to match the user’s name as stored in Active Directory?

— SM

SpacerHey, Scripting Guy! AnswerScript Center

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’s 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’s because no one knew of a quick and easy way to change that information. JC, you might be gone, but you were never forgotten.

Oh, if only they had had Scripting Guys back in those days! It’s actually pretty easy to change the user information in Office, and to make it match the user’s name as found in Active Directory to boot. Let’s first show you a generic, hard-coded script for changing user information, and then we’ll talk about how to make that script a bit more dynamic.

We should note that we’re 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’s name; they don’t allow you to change the user’s initials. That could be a problem: in Microsoft Word (the only place – we think – 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.

And yes, this script does require you to have Word installed on the user’s computer. But if you don’t have Word installed then you probably don’t have Office installed, making this a moot point.

Here’s what our script looks like:

Set objWord = CreateObject(“Word.Application”)
objWord.UserName = “Ken Myer”
objWord.UserInitials = “KM”
objWord.Quit

That’s right: just 4 lines of code. We begin by creating an instance of the Word.Application object. You might note that in this script – unlike most of our Microsoft Office scripts – we don’t set the Visible property to True. That’s because we don’t want to view Word onscreen; we just want to start it, change the user information, then dismiss it. There’s no need for anyone to see that process take place.

After starting Word we use these two lines of code to change the value of the UserName and UserInitials properties:

objWord.UserName = “Ken Myer”
objWord.UserInitials = “KM”

That’s it: we call the Quit method to terminate our instance of Word, and we’re done. The next time you start Word – or any Office application – the user name will show up as Ken Myer and the user initials as KM.

This script works great as long as you know – in advance – the name of the user logged on to the computer. But what if you don’t 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?

Why you simply use this script, of course:

Set objSysInfo = CreateObject(“ADSystemInfo”)

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

Set objWord = CreateObject(“Word.Application”) objWord.UserName = objUser.givenName & ” ” & objUser.SN objWord.UserInitials = Left(objUser.givenName, 1) & Left(objUser.SN, 1) objWord.Quit

Don’t worry, we’ll explain what’s going on here. Before we do so, we should note that we’re 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’t be run remotely (at least not easily) simply because the ADSystemInfo object can only be created locally.

Of course, that leads to an obvious question: what is 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’s 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:

CN=kenmyer, OU=Finance, DC=fabrikam, DC=com

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 givenName (first name) and SN (surname, or last name) attributes.

So how do we bind to the user account? We just use these two lines of code:

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

In the first line we store the value of the UserName property in a variable named strUser; the UserName property (as derived from the ADSystemInfo object) just happens to be the user’s Distinguished Name. In line 2 we then combine that value with the LDAP:// provider, creating an ADsPath similar to this:

LDAP://CN=kenmyer, OU=Finance, DC=fabrikam, DC=com

By passing the ADsPath to the GetObject method we can connect to the logged-on user’s Active Directory user account. And once we’re there, we’re pretty much home free.

In fact, the rest of the script is just a variation of the hard-coded script we showed you at the beginning of today’s column. We create an instance of the Word.Application object, then use this line of code to configure the user name:

objWord.UserName = objUser.givenName & ” ” & objUser.SN

As you can see, we’re concatenating the givenName, a blank space, and the SN, resulting in a name like Ken Myer. That’s pretty easy.

Setting the user initials is just a tiny bit trickier; that’s because Active Directory doesn’t have a property for storing user initials. Therefore we have to extract the initials from the user’s first and last names. To do that we use the Left function and this line of code:

objWord.UserInitials = Left(objUser.givenName, 1) & Left(objUser.SN, 1)

To grab the first letter of the user’s first name we use this code:

Left(objUser.givenName, 1)

We simply pass the Left function two parameters: objUser.givenName, which contains the user’s 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’s first name is Ken this function will return the letter K. 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.

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 real author, not just the person who installed the software. Sorry, JC, but you know what they say: nothing lasts forever.

0 comments

Discussion is closed.

Feedback usabilla icon