Code:
' Reads from AD Display Name and initials from First name and Last name.
' Writes this info into the registry for the Office username and initials.
Option Explicit
Const HKCU = &H80000001
Const HKLM = &H80000002
Dim oADSystemInfo, oADsUser, oReg
Dim sDisplayName, sInitials, sOfficeVersion, sRegKey, sPath
Dim aUsername, aInitials, aOfficeVersions
Dim iRC1, iRC2, iRet
aOfficeVersions = Array("12.0","11.0","10.0","9.0","8.0")
On Error Resume Next
' Get full username from AD
Set oADSystemInfo = CreateObject("ADSystemInfo")
' get AD user object
Set oADsUser = GetObject("LDAP://" & oADSystemInfo.UserName)
' get full name of the current user and the account name
sDisplayName = oADsUser.givenName & " " & oADsUser.SN
'sDisplayName = oADsUser.DisplayName
' Removed, JP, 24/08.06 & " (" & oADsUser.SAMAccountName & ")"
' get initials of the current user
sInitials = Left(oADsUser.givenName, 1) & Left(oADsUser.SN, 1)
' Create a byte array of the name and initials
aUsername = ToByteArray(sDisplayName)
aInitials = ToByteArray(sInitials)
' Quit if there was an error converting to a byte array
If UBound(aUsername) = -1 Or UBound(aInitials) = -1 Then WScript.Quit
' Connect to the registry (via WMI)
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
& ".\root\default:StdRegProv")
' quit if any errors have occurred
If Err <> 0 Then WScript.Quit
' find the Office version(s) installed
For Each sOfficeVersion In aOfficeVersions
sRegKey = "SOFTWARE\Microsoft\Office\" & sOfficeVersion _
& "\Common\InstallRoot"
iRet = oReg.GetStringValue (HKLM, sRegKey, "Path", sPath)
If iRet <> 0 Or sPath = "" Then
'WScript.Echo "Checking 64-bit path"
sRegKey = "SOFTWARE\Wow6432Node\Microsoft\Office\" & sOfficeVersion _
& "\Common\InstallRoot"
iRet = oReg.GetStringValue (HKLM, sRegKey, "Path", sPath)
End If
If sPath <> "" Then
'WScript.Echo "Updating userinfo"
' Found an installed Office version, now update UserName value in HKCU
If CSng(sOfficeVersion) >= 12.0 Then
sRegKey = "SOFTWARE\Microsoft\Office\Common\UserInfo"
' create the key if it doesn't exist
oReg.CreateKey HKCU, sRegKey
' set the UserName and Initials values using string values
iRC1 = oReg.SetStringValue(HKCU, sRegKey, "UserName", sDisplayName)
iRC2 = oReg.SetStringValue(HKCU, sRegKey, "UserInitials", sInitials)
Else
sRegKey = "SOFTWARE\Microsoft\Office\" & sOfficeVersion & "\Common\UserInfo"
' create the key if it doesn't exist
oReg.CreateKey HKCU, sRegKey
' set the UserName and Initials values using binary values
iRC1 = oReg.SetBinaryValue(HKCU, sRegKey, "UserName", aUsername)
iRC2 = oReg.SetBinaryValue(HKCU, sRegKey, "UserInitials", aInitials)
End If
End If
Next
' returns an empty array for error (the length of an empty array is -1)
Function ToByteArray(ByVal sString)
Dim iIndex, iPos
' return an empty array if a sString is not a string
If VarType(sString) <> vbString Then
ToByteArray = Array()
Exit Function
End If
ReDim aBytes(Len(sString) * 2 + 1)
iIndex = -1
For iPos = 1 To Len(sString)
iIndex = iIndex + 1
aBytes(iIndex) = Asc(Mid(sString, iPos, 1))
' add a 0 after each letter
iIndex = iIndex + 1
aBytes(iIndex) = 0
Next
' add two closing 0's
iIndex = iIndex + 1
aBytes(iIndex) = 0
iIndex = iIndex + 1
aBytes(iIndex) = 0
ToByteArray = aBytes
End Function