Private Sub txtMessage_KeyPress(KeyAscii As Integer)
LimitKeyPress Me.txtMessage, 160, KeyAscii
End Sub
Function SendSMS(fromNumber As String, toNumber As String, body As String)
Dim SmsUrl As String
'Dim CallUrl As String
Dim TwimlUrl As String
Dim postData As String
On Error GoTo Error_Handler
' ******************************************************
'
' To switch to from sending SMS messages to making
' phone calls, comment out the SmsUrl and postData
' variables and uncomment CallUrl, TwimlUrl and
' postData variables below.
SmsUrl = BASEURL & "/2010-04-01/Accounts/" & ACCOUNTSID & "/SMS/Messages"
postData = "From=" & fromNumber _
& "&To=" & toNumber _
& "&Body=" & body
' CallUrl = BASEURL & "/2010-04-01/Accounts/" & ACCOUNTSID & "/Calls"
' TwimlUrl = "http://twimlets.com/" _
' & "message?Message[0]=" _
' & "Due%20to%20inclement%20weather,%20todays%20practice%20has%20been%20canceled."
' postData = "From=" & fromNumber _
' & "&To=" & toNumber _
' & "&Url=" & url
' ******************************************************
' setup the request and authorization
Dim http As MSXML2.XMLHTTP60
Set http = New MSXML2.XMLHTTP60
http.Open "POST", CallUrl, False, ACCOUNTSID, AUTHTOKEN
http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
' send the POST data
http.send postData
' optionally write out the response if you need to check if it worked
Debug.Print http.responseText
If http.Status = 201 Then
ElseIf http.Status = 400 Then
MsgBox "Failed with error# " & _
http.Status & _
" " & http.statusText & vbCrLf & vbCrLf
ElseIf http.Status = 401 Then
MsgBox "Failed with error# " & http.Status & _
" " & http.statusText & vbCrLf & vbCrLf
Else
MsgBox "Failed with error# " & http.Status & _
" " & http.statusText
End If
Exit_Procedure:
On Error Resume Next
' clean up
Set http = 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
Sub LimitKeyPress(ctl As Control, iMaxLen As Integer, KeyAscii As Integer)
On Error GoTo Err_LimitKeyPress
' Purpose: Limit the text in an unbound text box/combo.
' Usage: In the control's KeyPress event procedure:
' Call LimitKeyPress(Me.MyTextBox, 12, KeyAscii)
' Note: Requires LimitChange() in control's Change event also.
If Len(ctl.Text) - ctl.SelLength >= iMaxLen Then
If KeyAscii <> vbKeyBack Then
KeyAscii = 0
Beep
End If
End If
Exit_LimitKeyPress:
Exit Sub
Err_LimitKeyPress:
'Call LogError(Err.Number, Err.Description, "LimitKeyPress()")
Resume Exit_LimitKeyPress
End Sub
Sub LimitChange(ctl As Control, iMaxLen As Integer)
On Error GoTo Err_LimitChange
' Purpose: Limit the text in an unbound text box/combo.
' Usage: In the control's Change event procedure:
' Call LimitChange(Me.MyTextBox, 12)
' Note: Requires LimitKeyPress() in control's KeyPress event also.
If Len(ctl.Text) > iMaxLen Then
MsgBox "Truncated to " & iMaxLen & " characters.", vbExclamation, "Too long"
ctl.Text = Left(ctl.Text, iMaxLen)
ctl.SelStart = iMaxLen
End If
Exit_LimitChange:
Exit Sub
Err_LimitChange:
'Call LogError(Err.Number, Err.Description, "LimitChange()")
Resume Exit_LimitChange
End Sub