This may be a lot to ask of anyone to investigate. I have a Macro that I built up over a couple weeks that does exactly what I need. Refreshes my data, Creates an image of a range, Pastes that image into an email that is structured in the VBA and sends it to desired people with appropriate fields. Updates the last run time on the main sheet, Saves the open book, then saves it to a shared location without the macros, Then Closes.
Runs beautifully when I hit play. The issue comes when I allow windows task to run it for me at 1 in the morning. For some reason it is freezing before sending the paste command. I come in and the email is sitting open waiting to be sent without the image pasted!? Sometimes (Most of the time) the email will then send without the image before I have a chance to click/edit and/or close it.
My question is first blanketed without looking at the code - are there known causes of this behavior?
If not, or even if so... feel free to peruse through my code and poke holes in it so that it can be enhanced please.
Thanks,
So to start everything Windows has a task to open the document at 12:59a
Then I have in "ThisWorkbook" the following Private sub to run on time value XX
Private Sub Workbook_Open()
Application.OnTime TimeValue("01:00:00"), "Refresh"
Application.OnTime TimeValue("01:15:00"), "SendEmail"
Application.OnTime TimeValue("01:16:00"), "Save_Quit"
End Sub
Then in Module 1 is the macro list of actions as follows
Sub Refresh()
'Refreshing entire workbook which contains a query to MySQL
Windows("REPORTNAME.xlsm").Activate
ActiveWorkbook.RefreshAll
End Sub
Sub SendEmail()
Dim EmailSubject As String
Dim SendTo As String
Dim EmailBody As String
Dim BccTo As String
Dim ccTo As String
'Disable screen updating so that I dont have to watch it jump around
Application.ScreenUpdating = False
'I call on the sub to get and create an image out of a range in which we desire saving it to clipboard for later paste
CreateImage
EmailSubject = "REPORT update " & Date - 1
SendTo = "WHOM I SEND IT TO"
ccTo = ""
BccTo = ""
'HTML body Blank VB line for paste in which comes later through SendKeys
EmailBody = "Good Morning,<P> The REPORT has been updated for 10/01/2014 through " & Date - 1 & ".<br>" & vbNewLine & _
"Please follow this link to review a copy of the <A HREF=LINK>FRIENDLY NAME</A><BR>" & vbNewLine & _
"" & vbNewLine & _
"<Br><BR><Br>****This Automated Message was ran at " & Now & "****" & vbNewLine & _
"<p style='font-family:calibri;font-size:10;Color:gray'> The information in this e-mail is confidential and may be legally privileged. It is intended solely for the addressee and access to this e-mail by anyone else is unauthorized. If you are not the intended recipient, any disclosure, copying, distribution or any action taken or omitted to be taken in reliance on it, is prohibited and may be unlawful. The information contained herein and attached is and remains the property of COMPANY Scheme in whom, in addition to the aforesaid, copyright vests. If you are not the intended recipient, you are hereby notified to kindly and without any delay confirm that all copies of such information have been destroyed. </p>"
'Time to open outlook and input all fields
Set App = CreateObject("Outlook.Application")
Set Itm = App.CreateItem(0)
With Itm
.Subject = EmailSubject
.SentOnBehalfOfName = "FROMWHO"
.To = SendTo
.Cc = ccTo
.Bcc = BccTo
.HTMLBody = EmailBody
'Display must happen for the paste of Send Keys, unless someone has a better way of pasting image over
.Display
'Wait gives the email time to update fields, then we wait again to allow paste update to happen
Wait
'Special commands are just sending the keys to the active window, in this case new email from outlook
SendSpecialCommands
Wait
.Send
End With
Set App = Nothing
Set Itm = Nothing
Sheets("FRONT PAGE").Select
Application.ScreenUpdating = True
End Sub
Sub CreateImage()
'The information for the email is on a hidden tab, so we unhide, select, copy, paste image, resize, copy (To Clipboard) delete the image and then hide the tab again
Sheets("Email").Visible = True
Sheets("Email").Select
Range("G3:P28").Select
Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture
Range("E1").Select
ActiveSheet.PasteSpecial Format:="Picture (Enhanced Metafile)", Link:=False _
, DisplayAsIcon:=False
Selection.ShapeRange.ScaleWidth 1.1, msoFalse, msoScaleFromTopLeft
Selection.ShapeRange.ScaleHeight 1.1, msoFalse, msoScaleFromTopLeft
Selection.Copy
Selection.Delete
Sheets("Email").Select
ActiveWindow.SelectedSheets.Visible = False
End Sub
Sub SendSpecialCommands()
'Modify to send the set of commands that work for your structured Email or file
SendKeys "{down}"
SendKeys "{down}"
SendKeys "{down}"
SendKeys "{down}"
SendKeys "^v", True
End Sub
Sub Wait()
'I Set a delay of ten seconds, this is what I use for all my delays in hours, minutes and/or seconds
newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 10
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait waitTime
End Sub
Sub Save_Quit()
'Saving this workbook, marking the time it was ran, saving a copy without macros to shared location and then quitting
Application.DisplayAlerts = False
Windows("REPORTNAME.xlsm").Activate
MarkTime
ActiveWorkbook.Save
SaveCopy
Application.Quit
End Sub
Sub MarkTime()
'Note the last run time within the sheet
Sheets("FRONT PAGE").Select
Range("D2").Value = Now
End Sub
Sub SaveCopy()
'Saving a copy of the workbook to a shared location for other to open with Read only prompt
ActiveWorkbook.SaveAs Filename:= _
"FILEPATH\REPORTNAME.xlsx" _
, FileFormat:=51, ReadOnlyRecommended:=True, CreateBackup:=False
End Sub
Bookmarks