从Excel VBA发送短信

时间:2019-03-07 06:33:16

标签: html excel vba post xmlhttprequest

我已经有一个带有VBA的Excel文件。我现在想做的是使用VBA并与已经获得的功能集成来发送SMS。我已经与他们联系并从中获得了示例代码,但是我并不期望他们提供太多帮助,因为他们不知道VBA是。 这是他们的示例代码:

POST /v1/messages HTTP/1.1
Host: api.messagemedia.com
Accept: application/json
Content-Type: application/json
Authorization: Basic dGhpc2lzYWtleTp0aGlzaXNhc2VjcmV0Zm9ybW1iYXNpY2F1dGhyZXN0YXBp
{
"messages": [


{
  "content": "Hello World",
  "destination_number": "+61491570156",
  "format": "SMS"
     }  
   ]
}

当然,我希望能够从VBA内以编程方式更改目标号码和内容。在这一点上,我真的不知道从哪里开始。

1 个答案:

答案 0 :(得分:2)

这是基本示例,应使用指定的参数发出HTTP POST请求:

Sub Test()

    Dim oXHR As Object
    Dim sPayload As String

    sPayload = _
        "{" & vbCrLf & _
        """messages"": [" & vbCrLf & vbCrLf & vbCrLf & _
        "{" & vbCrLf & _
        "  ""content"": ""Hello World""," & vbCrLf & _
        "  ""destination_number"": ""+61491570156""," & vbCrLf & _
        "  ""format"": ""SMS""" & vbCrLf & _
        "     }  " & vbCrLf & _
        "   ]" & vbCrLf & _
        "}"

    Set oXHR = CreateObject("MSXML2.XMLHTTP")
    With oXHR
        .Open "POST", "https://api.messagemedia.com/v1/messages", False
        .SetRequestHeader "Host", "api.messagemedia.com"
        .SetRequestHeader "Accept", "application/json"
        .SetRequestHeader "Content-Type", "application/json"
        .SetRequestHeader "Authorization", "Basic dGhpc2lzYWtleTp0aGlzaXNhc2VjcmV0Zm9ybW1iYXNpY2F1dGhyZXN0YXBp"
        .Send (sPayload)
        Debug.Print .ResponseText
    End With

End Sub