M
M
Maxim2015-08-01 16:31:32
Automation
Maxim, 2015-08-01 16:31:32

Is it possible to create a VBA macro for OUTLOOK with a condition on time and day of the week?

Good afternoon!
In MS Outlook, unfortunately, there is no way to create a rule with a temporary condition.
For example, you want to forward emails received from 7 pm to 8 am, but at the same time, the action must be performed for all emails around the clock on weekends (Friday 7 pm to Monday 8 am) .
The Internet found the possibility of filtering by time using a macro:

Public WithEvents myOlItems As Outlook.Items
 Public Sub Application_Startup()
    Set myOlItems = Outlook.Session.GetDefaultFolder(olFolderInbox).Items
 End Sub
 Private Sub myOlItems_ItemAdd(ByVal Item As Object)
    If Time() < #8:00:00 AM# Or Time() > #7:00:00 PM# Then
       If TypeName(Item) = "MailItem" Then
          Set myForward = Item.Forward
          myForward.Recipients.Add "[email protected]"
          myForward.Send
       End If
    End If
 End Sub

Is it possible to attach a condition to it, taking into account the weekend (marked in italics)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2015-08-01
@neversaymoo

Finished it on my own.
If anyone is interested:

Public WithEvents myOlItems As Outlook.Items
 Public Sub Application_Startup()
    Set myOlItems = Outlook.Session.GetDefaultFolder(olFolderInbox).Items
 End Sub
 Private Sub myOlItems_ItemAdd(ByVal Item As Object)
    If (WeekDay(checkindate) = 1 or 7) or (Time() < #9:00:00 AM# Or Time() > #5:00:00 PM#) Then
       If TypeName(Item) = "MailItem" Then
          Set myForward = Item.Forward
          myForward.Recipients.Add "[email protected]"
          myForward.Send
       End If
    End If
 End Sub

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question