Hi all,
I am a trader (and have zero programming experience of any sort - although I have learnt a lot from this site and others over the past month or so - now I know just enough to be dangerous!!).
Most of my macros are not particularly sophisticated - they simply call other macros, often on a schedule (and this because the base spreadsheet I am using is a sample one provided by the broker, and it contains macros configured to connect, send orders, update prices etc, and I don't want to mess with these).
I have managed to automate most of what I want, but am having trouble with the following code....in that it runs every time it is called (it gets called every 60 seconds to "watch the market").
I lifted the "hasRun" method from somewhere online, as well as the CDO_Mail code. I am only using this code to send an email at the moment - eventually, if I can get it working properly and reliably, the "mail" type of the code will be replaced and it will send the relevant order directly.
Anyway, here is my problem code....(I have removed email addresses).
The mail sends fine. My issue is possibly one of my understanding i.e., that once the code has run once, the variable hasRun sets to "True", and thus when it encounters the If hasRun = True the next time, it should just exit the Sub, but this is obviously not correct, as the email sends every 60 seconds.
I'm thinking the issue may be because this code is called from an outside (within the same workbook) macro which may somehow reset "hasRun" each time it calls it.
Could someone please tell me what I am doing wrong?
Regards
Hendrix
I am a trader (and have zero programming experience of any sort - although I have learnt a lot from this site and others over the past month or so - now I know just enough to be dangerous!!).
Most of my macros are not particularly sophisticated - they simply call other macros, often on a schedule (and this because the base spreadsheet I am using is a sample one provided by the broker, and it contains macros configured to connect, send orders, update prices etc, and I don't want to mess with these).
I have managed to automate most of what I want, but am having trouble with the following code....in that it runs every time it is called (it gets called every 60 seconds to "watch the market").
I lifted the "hasRun" method from somewhere online, as well as the CDO_Mail code. I am only using this code to send an email at the moment - eventually, if I can get it working properly and reliably, the "mail" type of the code will be replaced and it will send the relevant order directly.
Anyway, here is my problem code....(I have removed email addresses).
Code:
Sub SendMailTWShortLevel1()
Dim hasRun As Boolean
Dim CDO_Mail As Object
Dim CDO_Config As Object
Dim SMTP_Config As Variant
Dim strSubject As String
Dim strFrom As String
Dim strTo As String
Dim strCc As String
Dim strBcc As String
Dim strBody As String
strSubject = "TW Short Level 1"
strFrom = "xxxxxxxz@gmail.com"
strTo = "xxxxxxx@yahoo.com"
strCc = ""
strBcc = ""
strBody = "TW Short, move stop to (L1) " & Range("_TWShortLevel1")
On Error GoTo ErrorCatch
If hasRun = True Then[INDENT]Exit Sub[/INDENT]
End If
If hasRun = False And Time > TimeValue("13:30:00") And Range("_TWLevel1Direction") = "BUY" And Range("_TWSecondaryLow").value <= Range("_TWL1") Then
[INDENT]Set CDO_Mail = CreateObject("CDO.Message")[/INDENT]
[INDENT]Set CDO_Config = CreateObject("CDO.Configuration")[/INDENT]
[INDENT]CDO_Config.Load -1[/INDENT]
[INDENT]Set SMTP_Config = CDO_Config.fields[/INDENT]
[INDENT]With SMTP_Config[/INDENT]
[INDENT=2] .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2[/INDENT]
[INDENT=2] .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"[/INDENT]
[INDENT=2] .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1[/INDENT]
[INDENT=2] .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "xxxxxxx@gmail.com"[/INDENT]
[INDENT=2] .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "xxxxxx"[/INDENT]
[INDENT=2] .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25[/INDENT]
[INDENT=2] .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True[/INDENT]
[INDENT=2] .Update[/INDENT]
[INDENT]End With[/INDENT]
[INDENT]With CDO_Mail[/INDENT]
[INDENT=2] Set .Configuration = CDO_Config[/INDENT]
[INDENT] End With[/INDENT]
[INDENT]CDO_Mail.Subject = strSubject[/INDENT]
[INDENT]CDO_Mail.From = strFrom[/INDENT]
[INDENT]CDO_Mail.To = strTo[/INDENT]
[INDENT]CDO_Mail.TextBody = strBody[/INDENT]
[INDENT]CDO_Mail.CC = strCc[/INDENT]
[INDENT]CDO_Mail.BCC = strBcc[/INDENT]
[INDENT]CDO_Mail.send[/INDENT]
End If
hasRun = True
Exit Sub
ErrorCatch:
MsgBox Err.Description
End Sub
The mail sends fine. My issue is possibly one of my understanding i.e., that once the code has run once, the variable hasRun sets to "True", and thus when it encounters the If hasRun = True the next time, it should just exit the Sub, but this is obviously not correct, as the email sends every 60 seconds.
I'm thinking the issue may be because this code is called from an outside (within the same workbook) macro which may somehow reset "hasRun" each time it calls it.
Could someone please tell me what I am doing wrong?
Regards
Hendrix