Using Excel's OnTime method in Outlook

Ruddles

Well-known Member
Joined
Aug 24, 2010
Messages
5,851
Office Version
  1. 365
Platform
  1. Windows
I'd like to monitor file activity in a folder on my hard disk from Outlook to enable me to generate emails depending on that activity. I couldn't see anything in Outlook which corresponds to Excel's OnTime method, so I tried to 'borrow' it from Outlook like this:-
Code:
Sub UseExcelOnTime()
 
  Dim objExcel As Excel.Application
  Set objExcel = CreateObject("Excel.Application")
  
  objExcel.Visible = True
  objExcel.Wait Now() + TimeValue("00:00:05")
  objExcel.OnTime Now() + TimeValue("00:00:05"), "TestSub"
 
End Sub
Sadly this doesn't work. The Wait works okay but the OnTime complains that there's no macro called TestSub in the workbook when I single-step through the code and crashes when I let the code execute at full speed.

Any ideas for scheduling events to run in Outlook? As the mailbox may not receive mail for hours at a time I can't use the NewMail event.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
When you say "monitor file activity in a folder on my hard disk from Outlook" ... what are you doing exactly?
 
Upvote 0
I said 'my hard disk' but it's actually a subdirectory on a server.

There's a process on another machine dropping files into that folder. I need to detect when a file appears, read some data from it and create an email.
 
Upvote 0
Excel's not running. The user has Outlook open all day and I thought that would be a neater solution.

My fall-back position would be to use Application_MAPILogonComplete() to fire up Excel and kick off a series of OnTime events to check the folder every couple of minutes, then use Application_Quit() to close Excel when the user shuts down Outlook.

Also, purely for my own curiosity, I've called Excel functions in Word and Access and used the dictionary from Word in Excel, it just seems that I ought to be able to use OnTime from Outlook.
 
Last edited:
Upvote 0
There's a process on another machine dropping files into that folder. I need to detect when a file appears, read some data from it and create an email.
Have you considered WMI for monitoring the file creation?

http://blogs.technet.com/b/heyscrip...ent-types-of-events-with-just-one-script.aspx

The code shown is VBScript and should work unchanged in VBA. Note though that colMonitoredEvents.NextEvent() needs to be called in a continuous loop, so I'm not sure how you would handle that in Outlook VBA.

Alternatively, you could do the whole thing (file detection, read data, create email) in VBScript and run it as a separate scheduled task.
 
Upvote 0
Thanks for the suggestion - I'll give it a go in the morning. Time to sit back and watch Coronation Street!
 
Upvote 0
Why not run the monitoring code inside a loop in outlook instead of using Ontime ?

Code:
Do
'Code to monitor the folder here
Doevents
Loop
 
Upvote 0
I've cobbled something together whereby when Outlook starts it opens an Excel workbook in the background. The Excel workbook uses OnTime to monitor the folder every few minutes and if it finds a file, it creates a new mail item if Outlook is still running. If Outlook has been closed, the Excel workbook closes itself.

The only issue is that the workbook has to remain active all day for the system to work... we'll just have to see how it goes.
 
Upvote 0

Forum statistics

Threads
1,224,550
Messages
6,179,463
Members
452,915
Latest member
hannnahheileen

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