(Should be easy for all of you) How to stop a application.ontime???

weiser386

New Member
Joined
Sep 2, 2015
Messages
10
DON'T MIND MY SLOPPY CODE I'M JUST WINGING IT! ha

OK so here is my problem...I have a worksheet that displays production data on a TV monitor in the production office. This worksheet is generated by iHistorian tags and i have it set to update every 3 seconds (so we can see when equipment goes down, and the speed of the equipment "real time") I just created a macro that takes a screen shot of the report at the end of every shift (12 hour shift so they end at 7am and 7pm) then it saves the screen shot in a folder so it can be emailed out to everyone at the end of every shift (i still need help automating this screen shot to be embedded in the body of the email but that's another story) so to get to the point i added a application.ontime for this said screen shot and i dont know how to stop it after it takes the screen shot, so it takes a screen shot every 3 seconds....I need help!! thank you in advance!!!

(OH AND HOW DO YOU PUT CODE IN YOUR POST SO IT LOOKS LIKE REGULAR CODE INSTEAD OF PLAIN TEXT???)

Sorry i know this is a lot but you guys are awesome and I'm trying to lear it ALL!!


this is my code written in "thisworkbook"


Private Sub Workbook_Open()

Call MyMacro

End Sub

This is my code in module1 (where my issue is):

Private dTime As Date

Sub MyMacro()

dTime = Now + TimeValue("00:00:03")
Application.OnTime dTime, "MyMacro"

Application.OnTime TimeValue("18:59:00"), "KPI_Screenshot"
Application.OnTime TimeValue("06:59:00"), "KPI_Screenshot"

If ActiveWorkbook.Name <> ThisWorkbook.Name Then Exit Sub

Calculate

Application.DisplayFullScreen = True

End Sub


This is my code for my screenshot to be embedded in an email body its located in module2:

Sub KPI_Screenshot()

Dim OutApp As Object
Dim OutMail As Object


On Error Resume Next

'Prepare the email
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)

On Error Resume Next

With OutMail
.to = "exampleEmail@example.com"
.Subject = "Screen Shot"
.Display

bdy = "<img src='C:\Users\Example\Desktop\KPI Screenshots\test.jpg'>"

'Now add it into body
.HTMLBody = bdy

End With
On Error GoTo 0

OutApp.Session.Logoff
Set OutMail = Nothing
Set OutApp = Nothing

End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
[code ][ /code] tags (without the spaces) - It's the # button when writing posts.

Code:
[COLOR=#333333]dTime = Now + TimeValue("00:00:03")[/COLOR]
That's saying to do something in three seconds.

Code:
[COLOR=#574123]Application.OnTime dTime, "MyMacro"[/COLOR]
In three seconds, run MyMacro.

So you keep on running this routine every three seconds.

Edit:
It probably is still adding the schedule to run your other routine at the specified times.
I don't see any code for taking a screenshot.

Check out https://msdn.microsoft.com/en-us/library/office/Ff196165
 
Last edited:
Upvote 0
First thank you for your help!!

ok so i can see how i could rewrite the "mymacro" code, ill check that out but my issue is the application.ontime that runs the "KPI_screenshot" does not stop running it runs every 3 seconds along with the "mymacro" i only need the "KPI_screenshot code to run on the 2 selected times (18:59 and 06:59) every day and just those two times...so everyday at 6:59pm and at 6:59am it takes a screen shot and saves it to a folder. I'm not worried about the screen shot macro at this time i think i accidently deleted it...im more looking for the answer as to how to stop the .ontime and only run it when i want it to run lol


Here is the "KPI_screenshot" macro:

Code:
Private dTime As Date

Sub MyMacro()

  dTime = Now + TimeValue("00:00:03")
  Application.OnTime dTime, "MyMacro"
  
    Application.OnTime TimeValue("18:59:00"), "KPI_Screenshot"
    Application.OnTime TimeValue("08:07:00"), "KPI_Screenshot"

  If ActiveWorkbook.Name <> ThisWorkbook.Name Then Exit Sub

  Calculate

  Application.DisplayFullScreen = True

End Sub
 
Upvote 0
Every time "MyMacro" runs, it adds another instance of running KPI. If you had KPI set to one minute in the future, and MyMacro ran every 3 seconds, then KPI would run 20x once that future time came about.

From the link I provided

This example cancels the OnTime setting from the previous example.
Code:
[COLOR=#000000][FONT=Consolas]Application.OnTime EarliestTime:=TimeValue("17:00:00"), _ 
[/FONT][/COLOR][COLOR=#000000][FONT=Consolas] Procedure:="my_Procedure", Schedule:=False[/FONT][/COLOR]
 
Upvote 0

Forum statistics

Threads
1,215,436
Messages
6,124,869
Members
449,192
Latest member
MoonDancer

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