无法从Excel VBA触发电源自动请求触发器

时间:2019-12-09 16:28:09

标签: vba http power-automate

我已经在Power Automate中进行了一个过程,该过程要从VBA启动。该流响应HTTP请求,并使用以下代码在chrome中正常工作:

http = new XMLHttpRequest;
http.open("POST","https://prod-32.westeurope.logic.azure.com:443/workflows/####/triggers/manual/paths/invoke?####");
http.setRequestHeader("Content-Type","application/json");
http.send('{"identity":"test","usage":["asdf"]}');

在chrome中,此代码成功执行。我还测试了这段代码:

  • 在Internet Explorer中,从桌面上的文件网页访问-失败,并显示“访问被拒绝”
  • 在Internet Explorer中,例如通过网站azure.com-成功。

在VBA中,我执行以下操作:

Dim http As Object
Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
Call http.Open("POST", "https://prod-32.westeurope.logic.azure.com:443/workflows/####/triggers/manual/paths/invoke?####", False)
Call http.setRequestHeader("Content-Type", "application/json")
sData = "{""identity"":""test"",""usage"":[""asdf""]}"
Call http.send(sData)

从VBA运行代码时,会有一个延迟尖峰,然后VBA在Power Automate中以“ 操作超时”进行响应,没有证据表明曾经收到过HTTP请求因为该请求不会出现在运行历史记录中。在Microsoft流中使用HTTP请求是否有任何警告?

这也许是因为像IE VBA一样不是“在线”,因此会收到CORS错误吗?不太确定如何调试该问题...有什么想法吗?

1 个答案:

答案 0 :(得分:0)

这还不是一个完整的答案,因为我仍然不确定实际发生的事情,也不是解决此问题的正确方法。但是我确实想到了一项适合我的工作:

Private Sub SendData(ByVal sMethod As String, ByVal sURL As String, ByVal sJSONData As String)
    On Error Resume Next
    Dim ie As Object

    'Launch IE and Navigate to azure
    Set ie = CreateObject("InternetExplorer.Application")
    Call ie.navigate("http://azure.com")

    'Wait for IE to load
    While ie.busy Or ie.readyState <> 4: DoEvents: Wend

    'Define javascript from params
    Dim sJavascript As String
    sJavascript = "javascript:void(0);window.http = new XMLHttpRequest;http.open(""${method}"",""${url}"");http.onreadystatechange=function(){if(http.readyState==4) window.name=""DONE"";};http.setRequestHeader(""Content-Type"",""application/json"");http.send('${data}');"
    sJavascript = Replace(sJavascript, "${method}", sMethod)
    sJavascript = Replace(sJavascript, "${url}", sURL)
    sJavascript = Replace(sJavascript, "${data}", sJSONData)

    'Call the javascript
    Call ie.navigate(sJavascript)

    'Wait to ensure data is sent
    Dim Window As Object
    Set Window = ie.document.parentWindow
    While Window.Name <> "DONE"
        DoEvents
    Wend

    'Read response
    Debug.Print "HTTP Response: " & http.status

    'Quit ie to save RAM
    Call ie.Quit
End Sub

其他用户应该能够修改此设置以适合自己的需求