繁体   English   中英

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

[英]Pass variable from one sub to another

如何将subrun的uniqueId和uniqueId传递给Sub DisplayCustomError。 我试图通过DisplayCustomError,但是它给出了“调用Sub时不能使用括号”。

预期结果: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

结束子

Call线路从以下位置更改:

DisplayCustomError(errorMessage)

至:

DisplayCustomError errorMessage 

编辑1:传递多个参数:

首先,您需要重新定义您的Sub

Sub DisplayCustomError(errorMessage As String, uniqueId As Long)

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

DisplayCustomError errorMessage, uniqueId

顺便说一句,您可以使用具有不同网络名称的参数传递它,它将仍然有效。 例如:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM