• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 534
  • Last Modified:

Outlook Logon Script - Works fine with usernames less than 10 characters

I am using a .vbs script parsing an Outlook.prf file to create Outlook profiles for first time logons. The script works fine except with usernames more than 9 characters.  

Usernames more than 9 characters get a Windows Script Host Error with the following information listed; Script Path, Line Error, Char, Error: String to long

The .vbs file is listed below. Line 102 gets the error which is the following.

objWord.UserInitials = Ucase(StrInitials)

Thanks in advance for your help in reference to this issue.


Chris B.

Option Explicit
Set WshShell = CreateObject("WScript.Shell")
Set WshNetwork = Wscript.CreateObject("Wscript.Network")
Set fso = CreateObject("Scripting.FileSystemObject")
Set ObjEnv = WshShell.Environment("Process")
Set objShell = CreateObject("Shell.Application")
Set objSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objSysInfo.UserName) 
Const HKEY_CURRENT_USER = &H80000001
Const HKCUfirstRunflag = "HKCU\Software\COMPANYNAME\FirstRunFlag"
Const HKCUprofile = "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\DefaultProfile"
Dim WshShell, WshNetwork, objEnv, fso, objShell, username, key, FirstRunCurrentUser, MSOKey, NoProfile, OfficeInstalled, strUserName, strInitials
Dim strOfficePath, strMachineName, objSysInfo, objUser
Dim objWord, LogonSrv, PRFPath, result, OSnummer, objWMIService, colOperatingSystems, objOperatingSystem
strOfficePath = "Software\Microsoft\Office\11.0\Common\UserInfo" 'Path for office user info
strMachineName = "."
' ============== START OF MAIN SCRIPT ==============
 'Check OS
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strMachineName & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
 result =  objOperatingSystem.Version
OSnummer = Left(result,3)
if OSnummer <> "5.1" then
'Wscript.Echo "This is not XP"  'testing
'Wscript.Echo OSnummer    'testing
end if
TestfirstRunUser  ' Has script been run before?
If FirstRunCurrentUser then 'If not then continue
TestProfile  'Eksisting profile?
 If NoProfile then 'Set up profile if none exists
   SetUser 'Set displayname and Initials to Office
   OutlookSetup 'Setup Outlook profile
 End if
end if
WshShell.RegWrite HKCUfirstrunflag, "1", "REG_DWORD"  ' Mark script as run
' ================================================
' ============== END OF MAIN SCRIPT ==============
' ================================================
' -------- Test if first run for this user?
Sub TestFirstRunUser
  on error resume next 'cannot be read first time
  key = WshShell.RegRead(HKCUfirstRunflag)
  If Err <> 0 Then
    FirstRunCurrentUser = True
    FirstRunCurrentUser = False
  End If
  On Error Goto 0
End Sub
'------------ Test if profile exists?
Sub TestProfile
 on error resume next 'cannot be read first time
 MSOKey = WshShell.RegRead(HKCUprofile)
' determine if a profile has already been setup 
  If MSOKey = "" Then
   'wscript.echo "No Profile" 'Testing
   NoProfile = True
   'wscript.echo "Profile exists" 'Testing
   NoProfile = False
  end if
  On Error Goto 0
End sub
'------------ Setup username and initials to Office
Sub SetUser
' Currently logged in User
strUserName = objUser.displayName 'Get name of current user
StrInitials = objUser.samaccountname 'Get initials
Set objWord = CreateObject("Word.Application")
objWord.UserName = strUserName
objWord.UserInitials = Ucase(StrInitials)
End sub
  ' -------- Outlook setup
Sub OutlookSetup
    WshShell.Run "outlook.exe /importprf \\dc\netlogon\outlook.prf", 1, False
End Sub
Sub Cleanup
 Set WshNetwork = Nothing
 Set objSysInfo = Nothing
 Set WshShell = Nothing
 Set fso = Nothing
 Set ObjEnv = Nothing
 Set objShell = Nothing
 Set objUser = Nothing
 Set objWord = Nothing
 Set objWMIService = Nothing
 Set colOperatingSystems = Nothing
 Set objWord = Nothing 
End Sub

Open in new window

  • 2
1 Solution
David LeeCommented:
Hi, cbarone2004.

The error is caused because the UserInitials property is limited to nine characters.  You can't put in more characters than the field is designed to allow.
cbarone2004Author Commented:
I figured it out and now it works perfectly.

I replaced the following

StrInitials = objUser.samaccountname 'Get initials

&and replace it with this line:

StrInitials = (Left(objUser.givenName,1) & Left(objUser.sn,1))  'Get initials

Do I get point for answering my own question...just kidding.
cbarone2004Author Commented:
II figured it out and now it works perfectly.

I replaced the following

StrInitials = objUser.samaccountname 'Get initials

&and replace it with this line:

StrInitials = (Left(objUser.givenName,1) & Left(objUser.sn,1))  'Get initials

Do I get point for answering my own question...just kidding.

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now