2

我做了很多研究,但我一无所获。反正已经这样做了吗?有什么例子吗?

我想使用表单中的按钮发送消息,因此它应该调用 vba 代码并使用相同表单中的一些信息来发送消息。任何想法都会很棒!

我正在使用 Access 2010

4

2 回答 2

9

Twilio 布道者在这里。

简短的回答是您需要使用 VBA 发布到 Twilio REST API。

下面是一些示例代码,展示了如何使用 XMLHTTP 执行此操作:

Public Function GET_MESGS()
    Dim Message As String
    Dim Number As String

    On Error GoTo Error_Handler

    Const NOINTERNETAVAILABLE = -2147012889

    Dim objSvrHTTP As XMLHTTP
    Dim varProjectID, varCatID, strT As String

    Set objSvrHTTP = New XMLHTTP
    objSvrHTTP.Open "POST", "https://api.twilio.com/2010-04-01/Accounts/[YOUR_ACCOUNT_SID]/SMS/", False, "[YOUR_ACCOUNT_SID]", "[YOUR_AUTH_TOKEN]"

    objSvrHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    objSvrHTTP.send "message=" & Message & "&from=15555555555&to=" & Number

    debug.print objSvrHTTP.responseText

    ‘If objSvrHTTP.status = 201 Then
    ‘txtXML = objSvrHTTP.responseText
    ‘MsgBox "Sent"
    ElseIf objSvrHTTP.status = 400 Then
        MsgBox "Failed with error# " & _
            objSvrHTTP.status & _
            " " & objSvrHTTP.statusText & vbCrLf & vbCrLf
    ElseIf objSvrHTTP.status = 401 Then
        MsgBox "Failed with error# " & objSvrHTTP.status & _
            " " & objSvrHTTP.statusText & vbCrLf & vbCrLf
    Else
        MsgBox "Failed with error# " & objSvrHTTP.status & _
            " " & objSvrHTTP.statusText
    End If

Exit_Procedure:

    On Error Resume Next

    Set objSvrHTTP = Nothing

Exit Function

Error_Handler:

    Select Case Err.Number

        Case NOINTERNETAVAILABLE
            MsgBox "Connection to the internet cannot be made or " & _
                "Twilio website address is wrong"

        Case Else
            MsgBox "Error: " & Err.Number & "; Description: " & Err.Description

            Resume Exit_Procedure

        Resume

    End Select

End Function

德文

于 2013-06-19T20:41:32.797 回答
1

这是一个 VBA 示例,可以很容易地适应 Access。

Sub SendSMS()

'credit to Coding is Fun
'https://www.youtube.com/channel/UCZjRcM1ukeciMZ7_fvzsezQ
'https://www.youtube.com/watch?v=v49tYBmnnFg&list=FLDXOykjNf5zoOlCFtYYyPng&index=3

'Authentication
ACCOUNTSID = ActiveSheet.Range("C4").Value
AUTHTOKEN = ActiveSheet.Range("C5").Value

'Variables
fromNumber = "%2B" & ActiveSheet.Range("C6").Value   'use as variable or something else to call number DO NOT ADD leading +
toNumber = ActiveSheet.Range("C8").Value 'use as variable or something else to call number DO NOT ADD leading +
BodyText = ActiveSheet.Range("C9").Value

'Use XML HTTP
Set Request = CreateObject("MSXML2.ServerXMLHTTP.6.0")

'Specify URL
Url = "https://api.twilio.com/2010-04-01/Accounts/" & ACCOUNTSID & "/Messages.json"

'Post URL with authentication
Request.Open "POST", Url, False, ACCOUNTSID, AUTHTOKEN

'Request Header
Request.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"

'Concatenate PostData
postData = "From=" & fromNumber & "&To=%2B" & toNumber & "&Body=" & BodyText

'Send the Post Data
Request.send postData

'[OPTIONAL] Get response text (result)
MsgBox Request.responseText

End Sub
于 2021-05-12T09:55:28.463 回答