Results 1 to 2 of 2

cdo send send mail - getting my external .vbs script directly into a an excel worksheet

Threaded View

  1. #1
    Registered User
    Join Date
    01-03-2014
    Location
    Belgium
    MS-Off Ver
    Excel 2007
    Posts
    2

    cdo send send mail - getting my external .vbs script directly into a an excel worksheet

    I have an excel worksheet that collects some data from the internet every minute, some calculations are done & if this calculation gives a result = 1 in cell B1 of steet1 then an Email needs to be sent. For the moment I have the following code in the microsoft excel object of sheet1:

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim alarm As Integer
    alarm = Range("B1").Value
    If Not Intersect(Target, Range("F13:F43")) Is Nothing And alarm = 1 Then
    Application.EnableEvents = False
    Shell "Explorer.exe ""C:\SendMailalarm_no_attachment.vbs""", 1
    Application.EnableEvents = True
    End If
    End Sub
    The external SendMailalarm_no_attachment.vbs script that is triggered has code that I based on code from http://www.paulsadowski.com/wsh/cdo.htm and http://www.rondebruin.nl/win/s1/cdo.htm (thanks guys)
    to send an alarm Email, triggerd by an excel cell value, with cdo from a Gmail address with SSL

    EmailSubject = "alarm message"
    EmailBody = "best regards," & vbCRLF & _
    		"me"
    
    Const EmailFrom = "xxxx@gmail.com"
    Const EmailFromName = "xxxx"
    Const EmailTo = "me@xxx.com"
    Const SMTPServer = "smtp.gmail.com"
    Const SMTPLogon = "xxxx@gmail.com"
    Const SMTPPassword = "password"
    Const SMTPSSL = True
    Const SMTPPort = 465
    
    Const cdoSendUsingPickup = 1 	'Send message using local SMTP service pickup directory.
    Const cdoSendUsingPort = 2 	'Send the message using SMTP over TCP/IP networking.
    
    Const cdoAnonymous = 0 	' No authentication
    Const cdoBasic = 1 	' BASIC clear text authentication
    Const cdoNTLM = 2 	' NTLM, Microsoft proprietary authentication
    
    ' First, create the message
    
    Set objMessage = CreateObject("CDO.Message")
    objMessage.Subject = EmailSubject
    objMessage.From = """" & EmailFromName & """ <" & EmailFrom & ">"
    objMessage.To = EmailTo
    objMessage.TextBody = EmailBody
    
    ' Second, configure the server
    
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SMTPServer
    
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic
    
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusername") = SMTPLogon
    
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = SMTPPassword
    
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = SMTPPort
    
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = SMTPSSL
    
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
    
    objMessage.Configuration.Fields.Update
    
    ' Now send the message!
    
    objMessage.Send
    This all works perfectly (WIN 8 - excel 2013) but I now want to put the external SendMailalarm_no_attachment.vbs script directly into the microsoft excel object of sheet1 so that I can get the EmailTo, EmailSubject and EmailBody from a cell in my sheet1 of my excel workbook.
    I thought I could just coppy the code of SendMailalarm_no_attachment.vbs to where I now have the line Shell "Explorer.exe ""C:\SendMailalarm_no_attachment.vbs""", 1
    (see below) but the code below does not work .... it would have been too easy I guess...
    Can anyone tell me what I'm doing wrong in the code below?

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim alarm As Integer
    alarm = Range("B1").Value
    If Not Intersect(Target, Range("F13:F43")) Is Nothing And alarm = 1 Then
    Application.EnableEvents = False
    EmailSubject = "alarm message"
    EmailBody = "best regards," & vbCRLF & _
    		"me"
    
    Const EmailFrom = "xxxx@gmail.com"
    Const EmailFromName = "xxxx"
    Const EmailTo = "me@xxx.com"
    Const SMTPServer = "smtp.gmail.com"
    Const SMTPLogon = "xxxx@gmail.com"
    Const SMTPPassword = "password"
    Const SMTPSSL = True
    Const SMTPPort = 465
    
    Const cdoSendUsingPickup = 1 	'Send message using local SMTP service pickup directory.
    Const cdoSendUsingPort = 2 	'Send the message using SMTP over TCP/IP networking.
    
    Const cdoAnonymous = 0 	' No authentication
    Const cdoBasic = 1 	' BASIC clear text authentication
    Const cdoNTLM = 2 	' NTLM, Microsoft proprietary authentication
    
    ' First, create the message
    
    Set objMessage = CreateObject("CDO.Message")
    objMessage.Subject = EmailSubject
    objMessage.From = """" & EmailFromName & """ <" & EmailFrom & ">"
    objMessage.To = EmailTo
    objMessage.TextBody = EmailBody
    
    ' Second, configure the server
    
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SMTPServer
    
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic
    
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusername") = SMTPLogon
    
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = SMTPPassword
    
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = SMTPPort
    
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = SMTPSSL
    
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
    
    objMessage.Configuration.Fields.Update
    
    ' Now send the message!
    
    objMessage.Send
    
    Application.EnableEvents = True
    End If
    End Sub
    Last edited by PieterBlan; 05-09-2014 at 05:09 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 03-11-2014, 12:24 PM
  2. Automatic send email (without even click send in mail software) with excel vba ??
    By alexnkc in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-17-2013, 08:31 PM
  3. [SOLVED] How do I send a xl-sheet as a mail using a VBA-script?
    By SOJ in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-01-2005, 05:20 PM
  4. Replies: 1
    Last Post: 11-09-2005, 05:00 PM
  5. [SOLVED] Send Mail Script Help
    By JBL in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-22-2005, 06:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1