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
Bookmarks