Skip to main content
The First Step
February 13, 2019
Question

Sending SMS from excel VBA

  • February 13, 2019
  • 13 replies
  • 4677 views
I am trying to create VBA code in excel send an SMS text to a short list of customers.

I have the following code and I can use a GET function and received responses from the API but when I try to POST an SMS I get an Error 400 Bad Request.  Not sure if my formatting is wrong or what?  I would also appreciate input on better VBA code to obtain authorization.  I am currently copying the active authorization code from my Sandbox which is only temporary.

Ultimately I want to execute this in a loop and post the send/read status to a cell in my spreadsheet by the number texted

Thanks in advance for any help.

Sub cmdOAuth2_Click()


  Dim webServiceURL As String
  Dim actionType As String
  Dim targetWord As String
  Dim actionType2 As String
  Dim targetWord2 As String
  Dim sMsg As String
  Dim sToPhone As String
  Dim sFromPHone As String
  
  
  webServiceURL = "https://platform.devtest.ringcentral.com/restapi/v1.0/account/~/extension/~/sms";
  actionType = "Accept"
  targetWord = "application/json"
  actionType2 = "Authorization"
  targetWord2 = "Bearer Code copied from sandbox (Is there a better way to obtain auth within the VBA code"
  sMsg = "test from excel"
  sToPhone = "+17313632593"
  sFromPHone = "+12055178260"
  
' use late binding
  With CreateObject("Microsoft.XMLHTTP")
    .Open "POST", webServiceURL, False
    .SetRequestHeader actionType, targetWord
    .SetRequestHeader actionType2, targetWord2
.Send _
"{" & _
        """from"": [{""phoneNumber"": ""+12055178260""}]," & _
        """to"": {""phoneNumber"": """ & sToPhone & """}," & _
        """text"": """ & sMsg & """" & _
        "}"
        
    If .Status = 200 Then
      Debug.Print .responseText
      MsgBox .GetAllResponseHeaders
    Else
      MsgBox .Status & ": " & .StatusText
    End If
  End With


End Sub


    13 replies

    New Participant
    May 18, 2021

    This should help you get started. It dumps the responses into C:Response.txt after which you can parse the parts you want to keep:

    Dim sFileName As String
    Dim sLogURI As String
    Dim sMessageType As String
    Dim sDateFrom As String
    Dim sDateTo As String
    Dim sDirection As String
    Dim sPageNum As String
    Dim sPerPage As String
    Set httpRequest = CreateObject("MSXML2.ServerXMLHTTP")
    ' Prepare string needed to submit to Ring Central servers for downloading replies from the last 3 days
    sMessageType = "SMS&"
    sDateFrom = Format(Date - 3, "yyyy-mm-dd") & "T" & "00:00:00.000Z&"
    sDateTo = Format(Date + 1, "yyyy-mm-dd") & "T" & Format(DateAdd("h", -7, Now), "hh:mm:ss") & ".000Z&"
    sDirection = "direction=Inbound&conversationId=&"
    sPageNum = "page=1&"
    sPerPage = "perPage=500"
    sLogURI = Trim(RCGetSMSURI) & "?availibility=Alive&messageType=" & sMessageType & "dateFrom=" & sDateFrom & sConversationID & sDirection & sPageNum & sPerPage
    ' Get Access Token to Ring Central server and get replies
    Call http_GetAccessToken
    httpRequest.Open "GET", sLogURI, False
    httpRequest.setRequestHeader "Transfer-Encoding", "chunked"
    httpRequest.setRequestHeader "Authorization", Access_Token
    httpRequest.setRequestHeader "Content-Type", "application/json; charset=UTF-8"
    httpRequest.send
    ' Now dump replies to a text file in the working directory in order to parse the desired pieces from the returned text string
    sFileName = "C:Response.txt"
    ' Dump incoming stream to sFileName
    Open sFileName For Output As #1
    Print #1, Replace(httpRequest.responseText, ",", Chr(13)), Tab
    Close #1

    Hope this help,

    Vick

    The First Step
    May 18, 2021

    Beautiful! Many thanks for a fast and helpful response.

    The First Step
    June 11, 2021

    OK, back to this project. I can now send and receive SMS messages using my VBA code. That's working great. Next, how can I download a file (typically an image) that was attached to an incoming SMS message? I can see information about the attachment in the response when I download the message, including the uri, file type, etc. What do I have to do to download that file? Thanks in advance for any help!