/build/static/layout/Breadcrumb_cap_w.png

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.

When I use this script it gives an error if Microsoft Office is not present and I am trying to alter it to just exit the script without an error if Office is not present.

Set objSysInfo = CreateObject("ADSystemInfo")

strUser = objSysInfo.UserName
Set objUser = GetObject("LDAP://" & strUser)

Set objWord = CreateObject("Word.Application")
objWord.UserName = objUser.givenName & " " & objUser.SN
objWord.UserInitials = Left(objUser.givenName, 1) & Left(objUser.SN, 1)
objWord.Quit


0 Comments   [ + ] Show comments

Answers (3)

Posted by: anonymous_9363 8 years ago
Red Belt
0
'// Get into the habit of ALWAYS declaring your variables - it saves a lot of heartache
'// if there are coding errors!
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

'// You should check whether or not these properties contain any text
'// before trying to trim them
objWord.UserName = objUser.givenName & " " & objUser.SN
objWord.UserInitials = Left(objUser.givenName, 1) & Left(objUser.SN, 1)
objWord.Quit

'// Always clean up afterwards, too
Set objWord = Nothing
Set objUser = Nothing
Set 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
Posted by: anonymous_9363 8 years ago
Red Belt
0

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
Posted by: anonymous_9363 8 years ago
Red Belt
0

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

Comments:
  • VBScab, I appreciate all the time you have given me. That fixed the error but it is not changing the name in Office. I'll continue to work on this. You have given me a great place to work from. - Robbieb 8 years ago
    • I got it to work! Thank you for all of your help! - Robbieb 8 years ago
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ