将变量从一个子传递到另一个子

时间:2017-08-07 11:04:04

标签: vba excel-vba vbscript adsutil.vbs excel

如何将subId和uniqueId从子运行传递给Sub DisplayCustomError。我试图通过DisplayCustomError,但它给出了"在调用Sub&#34时不能使用括号。

预期结果:uniqueId和uniqueId应该转到Sub DisplayCustomError来创建一个json对象。

sub run
    On Error Resume Next
    wrapper.getVariable( "IRR" ).value = excel.range( "'Cases'!$H$783" )
    Dim uniqueId , uniqueId , errorMessage
    If Err.Number <> 0 And excel.range( "'Cases'!$H$783" ) = "" Then
     errorCode = "MC2006"
     uniqueId = "12"                 
     errorMessage= "Error while executing EVMLite.           
     DisplayCustomError(errorMessage)
     On Error Goto 0         
     Call Err.Raise(vbObjectError + 10, "EVM Failed to execute. ", errorMessage)  
    End If      
end sub

Sub DisplayCustomError(errorMessage)
If Err.Number <> 0 Then
    Dim objHTTP, URL, json, uniqueId, networkInfo, jobId
    Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")    
    URL = "http://10.93.24.223:9005/vpp/logerror"
    objHTTP.Open "POST", URL, False
    objHTTP.SetRequestHeader "Content-Type", "application/json"
    json = "{""jobId"": """& jobId &""", ""uniqueId"": """& uniqueId &""", ""errorCode"": """& errorCode &""", ""errorMessage"": """& errorMessage &"""}"
    objHTTP.send (json)
 End If

结束子

1 个答案:

答案 0 :(得分:2)

更改Call行:

DisplayCustomError(errorMessage)

要:

DisplayCustomError errorMessage 

编辑1:传递多个参数:

首先,您需要重新定义Sub

Sub DisplayCustomError(errorMessage As String, uniqueId As Long)

然后,当您调用它时,请确保传递正确数量和类型的参数:

DisplayCustomError errorMessage, uniqueId

B.T.W 您可以使用不同的名称传递参数,它仍然有效。例如:

DisplayCustomError errorMessage, uniqueId

然后

Sub DisplayCustomError(errMsg As String, uId As Long)

编辑2 完整代码 已编辑(相关部分)

Sub run()

    On Error Resume Next
    wrapper.getVariable("IRR").Value = Excel.Range("'Cases'!$H$783")

    ' modified the line below
    Dim uniqueId As String, errorMessage As String

    If Err.Number <> 0 And Excel.Range("'Cases'!$H$783") = "" Then
        ErrorCode = "MC2006"
        uniqueId = "12"
        errorMessage = "Error while executing EVMLite.           "
        DisplayCustomError errorMessage, uniqueId ' <-- modifed this line
        On Error GoTo 0
        Call Err.Raise(vbObjectError + 10, "EVM Failed to execute. ", errorMessage)
    End If

End Sub

Sub DisplayCustomError(errMsg As String, uID As String) ' <-- modifed this line

If Err.Number <> 0 Then
    Dim objHTTP, URL, json, networkInfo, jobId ' <-- removed uniqueId from this line
    Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
    URL = "http://10.93.24.223:9005/vpp/logerror"
    objHTTP.Open "POST", URL, False
    objHTTP.SetRequestHeader "Content-Type", "application/json"

    ' --- modifed the line below ---
    ' *** WHere do you get the value of jobId and ErrorCode ***
    json = "{""jobId"": """ & jobId & """, ""uniqueId"": """ & uID & """, ""errorCode"": """ & ErrorCode & """, ""errorMessage"": """ & errMsg & """}"
    objHTTP.send (json)
End If

End Sub