|
Answer» I'm currently trying to make a tedious task I have to do daily much easier, and I figured I may as WELL attempt to fully automate it. I am currently attempting to automate/schedule an Outlook 2003 Macro I wrote to download all of the files off of specific email messages by creating another macro in the ThisOutlookSession area...
However, I do not know VBA very well.
Does anyone know how I might be able to accomplish this? Thank you in advance for your help.
So far, the only Idea I have had is the following (using a couple of test subroutines):
Private Sub Application_Startup()
test
End Sub
Sub test()
Dim CurrTime As String Dim CurrDay As String
CurrDay = Format("ddmmyy", Date)
CurrTime = DatePart("h", TIME)
If CurrTime = "11" Then testSend End If
'INSERT CODE TO PAUSE MACRO WITHOUT HANGING APPLICATION test
End Sub Sub testSend()
SendMessage "Test Success", "The Macro Appears to Work...For Now", "[emailprotected]"
End Sub
Sub SendMessage(Subj As String, Message As String, Receivers As String)
Dim toSend As MailItem
Set toSend = CreateItem(olMailItem) toSend.BodyFormat = olFormatPlain toSend.Subject = Subj toSend.Body = Message toSend.To = Receivers toSend.Send
SendMessage_exit: Set toSend = Nothing Exit Sub
End Sub
Do you have any ideas as to how I might tell the macro to wait an hour before checking the time again without causing the system to hang?Add a new module (Insert->Module)
Paste this:
Code: [Select]Option Explicit Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerfunc As Long) As Long Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Public TimerID As Long 'Need a timer ID to eventually turn off the timer. If the timer ID <> 0 then the timer is running
Public Sub ActivateTimer(ByVal nMinutes As Long) nMinutes = nMinutes * 1000 * 60 'SetTimer accepts milliseconds- convert to minutes. If TimerID <> 0 Then Call DeactivateTimer 'make sure there isn't already a timer. TimerID = SetTimer(0, 0, nMinutes, AddressOf TriggerTimer) If TimerID = 0 Then MsgBox "Timer failure." End If End Sub
Public Sub DeactivateTimer() Dim lSuccess As Long lSuccess = KillTimer(0, TimerID)
End Sub
Public Sub TriggerTimer(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idevent As Long, ByVal Systime As Long) static lastday as integer if lastday <> Day(Now) And Hour(Now)=11 Then testsend lastday = day(now) End If End Sub
Sub testSend()
SendMessage "Test Success", "The Macro Appears to Work...For Now", "[emailprotected]"
End Sub
Sub SendMessage(Subj As String, Message As String, Receivers As String)
Dim toSend As MailItem
Set toSend = CreateItem(olMailItem) toSend.BodyFormat = olFormatPlain toSend.Subject = Subj toSend.Body = Message toSend.To = Receivers toSend.Send
SendMessage_exit: Set toSend = Nothing End Sub
Now for your Application_Startup and Shutdown events:
Code: [Select]Private Sub Application_Quit() If TimerID <> 0 Then Call DeactivateTimer 'Do NOT skip this step! End Sub
Private Sub Application_Startup() MsgBox "Activating the Timer." Call ActivateTimer(10) 'Set timer for 10 min End Sub
Hope this helps. Basically, what it does is create a windows Timer to fire every 10 minutes , which ends up calling the "TriggerTimer()" function. Here I just plopped in your testsend and sendmessage routines and call testsend from the timer routine, after running a few checks to make sure that the hour is eleven and we haven't already sent it today. (the "static" variable keeps it's value between invocations) Thank you so much!
This works perfectly! Quote from: southpaw63119 on July 16, 2010, 08:59:27 AM Thank you so much!
This works perfectly!
Cool! Glad I could help Sorry, I know this is a little bit of a sidetrack, but it is still relevant to this scenario.
The code works fine, but I created this code:
Code: [Select]Dim Item As Object Dim ns As NameSpace Dim Inbox As MAPIFolder
Set ns = GetNamespace("MAPI") Set Inbox = ns.GetDefaultFolder(olFolderInbox)
For Each Item In Inbox.Items If TypeName(Item) = "MailItem" Then If Item.Subject = "Kill Timer Macro" Then Call DeactivateTimer SendMessage "Macro Process STOPPED", "", Item.SenderEmailAddress Item.UnRead = False Item.Delete Else If Item.Subject = "Timer Running?" Then If TimerID <> 0 Then SendMessage "Is Timer Running?", "Yes, the timer is currently running.", Item.SenderEmailAddress Else SendMessage "Is Timer Running?", "No, the timer is not currently running.", Item.SenderEmailAddress End If Item.UnRead = False Item.Delete Else If Item.Subject = "Reset Timer" Then If TimerID <> 0 Then Call DeactivateTimer Call ActivateTimer(10) SendMessage "Timer Reset", "The timer has been reset to check every 10 minutes", Item.SenderEmailAddress Item.UnRead = False Item.Delete End If End If End If End If Next Item
End Sub as a means of controlling the timer remotely (I have the timer set to kick everything off between midnight and 1 AM).
However, when I send the message to the email account to deactivate the timer and then I send the message to see if the timer is still running, the macro responds that it is still running.
Do you know why this occurs?
Thanks. -southpawWhen you ask wether the timer is running, you are testing wether the TimerID value is 0, but when you deactivate it the TimerID is NEVER set to zero.
I feel like such an idiot.
Thanks again!Would you recommend setting the value to 0, or is there another way I can do it?
(sorry about all of this...)Quote from: southpaw63119 on July 21, 2010, 12:15:23 PMWould you recommend setting the value to 0, or is there another way I can do it?
I'd recommend setting it to zero in the DeActivateTimer() routine (after the call to KillTimer()).
Quote(sorry about all of this...)
Don't be silly! you're here for help, honestly I felt bad that I couldn't help as much before and you were just posting all by your LONESOME (I'm not very familiar with the Outlook object model)
|