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

Comments

Please log in to comment

Answers

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.

Answered 06/01/2016 by: VBScab
Red Belt

  • 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.
Please log in to comment
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.

Answered 06/01/2016 by: VBScab
Red Belt

  • 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?
Please log in to comment
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
Answered 06/01/2016 by: VBScab
Red Belt

  • 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.
    • I got it to work! Thank you for all of your help!
Please log in to comment
Answer this question or Comment on this question for clarity
Admin Script Editor
Admin Script Editor is an integrated scripting environment available free here at ITNinja

Share