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

Posted on 2009-02-19
Last Modified: 2012-05-06
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

Question by:cbarone2004
    LVL 76

    Expert Comment

    by:David Lee
    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.

    Author Comment

    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(,1))  'Get initials

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

    Accepted Solution

    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(,1))  'Get initials

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Sometimes Outlook might have problems sending a message. There may be various causes- corrupted PST, AV scanner etc. The message, instead of going to the Sent Items folder, sits in the Outbox indefinitely. To remove it you can use a free tool cal…
    Outlook Free & Paid Tools
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now