0

我在 VBA Excel 中制作了一个用户表单,通过 Pushbullet 向我的客户发送短信,当我发送单行消息时它工作得很好,但是当我尝试发送多行消息时它不起作用,我在 stackoverflow 和谷歌中搜索没有答案,我需要一些帮助我正在使用的代码是:

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

另外,如果没有发送短信的通知,这将对我有很大帮助,我感谢您的帮助

4

1 回答 1

0

JSON 中的字符串值需要引号 - 您没有引号text_message

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

编辑:请参阅此处了解如何处理消息文本中的换行符: Pushbullet pycurl with linefeed in message

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

如果您仍然遇到问题,请尝试使用 Debug.Print 输出postData并检查它是否符合您的预期。

EDIT2:如果文本框使用 vbCrLf 作为换行符,然后尝试

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 & """}}"

很遗憾,当您从文本框中读取值时,我错过了您添加引号的情况-我没有阅读整个代码而只是跳到postData

于 2021-09-03T15:49:46.877 回答