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

无法从Excel VBA触发电源自动请求触发器,vba,http,power-automate,Vba,Http,Power Automate,我已经在Power Automation中创建了一个流程,我想从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-Ty

我已经在Power Automation中创建了一个流程,我想从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 Automation中,没有证据表明收到过HTTP请求,因为该请求未出现在运行历史记录中。在Microsoft flow中使用HTTP请求是否有任何注意事项


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

这不是一个完整的答案,因为我仍然不确定到底发生了什么,也不确定解决这个问题的正确方法。然而,我确实想到了一个适合我的工作:

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
Private Sub SendData(ByVal sMethod作为字符串,ByVal sURL作为字符串,ByVal sJSONData作为字符串)
出错时继续下一步
模糊的物体
'启动IE并导航到azure
设置ie=CreateObject(“InternetExplorer.Application”)
调用ie.navigate(“http://azure.com")
“等我上船
当ie.busy或ie.readyState 4:DoEvents:Wend时
'从参数定义javascript
将脚本设置为字符串
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)
'调用javascript
调用ie.navigate(sJavascript)
'等待以确保发送数据
将窗口变暗为对象
设置窗口=ie.document.parentWindow
While Window.Name“DONE”
多芬特
温德
“阅读回答
调试。打印“HTTP响应:&HTTP.status”
“为了救拉姆而辞职
打电话给我,退出
端接头
其他用户应该能够修改此项以满足自己的需要