Forum Discussion

TempCampus's avatar
TempCampus
Copper Contributor
Aug 16, 2023

Viablity of macros/rules in a given case

I'm attempting to implement a change to an employee's email box.  Basically, they want to pause all emails from being sent and received at 5:01 on Fridays and allow those emails to populate at 8:00am Monday morning.  I've been trying to implement a macro that will migrate those emails to folders called DowntimeSent/DowntimeReceived during the given downtime, but I'm having issues getting my macro to perform the desired action.  Here's a test snippet I'm working with currently.  It's written to allow the received mail to be sent to a folder called 'Downtime' today at 10:00am - 10:30am, and then allow that mail to delivered to inbox after 10:30.  I've been looking at macros, powershell cmdlets, and task scheduler configs, but haven't been able to get what I'm looking for.  My Macro security settings are configured correctly, so I'm reaching out for any advice.

Sub MoveEmailsToDowntimeFolder()
    Dim objNamespace As Outlook.NameSpace
    Dim objInbox As Outlook.MAPIFolder
    Dim objDowntimeFolder As Outlook.MAPIFolder
    Dim objMail As Outlook.mailItem
    Dim currentTime As Date
    Dim startTime As Date
    Dim endTime As Date
    
    ' Get the current time
    currentTime = Now
    
    ' Set the start time and end time for the move
    startTime = TimeValue("10:00:00 AM")
    endTime = TimeValue("10:30:00 AM")
    
    ' Check if the current time is within the specified range
    If currentTime >= startTime And currentTime <= endTime Then
        Set objNamespace = Application.GetNamespace("MAPI")
        Set objInbox = objNamespace.GetDefaultFolder(olFolderInbox)
        
        ' Create or get the "Downtime" folder
        On Error Resume Next
        Set objDowntimeFolder = objInbox.Folders("Downtime")
        On Error GoTo 0
        
        If Not objDowntimeFolder Is Nothing Then
            ' Loop through the items in the inbox
            For Each objMail In objInbox.Items
                If TypeOf objMail Is Outlook.mailItem And objMail.receivedTime >= Date + startTime And objMail.receivedTime <= Date + endTime Then
                    ' Move the email to the "Downtime" folder
                    objMail.Move objDowntimeFolder
                End If
            Next objMail
        End If
        
        ' Clean up
        Set objMail = Nothing
        Set objDowntimeFolder = Nothing
        Set objInbox = Nothing
        Set objNamespace = Nothing
    End If
End Sub

 

No RepliesBe the first to reply

Resources