Applacation.OnTime runs 3 times in a row

Status
Not open for further replies.

sbdtech

New Member
Joined
Oct 8, 2021
Messages
4
Office Version
  1. 365
I am trying to have this Macro run once a day, First run from a control button runs fine then the next day( and everyday past that) its repeating 3 times every time it runs.

Sub Mail_Daily_Log()

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

'Working in Excel 2000-2016

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 = "forme@atme.com;"

.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("07:30:00"), "Mail_Daily_Log"





End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
72,971
Office Version
  1. 365
Platform
  1. Windows
Duplicate to: application.OnTime issue

In future, please do not post the same question multiple times. Per Forum Rules (#12), posts of a duplicate nature will be locked or deleted.

In relation to your question here, I have closed this thread so please continue in the linked thread. If you do not receive a response, you can "bump" it by replying to it yourself, though we advise you to wait 24 hours before doing so, and not to bump a thread more than once a day.
 
Status
Not open for further replies.
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,439
Messages
5,831,632
Members
430,079
Latest member
lucasabreueng

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
Top