0
votes

Trying to return string from Function results in Object required error. Running this code on Windows 2003. Note: on w2k3, Win32_OperatingSystem does not support the =@ singleton which is why we're using For Each ... / Exit Function

Function GetLocalTime()
    Dim wmi, itm
    Set wmi = GetObject("winmgmts:root\cimv2")
    For Each itm in wmi.ExecQuery("SELECT * FROM Win32_OperatingSystem")
        ' NEXT LINE THROWS 'Object required' error
        GetLocalTime = itm.LocalDateTime
        Exit Function
    Next
End Function

Dim started : Set started = GetLocalTime
WScript.Echo "started " & started
1

1 Answers

2
votes

Figured it out. The error message reported as occurring within the body of the GetLocalTime() function, but the error was actually from the call. Needed to remove Set. Here is corrected code:

Function GetLocalTime()
    Dim wmi, itm
    Set wmi = GetObject("winmgmts:root\cimv2")
    For Each itm in wmi.ExecQuery("SELECT * FROM Win32_OperatingSystem")
        GetLocalTime = itm.LocalDateTime
        Exit Function
    Next
End Function

Dim started : started = GetLocalTime  ' <-- CHANGED LINE
WScript.Echo "started " & started