0

i made a userform in VBA Excel to send SMS via Pushbullet to my clients it works perfectly when i send a single line message , but when i try to send multiple line message it doesn't work,i searched in stackoverflow and google with no answer , i need some help the code i'm using is:

Private Sub smsfac_Click()
Dim lastrow As Integer
'Activate "Microsoft XML, v6.0" under Tools > References
If Me.sms_client.value = "" Or Me.sms_nmbr.value = "" Or Me.sms_txt.value = "" Then
Me.smsfac.ForeColor = vbRed
Me.smsfac.BorderColor = vbRed
Application.Wait (Now + TimeValue("0:00:3"))
Me.smsfac.ForeColor = &HFFFF00
Me.smsfac.BorderColor = &H80000006
Else
Dim item As ListItem
Dim ACCESS_TOKEN As String
Dim TARGET_DEVICE_IDEN As String

'Authentication & Target Device ID
ACCESS_TOKEN = calcule.Range("C7").value
TARGET_DEVICE_IDEN = """" & calcule.Range("C8").value & """"

'Get last row number

'Clear Status
     
    'Get receiver number & text message
    receiver_number = """" & Me.sms_nmbr.Text & """"
    text_message = """" & Me.sms_txt.Text & """"

    'Use XML HTTP
    Set Request = CreateObject("MSXML2.ServerXMLHTTP.6.0")
    
    'Specify Target URL
    URL = "https://api.pushbullet.com/v2/texts"
    
    'Open Post Request
    
    Request.Open "Post", URL, False
    
    'Request Header
    Request.setRequestHeader "Access-Token", ACCESS_TOKEN
    Request.setRequestHeader "Content-Type", "application/json;charset=UTF-8"
    
    'Concatenate PostData
    postData = "{""data"":{""target_device_iden"":" & TARGET_DEVICE_IDEN & ",""addresses"":[" & receiver_number & "],""message"":" & text_message & "}}"
    
    'Send the Post Data
    Request.send postData

End If
End Sub

also if there's away to the a notification that the SMS was sent or not that will help me alot , I appreciate your help

General Grievance
  • 4,555
  • 31
  • 31
  • 45

1 Answers1

0

String values in JSON need quotes - you do not have quotes around text_message

postData = "{""data"":{""target_device_iden"":" & TARGET_DEVICE_IDEN & _
    ",""addresses"":[" & receiver_number & "],""message"":""" & text_message & """}}"

EDIT: See here for how to handle line breaks in message text: Pushbullet pycurl with linefeed in message

postData = "{""data"":{""target_device_iden"":" & TARGET_DEVICE_IDEN & _
    ",""addresses"":[" & receiver_number & "]," & _
     """message"":""" & replace(text_message, vbLf, "\n") & """}}"

If you're still having problems, then try using Debug.Print to output postData and check it looks as you expect.

EDIT2: if the textbox uses vbCrLf as a line break then try

text_message = Replace(Me.sms_txt.Text, vbCrLf, vbLf)
text_message = Replace(text_message , vbLf, "\n")

postData = "{""data"":{""target_device_iden"":" & TARGET_DEVICE_IDEN & _
    ",""addresses"":[" & receiver_number & "]," & _
     """message"":""" & text_message & """}}"

Soory I missed you were adding the quotes when you read the value from the textbox - my bad not reading the whole code and just jumping to postData

Tim Williams
  • 154,628
  • 8
  • 97
  • 125