【问题标题】:Send SMS from Excel VBA从 Excel VBA 发送短信
【发布时间】:2018-08-01 23:56:31
【问题描述】:

我有我的 Excel 应用程序的代码。这是一个会员忠诚度计划,每次向会员收费时,它都会发送当前的收费金额以及赚取或兑换的积分。

我购买了一个群发短信帐户,他们为我提供了 API。我需要在我的代码中使用这个 API。

以下是我的群发短信提供商提供的 API。

https://malert.in/api/api_http.php?username=user&password=pwd&senderid=myid&to=9000000000&text=Hello%20world&route=Enterprise&type=text&datetime=2018-02-22%2012%3A54%3A22

【问题讨论】:

  • 我想你只是想使用ActiveWorkbook.FollowHyperlink Address:="http://example.microsoft.com" 只需使用你想使用的任何东西设置Address 字符串 - 所以,像ActiveWorkbook.FollowHyperlink Address:="https://malert.in/api/api_http.php?username=user&password=pwd&senderid=myid&to=9000000000& text=Hello%20world&route=Enterprise&type=text&datetime=2018-02-22%2012%3A54%3A22"
  • 你也可以这样做吗?如果不想弹出窗口Sub WebReq() link = "https://malert.in/api/api_http.php?username=user&password=pwd&senderid=myid&to=9000000000& text=Hello%20world&route=Enterprise&type=text&datetime=2018-02-22%2012%3A54%3A22" Set htm = CreateObject("htmlFile") Dim objHttp Set objHttp = CreateObject("Msxml2.ServerXMLHTTP") objHttp.Open "GET", link, False objHttp.Send Debug.Print objHttp.responsetext Set objHttp = Nothing End Sub
  • 发送短信收费吗?您没有电子邮件帐户吗?
  • Jeep 电子邮件的一个问题是您必须了解他们的运营商 - 我想可能会发现,但这是个问题。

标签: excel vba


【解决方案1】:

假设您的 SMS 使用 https 获取请求提供,那么这里是示例。

Sub Test_SMS()

'  //this should work with if winhttp.dll existing in system32 dir. 
'  Dim HttpReq  As New WinHttpRequest


Dim response As String
Dim sURL As String

' //another way to create the HttpReq
Set HttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")

' // build your string
sURL = "https://malert.in/api/api_http.php?username=user&password=pwd&senderid=myid&to=9000000000& text=Hello%20world&route=Enterprise&type=text&datetime=2018-02-22%2012%3A54%3A22"

On Error Resume Next

With HttpReq
.Open "GET", sURL, False
.Send
End With

response = HttpReq.responseText
HttpReq.WaitForResponse
Debug.Print response

End Sub

【讨论】:

  • 添加哪个引用
  • 不明白你的问题,这段代码使用的是windows/system32中的winhttp.dll。
  • 什么都没有发生,代码没有错误但没有收到短信。
  • 请先用ie/chrome测试url,把你的url放到chrome里,看看短信是否收到。另一种方法是使用带有 chrome 的邮递员来测试这个 url..
  • 代码只是使用https get方法发送url,所以你需要先测试你的url是否正确,尤其是token(用户名/密码).....
【解决方案2】:

好的,我已经弄清楚了,它正在工作。下面是代码

Sub send_SMS(xyz As Integer)

    Application.ScreenUpdating = False
'   Declaring varibles for sending sms

    Dim HttpReq  As New WinHttpRequest
    Dim response As String
    Dim sURL As String
    Dim smsto, smstext As String
'   Declaring varibles for Application

    Dim lastrow, lastrow1, lastrow2, x, pointe As Long
    lastrow = Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
    lastrow1 = Sheets(2).Range("A" & Rows.Count).End(xlUp).Row
    lastrow2 = Sheets(3).Range("A" & Rows.Count).End(xlUp).Row

'   Caculation of red card points

    If xyz = 1 Then
        pointe = (frmmain.txtpointe.Value - frmmain.txtpointr.Value) + (frmmain.txtamount.Value * 10 / 100)
        smstext = "Dear Member, You have reedemed " & frmmain.txtpointr.Text & " red points and your balance is " & pointe & " points"
    Else
        pointe = frmmain.txtpointe.Value + (frmmain.txtamount.Value * 10 / 100)
        If pointe >= 1000 Then

            smstext = "Dear Member, You have reached " & pointe & " red points and you can reedem it your next visit"
        Else

            smstext = "Dear Member, Your bill amount is " & frmmain.txtinvoice.Text & " and your Red Point balance is " & pointe & " Points"
        End If
    End If
'  Checking for valid mobile number

    If Len(frmmain.lblmobile.Caption) < 10 Then


      Call nomobile(pointe)

    Else
        smsto = CStr(frmmain.lblmobile.Caption)

        ' //another way to create the HttpReq
        Set HttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")

        ' // API for sending sms
        sURL = "https://malert.in/api/api_http.php?username=username&password=password&senderid=REDHCP&to=" & smsto & "&text=" & smstext & "&route=Enterprise&type=text"
    '    Debug.Print sURL
        On Error Resume Next

        With HttpReq
        .Open "GET", sURL, False
        .send
        End With

        response = HttpReq.responseText
        HttpReq.waitForResponse
'        MsgBox Left(response, 2)
        Debug.Print response

            If Left(response, 2) = "OK" Then
                Call nomobile(pointe)
            Else
               Call errorconnection(smstext, pointe)
            End If
    End If
    sURL = "https://malert.in/api/api_http_balance.php?username=username&password=password&route=Enterprise"
    '    Debug.Print sURL
        On Error Resume Next

        With HttpReq
        .Open "GET", sURL, False
        .send
        End With

        response = HttpReq.responseText
        HttpReq.waitForResponse
        frmmain.lblstatus.Caption = response
        Debug.Print response
        Application.ScreenUpdating = True
End Sub

【讨论】:

    猜你喜欢
    • 2019-07-28
    • 1970-01-01
    • 2014-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-27
    相关资源
    最近更新 更多