Outlook 2016 call to Stop Timer Event

Status
Not open for further replies.

finod

Member
Outlook version
Outlook 2016 32 bit
Email Account
POP3
I have VBA code in Access that sends email with attachments to about 500 veterans. My ISP has a limit of 100 email being sent at one time. My code creates all the email and then uses MyMail.Save which puts all the email into the Outlook 2016 Draft Folder.

The code below starts a timer event that runs every 25 minutes. It sends the first 98 emails and then waits until 25 minutes pass and sends the second batch of 98. It continues to run until all the emails in the Draft folder are sent.

All this works fine. When the last email batch is sent, I must manually STOP the timer by calling Stop Timer.


To fully automate my process, I would like for the STOP Timer to be called when there are no more Drafts to send. My problem is I am not sure the best way to code for the this to occur. Any suggesting will be appreciated.

Code:
'********* Stop Timer **************

Private Sub Application_Quit()

  If TimerID <> 0 Then Call DeactivateTimer 'Turn off timer upon quitting **VERY IMPORTANT**
 
End Sub

'************ Start Timer **********

Sub StartTimer()
'First call macro to initially send fist round of drafts
SendDrafts

MsgBox "Activating the Timer."
  Call ActivateTimer(25) 'Set timer to go off every 25 minutes
 
End Sub

'*******************************  Module 1

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 'The SetTimer call accepts milliseconds, so convert to minutes
  If TimerID <> 0 Then Call DeactivateTimer 'Check to see if timer is running before call to SetTimer
  TimerID = SetTimer(0, 0, nMinutes, AddressOf TriggerTimer)
  If TimerID = 0 Then
    MsgBox "The timer failed to activate."
  End If
End Sub

Public Sub DeactivateTimer()
Dim lSuccess As Long
  lSuccess = KillTimer(0, TimerID)
  If lSuccess = 0 Then
    MsgBox "The timer failed to deactivate."
  Else
    TimerID = 0
  End If
End Sub

Public Sub TriggerTimer(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idevent As Long, ByVal Systime As Long)
    'keeps calling every X Minutes unless deactivated
    If idevent = TimerID Then
        SendDrafts
        'MsgBox "The TriggerTimer function has been automatically called!"
    End If
End Sub


'*******************************  Module 2

Public Sub SendDrafts()
Dim olApp As Outlook.Application
Dim NS As Outlook.NameSpace
Dim DraftsFolder As Outlook.MAPIFolder
Dim Drafts As Outlook.Items
Dim DraftItem As Outlook.MailItem
Dim lDraftCount As Long

Set olApp = Outlook.Application
Set NS = olApp.GetNamespace("MAPI")

Set DraftsFolder = NS.GetDefaultFolder(olFolderDrafts)
Set Drafts = DraftsFolder.Items

' *****************************************************************************

'Loop through all Draft Items
lstNumber = Drafts.Count - 97
  If lstNumber < 1 Then
    lstNumber = 1
  End If
  For lDraftCount = Drafts.Count To lstNumber Step -1
    Set DraftItem = Drafts.Item(lDraftCount)
   'Send Item
    DraftItem.DeleteAfterSubmit = True 'Don't save a copy of sent email
    DraftItem.Send
  Next lDraftCount
 
' ********************************************************************************
'Clean-up
Set DraftsFolder = Nothing
Set NS = Nothing
Set olApp = Nothing

End Sub
 

finod

Member
Outlook version
Outlook 2016 32 bit
Email Account
POP3
If you know you´re going to send 500, then use a varible to count how many times Send has been called.
Thank you for your reply Michael. But the number to send varies so that will not work. However, I was thinking maybe I could use something like an IF statement:

'Loop through all Draft Items
lstNumber = Drafts.Count - 97
If lstNumber < 1 Then
lstNumber = 1
End If
For lDraftCount = Drafts.Count To lstNumber Step -1
Set DraftItem = Drafts.Item(lDraftCount)
'Send Item
DraftItem.DeleteAfterSubmit = True 'Don't save a copy of sent email
DraftItem.Send
If Draft > 0 then
Next lDraftCount else
DeactivateTimer

I am new to this VBA stuff so I am not sure this is possible.
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
After the Next line, read drafts.count and if it´s 0, stop the timer.
 

finod

Member
Outlook version
Outlook 2016 32 bit
Email Account
POP3
After the Next line, read drafts.count and if it´s 0, stop the timer.
Thanks again for your help. This is the code I came up with. It compiled and I will try it later to see if it works.

Code:
'Loop through all Draft Items
lstNumber = Drafts.Count - 97
  If lstNumber < 1 Then
    lstNumber = 1
  End If
  For lDraftCount = Drafts.Count To lstNumber Step -1
    Set DraftItem = Drafts.Item(lDraftCount)
   'Send Item
    DraftItem.DeleteAfterSubmit = True 'Don't save a copy of sent email
    DraftItem.Send
  Next lDraftCount
 
' Check Drats count for no more recores to process then stop timer.
  If Drafts.Count = 0 Then
        MsgBox "Timer deactivated."
    DeactivateTimer
  End If
 
Status
Not open for further replies.
Thread starter Similar threads Forum Replies Date
M Office 2016 Outlook is forgetting passwords Using Outlook 0
E Can one still buy Outlook (or Office) 2016? Using Outlook 6
I Error saving screenshots in a custom form in outlook 2016, outlook 365 - ok in outlook 2013, outlook 2010 Outlook VBA and Custom Forms 5
R seperate read layout to design in outlook 2016..Help!! Outlook VBA and Custom Forms 3
GregS Why is Outlook 2016 mail in Outlook.com? Using Outlook 1
G Recurring tasks break links Outlook 2016 Using Outlook 5
A How to get body of all emails in outlook 2016 to view in blue color Using Outlook 1
J Edit auto-complete list in Outlook 2016+/365? Using Outlook 0
D iCloud Add-in not working in Outlook 2013 and Outlook 2016 After Windows Upgrade & iCloud Upgrade Using Outlook 2
GregS Outlook 2016 iPhone won't download Outlook 2016 Mail. Using Outlook 0
E How to display "Change Folder" in Change Default Email Delivery Location in Exchange Outlook 2016 Using Outlook 1
A How to open a specific link automatically with outlook 2016 Outlook VBA and Custom Forms 5
V Outlook 2016 will not move emails in search results Using Outlook 4
K Using Outlook 2016 to draw Using Outlook 1
M Outlook 2016 Free/Busy settings ignored Using Outlook 0
K Outlook 2016 - controlling IMAP OST size with Group Policy not working Using Outlook 1
C Outlook 2016/2019 hangs after being open for an extended period Using Outlook 4
GregS 2016 Contact List being managed by Outlook.com? Using Outlook 1
A Outlook 2016 in Cached Mode Using Outlook 1
G Outlook 2016 sync contacts directly between phone and computer using outlook 2016 Using Outlook 0
N Outlook 2016 Folder Icons Look Using Outlook 0
Marc2019 Outlook 2016 Font Problem Using Outlook 5
B Outlook 2016 Does not Shutdown Correctly Using Outlook 3
GregS Outlook 2016 Outlook 2016 stalls Using Outlook 3
H Outlook 2016 sent over 30 copies of an e-mail with attachments Using Outlook 1
Q Problems with Autodiscover Outlook 2016 Using Outlook 0
D Importing Outlook Categories from another domain (Exchange 2016/Outlook 2016) Using Outlook 4
Q Unable to Sync Quicken reminder with Outlook 2016 64Bit Using Outlook 1
J How to open OST file in Outlook 2019 & 2016 Using Outlook 1
X I have met my waterloo trying to resolve embedded graphics problem with outlook 2007 and now 2016 Using Outlook 1
K Outlook 2016 Using Outlook 0
L What are the risks of opening an Outlook 2016 .pst file in Outlook 2010? Using Outlook 4
David Langer Outlook 2016 (365) How to restore the ability to Re-Map iCloud IMAP Folders Using Outlook 5
W Search Incomplete Outlook 2016 Using Outlook 5
A Outlook 2016 not synching Using Outlook 4
B The Outlook 2016 Profile that isn't always there Using Outlook 2
D Outlook 2016 customization of incoming messages Using Outlook 1
J Command Button to stamp a date and time in a textbox in Outlook 2016 Outlook VBA and Custom Forms 3
J Checkboxes when selected will appear in a textbox in Outlook 2016 Outlook VBA and Custom Forms 1
N Outlook 2016 Address Book lookup Using Outlook 9
D Outlook 2016 automatically increment anniversaries Using Outlook 1
D Sharing outlook.com calendars in Outlook 2016 Using Outlook 1
S Adding new Exchange (2016) rule very slow down Microsoft Outlook Exchange Server Administration 0
M Outlook 2016 Requesting data from server Using Outlook 0
P Outlook 2016 Client and 365 - Groups Folder Using Outlook 3
P Add a contact to the New Task in Outlook 2016 Using Outlook 2
T Outlook 2016 is not receiving emails until I restart it Using Outlook 1
O Outlook 2016 has "Outlook 2015" files Using Outlook 2
I Retention policies on outlook 2016 ribbon Using Outlook 2
M Outlook 2016 bottom reading pane has disappeared Using Outlook 2
Similar threads


















































Top