You are here:
ActiveXperts.com > SMS Component > How to Use the ActiveXperts SMS Component > GSM modem send > VBScript
Quicklinks
The ActiveXperts SMS Component is a software development kit (SDK) to enhance an application or script with SMS or Pager functionality. SMS messages can be sent/received using a GSM modem, an SMPP provider or an HTTP compliant SMSC.
In this example we are going to create a Visual Basic Script to send SMS messages. This demo project will ask the user to give a phone number and a message body on the command prompt. A GSM modem is required for this demo.
Download the ActiveXperts SMS Component from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.
Create a new script using your favorite editor. You can simply use notepad. However, a VBScript editor is recommended, so you can browse through objects, objects properties and object functions.
The following code will show you how to declare and create the GSM and SMS objects. We will use the 'objGsm' object to send the message itself. The 'objSmsMessage' object will be used to store information of the message and the 'objSmsConstants' object containes constant values releated to the SMS objects.
Set objGsm = CreateObject ( "AxSms.Gsm" ) Set objSmsMessage = CreateObject ( "AxSms.Message" ) Set objSmsConstants = CreateObject ( "AxSms.Constants" )
The following code will ask the user for the recipient telephone number and the content of the text message. If a PIN code is required, then it will be prompted you to enter one. This data will be stored in the 'objSmsMessage' object. If any error occours while trying to open the connection, the script will end.
' Message: set all properties objSmsMessage.Clear objSmsMessage.ToAddress = ReadInput( "Enter Recipient", "+", False ) objSmsMessage.Body = ReadInput( "Enter text", "Hello, world!", False )
The following code shows how to send an SMS message using the data that was stored in the 'objSmsMessage' and 'objSmsConstants' objects.
' Send the message WScript.Echo "Sending the message..." objGsm.SendSms( objSmsMessage ) WScript.Echo "SendSms, result: " & objGsm.LastError strReference = objSmsMessage.Reference If( objGsm.LastError <> 0 ) Then objGsm.Close WScript.Sleep 3000 WScript.Quit End If
Following you can find the full source code which is also included in the ActiveXperts SMS Component package.
' ********************************************************************
'
' ActiveXperts SMS Component
'
' Send a text SMS message through a connected GSM phone or modem.
' The GSM phone or modem must be connected to your computer.
'
' (c) Copyright ActiveXperts Software - www.activexperts.com
'
' ********************************************************************
Option Explicit
Dim objGsm, objSmsMessage, objSmsConstants, objStatusReport
Dim strPin, strReference, strDevice
Dim bSearchStatusReport
Set objGsm = CreateObject ( "AxSms.Gsm" )
Set objSmsMessage = CreateObject ( "AxSms.Message" )
Set objSmsConstants = CreateObject ( "AxSms.Constants" )
' Display SMS Component Version
WScript.Echo "SMS Component Version " & objGsm.Version & "; " & _
"Build " & objGsm.Build & "; " & _
"Module " & objGsm.Module
' Display if the ActiveXperts SMS Component is registered.
WScript.Echo "License Status: " & objGsm.LicenseStatus & vbCrLf
' Set Logfile
Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
objGsm.LogFile = fso.GetSpecialFolder(2) & "\GSM_sendsms_text.txt"
WScript.Echo "Log file can be found here:"
Wscript.Echo objGsm.LogFile
' Select Device
strDevice = AskDevice ( objGsm )
If (strDevice = "") Then
WScript.Echo "No devices or COM ports where found."
WScript.Echo "Ready."
WScript.Quit
End If
' Open connection to the device
objGsm.Open strDevice
WScript.Echo "Open, result: " & objGsm.LastError & " (" & _
objGsm.GetErrorDescription(objGsm.LastError) & ")"
'36101 means: modem requires pin code, see also: www.activexperts.com/support/errorcodes
If (objGsm.LastError = 36101) Then
strPin = ReadInput( "Enter PIN code (leave blank for no PIN code)", "", False )
objGsm.Open strDevice, strPin
WScript.Echo "Open, result: " & objGsm.LastError & " (" & _
objGsm.GetErrorDescription(objGsm.LastError) & ")"
End If
If (objGsm.LastError <> 0) Then
WScript.Echo "Ready."
WScript.Quit
End If
' Message: set all properties
objSmsMessage.Clear
objSmsMessage.ToAddress = ReadInput( "Enter Recipient", "+", False )
objSmsMessage.Body = ReadInput( "Enter the message text", "Hello, world!", False )
' Use GSM provider's validity period (To be specified in minutes)
objSmsMessage.ValidityPeriod = 0
objSmsMessage.BodyFormat = objSmsConstants.BODYFORMAT_TEXT
If( MsgBox( "Do you want a status report after submitting the message?",
vbYesNo, "Delivery report" ) = vbYes ) Then
objSmsMessage.RequestDeliveryReport = True
bSearchStatusReport = True
Else
objSmsMessage.RequestDeliveryReport = False
bSearchStatusReport = False
End If
' Send the message
WScript.Echo "Sending the message..."
objGsm.SendSms( objSmsMessage )
WScript.Echo "SendSms, result: " & objGsm.LastError & " (" &
objGsm.GetErrorDescription(objGsm.LastError) & ")"
strReference = objSmsMessage.Reference
If( objGsm.LastError <> 0 ) Then
objGsm.Close
WScript.Sleep 3000
WScript.Quit
End If
' Show the Message Reference
WScript.Echo "Message Reference (can be used with status reports): " & strReference
' Wait for status report
Do While bSearchStatusReport
Wscript.Echo "Receiving status report..."
objGsm.Receive objSmsConstants.GSM_MESSAGESTATE_ALL, False,
objSmsConstants.GSM_STORAGETYPE_ALL, 25000
WScript.Echo "Receive, result: " & objGsm.LastError & " (" & _
objGsm.GetErrorDescription(objGsm.LastError) & ")"
If objGsm.LastError <> 0 Then
Exit Do
End If
Set objStatusReport = objGsm.GetFirstReport()
Do While objGsm.LastError = 0
If objStatusReport.Reference = strReference Then
bSearchStatusReport = False
WScript.Echo "Status Report " & objStatusReport.Reference & " received!"
objGsm.DeleteReport objStatusReport
WScript.Echo "DeleteReport, result: " & objGsm.LastError & " (" & _
objGsm.GetErrorDescription(objGsm.LastError) & ")"
Exit Do
Else
Set objStatusReport = objGsm.GetNextReport()
End If
Loop
If( bSearchStatusReport ) Then
WScript.Echo "Not found yet."
WScript.Sleep 3000
End If
Loop
objGsm.Close
WScript.Echo "Ready."
WScript.Sleep 3000
' ***************************************************************************
' Function AskDevice
' ***************************************************************************
Function AskDevice( objDevice )
Dim strMessage, strDefaultDevice, strSelectedDevice, strPort, strDevice
strMessage = "Connected devices: " & vbCrLf
strDevice = objGsm.FindFirstDevice
While objGsm.LastError = 0
strMessage = strMessage & strDevice & vbCrLf
strDevice = objGsm.FindNextDevice()
WEnd
' Add COM ports.
' Gets first COM port.
strPort = objGsm.FindFirstPort()
While (objGsm.LastError = 0)
'Gets next COM port.
strMessage = strMessage & strPort & vbCrLf
strPort = objGsm.FindNextPort()
WEnd
strDefaultDevice = objGsm.FindFirstDevice
If (objGsm.LastError <> 0) Then
strDefaultDevice = objGsm.FindFirstPort()
End If
' If strDefaultDevice has no value it means there was no device or port found.
If (strDefaultDevice <> "") Then
Do
strSelectedDevice = InputBox( strMessage, "Input", strDefaultDevice )
Loop Until strSelectedDevice <> ""
End If
AskDevice = strSelectedDevice
End Function
' ***************************************************************************
' Function ReadInput
' ***************************************************************************
Function ReadInput( ByVal strTitle, ByVal strDefault, ByVal bAllowEmpty )
Dim strInput, strReturn
Do
strInput = inputbox( strTitle, "Enter value", strDefault )
If ( strInput <> "" ) Then
strReturn = strInput
End If
Loop until strReturn <> "" Or bAllowEmpty
ReadInput = strReturn
End Function
You can download the full source code of this project from the ActiveXperts FTP site: ftp.activexperts-labs.com/samples/sms-component. There are many other working samples included with the product or on the FTP site.
The ActiveXperts SMS Component project ships with a set of Microsoft Visual Studio .NET samples. The projects are created with Microsoft Visual Studio 2008.
Users with a later version of Microsoft Visual Studio can open such a project. The Visual Studio Conversion Wizard will guide you through the process of converting the project to the version used.