You are here:ActiveXperts.com > SMS Component > How to Use the ActiveXperts SMS Component > GSM modem send > Visual Basic for Applications
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 for Applications 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 the form displayed in the image below. To create the buttons, textarea's and drop down menu's, click "View", "Toolbars", "Control toolbox".
In this sample, we're creating a form that is able to send an SMS in Microsoft Excel. Sending an SMS with our SMS Component can be done in just a few lines of code. The core of this little system is the Gsm object witch is the first line of code that must be typed down in your code. After that, just add the phone-number, the message, tell the system what port the device is connected to and eventually send the message.
You can configure a lot of extra options. Few of them are used in this sample. More information about those options are to be found in the products manual whitch is shipped with our product. In this sample we are configuring a logfile, the message type, we are checking the status of the SMS (Whether it has been sent or not) and we're displaying the modems connected to the computer.
In this sample we work with forms. It is possible to work with just the fields too. To create an excel file whitch is able to send an SMS, first of all, create a new excel file with the form that is displayed below in it.
A lot of things in this form are optional. Basically you only need the fields mentioned below:
Make sure you don't forget to create a submit button.
The first and most important thing is to create the ActiveXperts objects. To do that means we need to type some code. You can add code to an excel sheet. Click "View", "Toolbars" and then "Control Toolbox". You will see a "Design Mode"-icon (), click it. Double click the submit button you created in your form. The MicroSoft Visual Basic Editor will now be opened. You can type all code here.
The ActiveXperts objects have to be public variables because they are used in different subs. Use the following code for that.
'Declare the object variables Public objGsm As Object Public objSmsMessage As Object Public objSmsConstants As Object
To fill these variables with the ActiveXperts Software, we're creating a function so in any situation we're sure the object is loaded properly. Use the following code:
Dim objGsm, objMessage, objConstants Set objGsm = CreateObject("AxSms.Gsm") Set objMessage = CreateObject("AxSms.Message") Set objSmsConstants = CreateObject("AxSms.Constants")
In the sample we created a seperate sub for sending the SMS. If the submit button is clicked fist a check is preformed. If the phone number is correct the SMS will be sent. The objects we've created work with the ActiveXperts SMS Component. Every property of the toolkit/component can used with a dot. Every object has its own properties whitch can be found in the products manual that is shipped with the product. Here is an example of how to call to a property. This script wil print the version of the ActiveXperts SMS Component's version in a messagebox.
Sub createobjects() Dim objGsm as Object Set objGsm = CreateObject("AxSms.Gsm") msgbox(objGsm.Version) End Sub
To send the SMS we need to collect the information entered the user and use it to send the SMS. When you're writing your script make sure the script stops if any command has not executed properly. You are able to check this using the "LastError" property in our toolkit/component. Here is an example what you could make your script look like.
The following code shows how to send an SMS message using the data that was stored in the 'objSmsMessage' and 'objSmsConstants' objects.
Following you can find the full source code which is also included in the ActiveXperts SMS Component package.
Private Sub Workbook_Open() End Sub Private Sub cmdSubmit_Click() ' Get the submitted info Dim strServer, strPort, strSystemID, strPassword, strSystemType, strRecipient, strMessage, numTimeout strServer = Cells(5, 2) strPort = Cells(6, 2) strSystemID = Cells(7, 2) strPassword = Cells(8, 2) strSystemType = Cells(9, 2) strRecipient = Cells(10, 2) strMessage = Cells(11, 2) ' Creating the objects Dim objSmpp, objMessage, objConstants Set objSmpp = CreateObject("AxSms.Smpp") Set objMessage = CreateObject("AxSms.Message") Set objSmsConstants = CreateObject("AxSms.Constants") ' Set some properties objSmpp.Clear numTimeout = 5000 ' Connect objSmpp.Connect strServer, CInt(strPort), numTimeout numLastError = objSmpp.LastError If (objSmpp.LastError = 0) Then objSmpp.Bind objSmsConstants.SMPP_BIND_TRANSCEIVER, strSystemID, strPassword, "", objSmsConstants.SMPP_VERSION_34, 0, 0, "", numTimeout numLastError = objSmpp.LastError If (objSmpp.LastError = 0) Then ' Message: set all properties objMessage.Clear objMessage.ToAddress = strRecipient objMessage.Body = strMessage objMessage.BodyFormat = objSmsConstants.BODYFORMAT_TEXT objSmpp.SubmitSms (objMessage) numLastError = objSmpp.LastError objSmpp.Unbind End If objSmpp.Disconnect End If Cells(17, 2) = CStr(numLastError) & ": " & objSmpp.GetErrorDescription(numLastError) Set objSmpp = Nothing Set objMessage = Nothing Set objConstants = Nothing End Sub
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.