Python: Changing Microsoft Office User Initials

A couple of months ago at work, we received a report that a file was locked. The dialog that appeared showed the initials of a user who wasn't even working for us any more. Thus we discovered an annoying bug that can crop up with Office. Basically, a user is asked by Word or Excel to input their name and initials during the first run of that respective application and it will keep that data no matter who logs into the machine later on. This can lead to some serious confusion when we get error messages of this sort. Anyway, let's take a quick look at how to get this done.

We will be using Python's _winreg module for this hack. You can see said hack below:

from _winreg import *

key = CreateKey(HKEY_CURRENT_USER,
                r'Software\Microsoft\Office\11.0\Common\UserInfo')
res = QueryValueEx(key, "UserInitials")
print repr (res) 

username = u"mldr\0"
SetValueEx(key, "UserInitials", 0, REG_BINARY, username)
CloseKey(key)

Here we use the CreateKey method just in case the key doesn't already exist. If the key does exist, then CreateKey will just open it. The first half of the script was used for checking to see if the key had the correct value in it. The last three lines overwrite the value with my initials. I can't remember why I had to make a unicode string, but the guys on PyWin32 told me that was the way to do it. I can tell you that I wasn't ever able to get a plain string to work. Once the value is set, we clean up after ourselves by closing the key.

That's it! Easy, eh? Have fun with Python!

Copyright © 2024 Mouse Vs Python | Powered by Pythonlibrary