Hey guys, i need some help automating a report. It needs to run 24/7 and perform macros every hour. I need the first line in the Starttimer sub to work properly. So if the current date and time is 10/23/2015 08:13:00, the code would generate the following
RunWhen= (2015,10,23)+(8,0,0)+(1,0,0) = 10/23/2015 09:00:00
Public RunWhen As Double
Public Const cRunInterivalHours = 1
Public Const cRunWhat = "ReportRefresh"
Public Const cRunSend = "EmailFilePDF"
Public Const cRunSave = "SaveFilePDF"
Sub Starttimer()
RunWhen = DateSerial(Now, Now, Now) + TimeSerial(Now, 0, 0) + TimeSerial(cRunInterivalHours, 0, 0)
Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, Schedule:=True
End Sub
Sub ReportRefresh()
Worksheets("Report 2").Range("C5").Value = Now()
ActiveWorkbook.Save
If RunWhen = TimeSerial(8, 0, 0) Then
Application.OnTime EarliestTime:=RunSe, Procedure:=cRunSave, Schedule:=True
End If
If RunWhen = TimeSerial(6, 0, 0) Then
Application.OnTime EarliestTime:=RunSe, Procedure:=cRunSend, Schedule:=True
End If
Starttimer
End Sub
Sub SaveFilePDF()
'change c;\users for local comp
ChDir "H:\PIC_SHAR\Production Report"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"H:\PIC_SHAR\Production Report\Daily Plant Technical Report " & Format(Date - 1, "mmmm dd yyyy") & ".PDF"
'Change directory to specific folder once detials confirmed
Starttimer
End Sub
Sub EmailFilePDF()
Dim OutLookApp As Object
Dim OutLookMailItem As Object
Dim myAttachments As Object
Set OutLookApp = CreateObject("Outlook.application")
Set OutLookMailItem = OutLookApp.CreateItem(0)
Set myAttachments = OutLookMailItem.Attachments
With OutLookMailItem
.To = ""
'.To = ""
'.CC = ""
.Subject = "Daily Technical Report " & Format(Date - 1, "mmmm dd yyyy")
.Body = "Automated PDF Daily Technical Report, Inventory Calculations need verification"
myAttachments.Add "H:\PIC_SHAR\Production Report\Daily Plant Technical Report " & Format(Date - 1, "mmmm dd yyyy") & ".PDF"
.send
End With
Set OutLookMailItem = Nothing
Set OutLookApp = Nothing
Starttimer
End Sub
Bookmarks