application.OnTime issue

sbdtech

New Member
Joined
Oct 8, 2021
Messages
4
Office Version
  1. 365
Hello, I need this to run once a day, it runs multiple times in a row. desired outcome in a nut shell is to pull data from the plc, make a report and email each day at 3:30 am. first run is started by a command button. fist run works fine then the next day it send several emails in a row. need it to only run once. Thank you!

VBA Code:
Sub Mail_Daily_Log()

Application.Run "'POWDER LINE DISPLAY 9_28_21A.xlsm'!readFromPLC"

'Working in Excel 2000-2016

'For Tips see: [URL='http://www.rondebruin.nl/win/winmail/Outlook/tips.htm']Excel Automation - Ron de Bruin[/URL]

Dim FileExtStr As String

Dim FileFormatNum As Long

Dim Sourcewb As Workbook

Dim Destwb As Workbook

Dim TempFilePath As String

Dim TempFileName As String

Dim OutApp As Object

Dim OutMail As Object



With Application

.ScreenUpdating = False

.EnableEvents = False

End With



Set Sourcewb = ActiveWorkbook



'Copy the ActiveSheet to a new workbook

Sheets("Report").Copy

Set Destwb = ActiveWorkbook



'Determine the Excel version and file extension/format

With Destwb

If Val(Application.Version) < 12 Then

'You use Excel 97-2003

FileExtStr = ".xls": FileFormatNum = -4143

Else

'You use Excel 2007-2016

Select Case Sourcewb.FileFormat

Case 51: FileExtStr = ".xlsx": FileFormatNum = 51

Case 52:

If .HasVBProject Then

FileExtStr = ".xlsm": FileFormatNum = 52

Else

FileExtStr = ".xlsx": FileFormatNum = 51

End If

Case 56: FileExtStr = ".xls": FileFormatNum = 56

Case Else: FileExtStr = ".xlsb": FileFormatNum = 50

End Select

End If

End With



' 'Change all cells in the worksheet to values if you want

With Destwb.Sheets(1).UsedRange

.Cells.Copy

.Cells.PasteSpecial xlPasteValues

.Cells(1).Select

End With

Application.CutCopyMode = False



'Save the new workbook/Mail it/Delete it

TempFilePath = Environ$("temp") & "\"

TempFileName = "Part of " & Sourcewb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")



Set OutApp = CreateObject("Outlook.Application")

Set OutMail = OutApp.CreateItem(0)



With Destwb

.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum

On Error Resume Next

With OutMail

.to = "[EMAIL]forme@atme.com[/EMAIL];"

.CC = ""

.BCC = ""

.Subject = "Powder Line Downtime Report"

.Body = "Powder Line Downtime Report is Attached.”

.Attachments.Add Destwb.FullName

'You can add other files also like this

'.Attachments.Add ("C:\test.txt")

.Send 'or use .Display

End With

On Error GoTo 0

.Close savechanges:=False

End With



'Delete the file you have send

Kill TempFilePath & TempFileName & FileExtStr



Set OutMail = Nothing

Set OutApp = Nothing



With Application

.ScreenUpdating = True

.EnableEvents = True

End With

Sheets("DASHBOARD1").Select

Range("A1").Select

Application.OnTime TimeValue("03:30:00"), "Mail_Daily_Log"

End Sub
 
Last edited by a moderator:

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try the following instead...

VBA Code:
    Application.OnTime (Date + 1) + TimeValue("03:30:00"), "Mail_Daily_Log"

Hope this helps!
 
Upvote 0
Unless that line of code is executed more than once, it should only call "Mail_Daily_Log" once. Maybe it might help if you shared all of the relevant code?
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,289
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top