There may be times when you want to generate an email using data from an application such as Excel. This is one simple illustration of how that could be done.In our example we will be using a Workbook with three columns starting at column A row 1. Each row represents one product in our inventory and the three columns contains the following data about each item: Part Number, Name of Part, Number of Items in Inventory. Graphically our Workbook looks like this:
Part | Name | Stock |
4583586 | Fliggalhopper | 452 |
5898547 | Looplonger | 293 |
This particular script works by walking down each cell of column 1 till it finds an empty cell which it assumes is the end of the list of entries. If your file may contain empty cells then you can use the Worksheet's UsedRange.Rows.Count property to find the last row in which an entry is made. Your code would then use a for loop something like this:
rowLast = objSheet.UsedRange.Rows.Count for x = rowStart to rowLast ' do stuff next |
Function GetData() Dim x, strTemp, objExcel, objWB
Set objExcel = Wscript.CreateObject("Excel.Application") Set objWB = objExcel.Workbooks.Open("c:\Acme Inc\Workbooks\Test.xls") Set objSheet = objExcel.ActiveWorkbook.Worksheets(1) objExcel.Visible = True x = 1
do while objExcel.Cells(x, 1).Value <> "" strTemp = strTemp & objExcel.Cells(x, 1).Value & _ Space(10 - Len(objExcel.Cells(x, 1).Value)) strTemp = strTemp & objExcel.Cells(x, 2).Value & _ Space(50 - Len(objExcel.Cells(x, 2).Value)) strTemp = strTemp & objExcel.Cells(x, 3).Value & vbCRLF x = x + 1 loop objExcel.ActiveWorkbook.Saved = True objWB.Close objExcel.Quit
set objWB = Nothing set objExcel = Nothing
GetData = strTemp End Function
' This is our main function. Dim strBody
Set objMessage = CreateObject("CDO.Message") objMessage.Subject = "Inventory report for " & Date objMessage.From = "me@my.com" objMessage.To = "bossman@my.com" strBody = "Part" & Space(6) & "Item" & Space(46) & "Stock" & vbCRLF strBody = strBody & GetData
objMessage.TextBody = strBody objMessage.Send |
The code above will produce an email that looks something like this:
To: bossman@my.com From: me@my.com Subject: Inventory report for 3/19/2005
Part Item Stock 4583586 Fliggalhopper 452 5898547 Looplonger 293 |
This sample sends a simple text email via GMail servers.
It's like any other mail but requires that you set the SMTP Port to 465 and tell CDO to use SSL
Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory. Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network).
Const cdoAnonymous = 0 'Do not authenticate Const cdoBasic = 1 'basic (clear-text) authentication Const cdoNTLM = 2 'NTLM
Set objMessage = CreateObject("CDO.Message") objMessage.Subject = "Example CDO Message" objMessage.From = """Me"" " objMessage.To = "me@my.com" objMessage.TextBody = "This is some sample message text.." & vbCRLF & "It was sent using SMTP authentication and SSL."
'==This section provides the configuration information for the remote SMTP server.
objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Name or IP of Remote SMTP Server objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
'Type of authentication, NONE, Basic (Base64 encoded), NTLM objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic
'Your UserID on the SMTP server objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "You@gmail.com"
'Your password on the SMTP server objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "YourPassword"
'Server port (typically 25) objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
'Use SSL for the connection (False or True) objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server) objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
objMessage.Configuration.Fields.Update
'==End remote SMTP server configuration section==
objMessage.Send |
1 comment:
It's very important to optimize the image size to the least possible weight. Usage of heavy images in newsletter can lead your mailer to land in the junk folder.
HTML email software
Post a Comment