Petite macro pour décaler l'envoie d'un e-mail au lendemain (ou prochain lundi) 8h30

Travailler en dehors des heures de travail n'engage que nous mais ne doit pas impacter la vie privée des collègues. Alors quand on envoie des courriels en dehors de heures travaillées, il peut être sympa de programmer l'envoie du courriel au lendemain (par exemple 8h30) ou au lundi si nous sommes en we.

Il existe dans MS Oulook un une option pour cela : "Delay Delivery" mais cela est assez laborieux à utiliser en de manière continuer (une fois ça va, trois fois, y'en a marre).

L'idée est donc de créer un nouveau bouton "Envoyer demain" qui va appeler cette petite macro. La macro va automatiquement programmé l'envoi du courriel au prochain jour ouvré 8h30 et afficher une popup indiquant la programmation du courriel.

Sub DelaySendMail()

    On Error GoTo ErrHand               ' Error Handling
    
    Dim objMailItem As MailItem         ' Object to hold mail item
    Dim SendDate As String              ' The date to send delayed mail
    Dim SendTime As String              ' The time to send delayed mail
    Dim MailIsDelayed As Boolean        ' Set if the mail will be delayed
    Dim NoDeferredDelivery As String    ' Value if deferred delivery is disabled
    Dim time As String                  ' Current time of the day
    
    SendTime = "08:30:00"               ' Time to deliver delayed mail (08:30:00)
    MailIsDelayed = False               ' We assume it's being delivered now
    NoDeferredDelivery = "1/1/4501"     ' Magic number Outlook uses for "delay mail box isn't checked"
    
    Const morningTime As String = "08:30:00"
    
    time = Format(Now, "HH:NN:SS")
    
    'Set object to mail item you have open
    Set objMailItem = Outlook.ActiveInspector.CurrentItem
    
    ' Check and make sure current item is an unsent message
    If objMailItem.Sent = True Then
        Err.Raise 9000
    End If
        
    ' Set the date appropriately for the next weekday
    If Weekday(Date, vbMonday) = 5 Then
        ' Today is Saturday
        ' Delay mail two days
        SendDate = Date + (3)
        MailIsDelayed = True
    ElseIf Weekday(Date, vbMonday) = 6 Then
        ' Today is Saturday
        ' Delay mail two days
        SendDate = Date + (2)
        MailIsDelayed = True
    ElseIf Weekday(Date, vbMonday) = 7 Then
        ' Today is Sunday
        ' Delay mail one day
        SendDate = Date + (1)
        MailIsDelayed = True
    Else

        ' Currently a weekday
        ' See if it's inappropriate to send mail right now
        If (StrComp(time, morningTime) < 0) Then
            ' It's early morning - delay it
            SendDate = Date
            MailIsDelayed = True
        Else
            ' We are during or after working hours, so we delay to tomorrow.
            SendDate = Date + (1)
            MailIsDelayed = True
        End If
        
    End If
    
    If MailIsDelayed Then
        ' Mail should be delayed - set the delivery date/time
        objMailItem.DeferredDeliveryTime = SendDate & " " & SendTime
        objMailItem.Send
        MsgBox "Your mail will be delivered at " & _
                SendDate & " " & SendTime, _
                vbOKOnly, "Mail delayed"

    End If

    Exit Sub

ErrHand:
    ' Handle well-known errors with message
    ' Other errors, just tell the user
    If Err.Number = 13 Then
        ' No current item or current item isn't a mail message
        MsgBox "Future delivery can only be set on mail items", vbOKOnly, "Not a mail item"
    ElseIf Err.Number = 9000 Then
        ' The active message has already been sent
        MsgBox "Please run this macro from an unsent mail item", vbOKOnly, "Not an unsent mail item"
    Else
        MsgBox "An error has occured on line " & Erl & _
                ", with a description: " & Err.Description & _
                ", and an error number " & Err.Number
    End If
        
End Sub

Sources de cet article :

Ajouter un commentaire

Le code HTML est affiché comme du texte et les adresses web sont automatiquement transformées.

Ajouter un rétrolien

URL de rétrolien : http://www.kamran.fr/dotclear/index.php?trackback/72

Haut de page