VB脚本没有返回任何值

时间:2015-07-15 16:47:23

标签: datetime vbscript

Function NextMonthName(dateval)
    Dim tmp : tmp = DateAdd("m", 1, dateval)
    NextMonthName = MonthName(Month(tmp))
    return NextMonthName
    Wscript.Echo  NextMonthName 
    End Function

Function PrevMonthName(dateval)
    Dim tmp : tmp = DateAdd("m", -1, dateval)
    NextMonthName = MonthName(Month(tmp))
    return NextMonthName 
    End Function

我正在运行上面提到的VB脚本,它没有任何输出就完成了。我希望结果在文本文件中。我也无法在控制台中获得输出。

2 个答案:

答案 0 :(得分:0)

由于VBScript中没有return,所以

Option Explicit

Function NextMonthName(dateval)
    Dim tmp : tmp = DateAdd("m", 1, dateval)
    NextMonthName = MonthName(Month(tmp))
    return NextMonthName
End Function

WScript.Echo NextMonthName(Now)

将失败

...\31436343.vbs(6, 5) Microsoft VBScript runtime error: Variable is undefined: 'return'

(看起来你问的问题基于包含全局“On Error Resume Next”的代码)

删除违规行 -

Option Explicit

Function NextMonthName(dateval)
    Dim tmp : tmp = DateAdd("m", 1, dateval)
    NextMonthName = MonthName(Month(tmp))
End Function

WScript.Echo NextMonthName(Now)

解决了这个问题:

cscript 31436343.vbs
August

答案 1 :(得分:0)

将输出写入文本文件;

Option Explicit
Dim Title,dateval,Message
Title = "The previous Month and The Next Month" 
dateval = Now()
Message = "The previous Month : "& PrevMonthName(dateval) & " "& Year(dateval) & vbCrLf &_
 "The Next Month   :"& NextMonthName(dateval) & " "& Year(dateval)
 MsgBox Message,VbInformation,Title
 WriteLog Message
'*******************************************
Function PrevMonthName(dateval)
    Dim tmp : tmp = DateAdd("m", -1, dateval)
    PrevMonthName = MonthName(Month(tmp))   
End Function
'*******************************************
Function NextMonthName(dateval)
    Dim tmp : tmp = DateAdd("m", 1, dateval)
    NextMonthName = MonthName(Month(tmp))
End Function
'*******************************************
Sub WriteLog(strText)
    Dim fs,ts,LogFile
    Const ForWriting = 2
    LogFile = Left(Wscript.ScriptFullName, InstrRev(Wscript.ScriptFullName, ".")) & "log"
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set ts = fs.OpenTextFile(LogFile,ForWriting,True)
    ts.WriteLine strText
    ts.Close
End Sub
'********************************************