I have a VB script being used for certain OU's that will check for an installation of Office and if it is there it will pull the AD name and insert it as the Office user.
Answers (3)
'// Get into the habit of ALWAYS declaring your variables - it saves a lot of heartache'// if there are coding errors!Option ExplicitDim objSysInfoDim objUserDim objWordOn Error Resume NextSet objSysInfo = CreateObject("ADSystemInfo")
If Not IsObject(objSysInfo) ThenWScript.Quit(False)End IfstrUser = objSysInfo.UserNameSet objUser = GetObject("LDAP://" & strUser)If Not IsObject(objUser) ThenSet objSysInfo = NothingWScript.Quit(False)End IfSet objWord = CreateObject("Word.Application")If Not IsObject(objWord) ThenSet objSysInfo = NothingSet objUser = NothingWScript.Quit(False)End If'// You should check whether or not these properties contain any text'// before trying to trim themobjWord.UserName = objUser.givenName & " " & objUser.SNobjWord.UserInitials = Left(objUser.givenName, 1) & Left(objUser.SN, 1)objWord.Quit'// Always clean up afterwards, tooSet objWord = NothingSet objUser = NothingSet objSysInfo = Nothing
Personally, to avoid the nasty "If Not IsObject..." code repetition, I'd build a function to create the objects and just pass in the name and ProgId, handling any errors in that function but it's a tiny script so maybe save that for a bigger project.
Comments:
-
Thank you so much VBScab. I am new to VB script and did have another version that declared the variables but I certainly have a lot to learn and I appreciate all of your pointers. I've run the script against a pc that doesn't have Office installed and I am getting an error that reads:
Line:35
Char:26
Error:Expected end of statement
Again, I appreciate your help and I'll let you know if I get this figured out. - Robbieb 8 years ago
Ha, ha, ha! Ha, ha, ha! Ha, ha, ha! Ha, ha, ha! Ha, ha, ha! Ha, ha, ha!
I don't think I've *ever* encountered this before...it seems the (let's call it) interpreter is falling over the word "trim" which, as you may or may not know, is a VBScript keyword. The WSH (Windows Scripting Host) version on my machine, 5.8, ignores it, though.
Anyway...remove the comment and the script should run.
Comments:
-
Thanks again VBScab. I cleaned it up so that the script looks like this:
Option Explicit
Dim objSysInfo
Dim objUser
Dim objWord
On Error Resume Next
Set objSysInfo = CreateObject("ADSystemInfo")
If Not IsObject(objSysInfo) Then
WScript.Quit(False)
End If
strUser = objSysInfo.UserName
Set objUser = GetObject("LDAP://" & strUser)
If Not IsObject(objUser) Then
Set objSysInfo = Nothing
WScript.Quit(False)
End If
Set objWord = CreateObject("Word.Application")
If Not IsObject(objWord) Then
Set objSysInfo = Nothing
Set objUser = Nothing
WScript.Quit(False)
End If
objWord.UserName = objUser.givenName & " " & objUser.SN
objWord.UserInitials = Left(objUser.givenName, 1) & Left(objUser.SN, 1)
objWord.Quit
Set objWord = Nothing
Set objUser = NothingSet objSysInfo = Nothing
and I am still getting the error.
It is running on a Windows 7 professional machine with no office products installed. When I run it on my computer which does have Office installed I also get the error:
Line:33
Char:26
Error: Expected end of statement
Source: Microsoft VBScript compilation error
Again, I don't know much about VBScript but is it an issue with "ending" the script? - Robbieb 8 years ago
The very last 2 lines have become concatenated into one. Change this:
Set objUser = NothingSet objSysInfo = Nothing
to this:
Set objUser = Nothing
Set objSysInfo = Nothing