Automatically set Microsoft Office User Info based on LDAP query

In our network we have had a problem where a lot of our users never configured their Microsoft Office user details and simply accepted the defaults. In our case it was ‘Admin’ due to our SOE not accommodating for it.

Example of the data I am referring to:

The worst part about it is that in our day and age we are using sophisticated search systems that use Author fields, along with advanced features within Office like reviewing that include initials when commenting/editing documents.

My Solution

To resolve this problem I have successfully deployed the below VB script as a start up script which collects the user info via LDAP (Microsoft Active Directory in my case) and updates the word application. The word application is the reference application for this setting (to the other Office applications).

' **********************************************************************
' *** Office User Info Populator
' *** Author: Ivan Dretvic http://ivan.dretvich.com
' *** Date: 05/05/2011
' *** This code was written to update user info within
' *** Office 2003, 2007 and Office 2010 and has been
' *** tested as a log on GPO script on a domain computer in
' *** a Windows 2003 domain environment.
' *** (internet helped complete the code - i did not invent it)
' **********************************************************************
On Error Resume Next
Set objSysInfo = CreateObject("ADSystemInfo")
strUser = objSysInfo.UserName
Set objUser = GetObject("LDAP://" & strUser)
Set objWord = CreateObject("Word.Application")
' There are two options for writing the user name. The first (and active) is a simple combination of GiveName and SN
' the latter is the same, however it capitalises the first character and sets the rest to lowercase. Handy when
' your AD environment does not use capitalisation for those two fields. Simply switch the comment tags around to
' enable the latter. Courtesy of Buggen - Thank you.
objWord.UserName = objUser.givenName & " " & objUser.SN
' objWord.UserName = Ucase(Left(objUser.givenName, 1)) & Lcase(Right(objUser.givenName, Len(objUser.givenName) – 1)) & " " & Ucase(Left(objUser.SN, 1)) & Lcase(Right(objUser.SN, Len(objUser.SN) – 1))
objWord.UserInitials = Left(objUser.givenName, 1) & Left(objUser.SN, 1)
' objWord.UserInitials = Ucase(Left(objUser.givenName, 1)) & Ucase(Left(objUser.SN, 1))
objWord.Quit

Simply paste the above code to a text file and save it as a VB script. From there you can simply double-click on it to test it, and once you are happy and satisfied that it works you can deploy it via a group policy or possibly load it up as a run-once on the default user profile of an SOE to populate the info.

Examples of where the data is used in Office Word:

Comments welcome, and if you find any improvements changes necessary please let me know.

Post implementation review

So far we have deployed it to a group base of 300 machines and it has been very successful. The script performs quickly and has not added much processing time at login (mind you our machines are current equipment). I have made this a login script for all users and it is performed every time they log in. I encountered a problem where clients that didnt have Word installed (for whatever reason) would come up with an error on the user. To combat this i simply addded the first line to continue processing on errors.

Leave a comment ?

10 Comments.

  1. Brilliant, exactly what i needed. I just added UPPER and lowercase string convertions to the script:

    objWord.UserName = Ucase(Left(objUser.givenName, 1)) & Lcase(Right(objUser.givenName, Len(objUser.givenName) – 1)) & ” ” & Ucase(Left(objUser.SN, 1)) & Lcase(Right(objUser.SN, Len(objUser.SN) – 1))

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

    • Hi Buggen,
      Thank you for the addition – i will add it as a variation should others want to use it (and might not be sure where it goes in the script).

      I never did this as our AD has first letter capitalisation for those two fields.

  2. This is great, it’s exactly what I needed. Thanks!

  3. Brilliant. I placed this right in the OCW so it will populate the fields for the user at first launch of Outlook. Nice work.

    • Hi Todd,
      Interesting to include it in the OCW (Office Configuration Wizard). If your clients are not on the network when they first log in the script wont execute. Maybe a combination of OCW and startup GPO would cover more ground.

      Thanks for the comment,
      Ivan

  4. My approach was almost the same.

    cls
    $adobj = “”
    $dom=”LDAP://OU=Users,OU=Corp,DC=CONTOSO,DC=LOCAL”
    $userName = $env:Username

    $root = new-object DirectoryServices.DirectoryEntry $dom
    $strFilter = “(&(objectCategory=User)(samaccountname=$userName))”
    $selector = new-object DirectoryServices.DirectorySearcher
    $selector.filter = $strFilter
    $selector.SearchRoot = $root
    $adobj = $selector.findone()

    $prop = $person.properties
    $initial = $($prop.initials)
    $displayName = $($prop.displayname)
    $sam = $($prop.samaccountname)

    $wshShell = new-object -comObject wscript.shell
    $wshShell.regWrite(“HKCU\Software\Microsoft\Office\Common\UserInfo\UserInitials”, $initial, “REG_SZ”)
    $wshShell.regWrite(“HKCU\Software\Microsoft\Office\Common\UserInfo\UserName”, $displayName, “REG_SZ”)

    • Hi Yann,
      Nice variant of script. Just keep in mind that the registry entry may differ depending on OS/Office architecture variant.
      Thanks for posting it!

      I just removed your personal LDAP info from the script just in case you forgot.
      Ivan

  5. I have a batch version as well:

    REM Get User's Display name
    for /f "tokens=2*" %%a in ('net user "%Username%" /domain ^| find /i "Full Name"') do set DisplayName=%NAME%
    REM Set User's display as current user in office
    reg add "HKCU\Software\Microsoft\Office\Common\UserInfo" /v "UserName" /t REG_SZ /d "%NAME%" /f

Leave a Reply

QR Code Business Card
%d bloggers like this: