VB脚本不返回任何值


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脚本,它完成了没有任何输出。我想在文本文件的结果。我也无法在控制台中获得输出

由于在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

将输出写入文本文件;

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
'********************************************

相关内容

  • 没有找到相关文章

最新更新