March 19th, 2008

Hey, Scripting Guy! How Can I Retrieve the User Name and User Initials From Microsoft PowerPoint?

Hey, Scripting Guy! Question

Hey, Scripting Guy! In PowerPoint user information is stored under the Tools menu. I would like to know if there is a way to extract this data by using a script.

— TV

SpacerHey, Scripting Guy! AnswerScript Center

Hey, TV. You know, you might find this hard to believe, but the Scripting Guy who writes this column hardly ever loses anything; in fact, he’s actually pretty good about putting things away so that they won’t get lost. Unfortunately, though, he often forgets that he put these things away; as a result, he spends an inordinate amount of time searching in the wrong place. For example, he’ll turn the upstairs inside-out searching for something, having forgotten that he moved that thing downstairs a long time ago. (After all, moving the thing downstairs would help ensure that it wouldn’t get lost.). He’ll go through all the boxes in his office looking for a book, a book he specifically took home with him. (And why did he take the book home? So that he’d always know exactly where the thing was.) He took a job a Microsoft assuming he’d find a rewarding career, and yet – well, never mind. Let’s just say that the list goes on and on.

Now, for the most part it’s not a good idea to look for something in the wrong place; typically you’ll have a better chance of finding something if you look in the right place. But while that’s usually true it’s not always true. For example, where do you think you should look to find the user name and user initials for Microsoft PowerPoint? Nope, sorry, “Microsoft PowerPoint” is not the correct answer. So then what is the correct answer? Why, “Microsoft Word,” of course:

Set objWord = CreateObject(“Word.Application”)

Wscript.Echo “Name: ” & objWord.UserName Wscript.Echo “Initials: ” & objWord.UserInitials

objWord.Quit

We have to admit that we didn’t realize this until we sat down to write this column, but it turns out that all the Microsoft Office applications share the same user name and initials; change the user name in one of the Office apps and you’ll end up changing the user name in all the Office apps. The problem is that not all the Office applications offer a programmatic way to change the user name and/or user initials; in fact, several of the Office applications don’t even show you the user name and/or initials. After a quick search through the UI for the Office 2003 applications we came up with the following list:

Application

Show Name

Show Initials

Change Name

Change Initials

Word

Yes

Yes

Yes

Yes

Excel

Yes

No

Yes

No

PowerPoint

Yes

Yes

No

No

Outlook

No

No

No

No

Visio

Yes

Yes

Yes

Yes

InfoPath

No

No

No

No

OneNote

Yes

No

Yes (GUI-only)

No

FrontPage

No

No

No

No

Publisher

No

No

No

No

Crazy, but true.

As you can see, PowerPoint displays both the user name and the user initials in its user interface. (Go to the Tools menu, click Options, then click the General tab.) However, PowerPoint’s object model doesn’t provide a way for you to programmatically retrieve that information. Because of that we don’t use PowerPoint’s object model; we use Microsoft Word’s object model instead.

With that in mind, our script starts off by creating an instance of the Word.Application object. Notice that we don’t set the Visible property of this object to True. Why not? Well, setting the Visible property to True would make our instance of Word appear onscreen. However, there’s really no need to make Word appear onscreen: there won’t be anything to see, and the script’s going to take only a second or two to run anyway. Therefore, we just let everything happen in an invisible window.

Once we have an instance of Word up and running we echo back the values of the UserName and the UserInitials properties:

Wscript.Echo “Name: ” & objWord.UserName
Wscript.Echo “Initials: ” & objWord.UserInitials

At that point we simply call the Quit method to terminate both Word and our script.

And yes, as a matter of fact you can use this same script to retrieve the user name and initials from a copy of Office installed on a remote computer. This modified script returns that information from the computer atl-ws-001:

Set objWord = CreateObject(“Word.Application”, “atl-ws-001”)

Wscript.Echo “Name: ” & objWord.UserName Wscript.Echo “Initials: ” & objWord.UserInitials

objWord.Quit

What’s the difference between this script and the first script we showed you? Not much; all we did was add a second parameter – the name of the remote computer – to our CreateObject call:

Set objWord = CreateObject(“Word.Application”, “atl-ws-001”)

That’s all we have to do; adding the second parameter tells the script to instantiate an instance of Word on a remote computer rather than the local computer. (Needless to say, you must have Word installed on the remote machine for this to work.) We then retrieve the UserName and UserInitials property values from that remote instance of Word. And then, again, we use the Quit method to terminate that remote instance.

And because TV initially asked about performing this task using VBA (Visual Basic for Applications) we should show you this script as well. This script can be run from inside PowerPoint (or any other Office application that supports VBA):

Set objWord = CreateObject(“Word.Application”)

Msgbox “Name: ” & objWord.UserName Msgbox “Initials: ” & objWord.UserInitials

objWord.Quit

It’s pretty much the same script we showed you at the beginning of today’s column; the only difference is that we substituted the MsgBox function for Wscript.Echo. (Why? Because VBA doesn’t support the use of Wscript.Echo.) If you happen to be inside Microsoft Word you could use this simplified version instead:

MsgBox “Name: ” &  Application.UserName
MsgBox “Initials: ” & Application.UserInitials

In this case we don’t need to create an instance of the Word.Application object, because our script is already running inside an instance of the Word.Application object.

While we’re at it, we might as well point out that you can use these two properties to change the user name and initials on a computer. For example, this script sets the UserName to Ken Myer and the UserInitials to KM:

Set objWord = CreateObject(“Word.Application”, “atl-ws-001”)

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

objWord.Quit

For an interesting variation on this script, one that sets the user name and initials to the name of the logged-on user, see one our previous columns on working with Microsoft Office.

Now, what happens if you’re running PowerPoint as a standalone-application; that is, what happens if you don’t actually have Word (or Visio or Excel) installed? Well, to be perfectly honest, in that case you’re probably out of luck. As near as we can tell, user information is stored in the registry; depending on your version of Office (and your upgrade path) that’s going to be in a registry key like this: HKCU\Software\Microsoft\Office\11.0\Common\UserInfo. The only problem is that this information is stored in binary format, and we have no idea how to convert a binary value like 47 00 4d 00 53 00 00 00 into a set of initials. Word is probably your best (and maybe your only) bet.

We hope that helps, TV; if you run into any further problems please let us know. In the meantime, the Scripting Guy who writes this column will probably continue to look for things in the absolute wrong place. For example, earlier today he needed some money, so he looked in his checking account. That was dumb: with a son who needs $84 for AP placement exams, $30 for college housing registration fees, and $135 for senior party signup, well, the checking account is the last place he should expect to find any money.

Author

0 comments

Discussion are closed.