none
Creating a macro for Outlook 2016 to delete emails from a specific group mailbox. RRS feed

  • Question

  • I have found and tried to use the below code to delete emails in Outlook 2016 to delete emails from a shared mailbox older then 2 weeks.

    How could I set this up that it would actually run on the inbox folder in a specific shared mailbox?

    sub deleteolderthen()
    Dim olApp As Outlook.Application
    Dim olNS As Outlook.NameSpace
    Dim olMailItem As Outlook.MailItem
    Dim objVariant As Variant
    Dim lngMovedItems As Long
    Dim intCount As Integer
    Dim intDateDiff As Integer
    Dim olSharedBox As Folder
    Dim mbOwner As Outlook.Recipient
    
    
    Set olApp = Outlook.Application
    Set olNS = olApp.GetNamespace("MAPI")
    Set mbOwner = olNS.CreateRecipient("mailbox@email.com")
    Set olSharedBox = olNS.GetSharedDefaultFolder(mbOwner, olFolderInbox)
    
    For intCount = olSharedBox.Items.Count To 1 Step -1
        Set objVariant = olSharedBox.Items.Item(intCount)
        DoEvents
        If objVariant.Class = olMail Then
    
             intDateDiff = DateDiff("d", objVariant.SentOn, Now)
    
            ' Set number of days
            If intDateDiff > 14 Then
              objVariant.Delete
              Call ClearDeletedFolder ' Working. Will change to call every 100 emails deleted after first run.
    
              'count the # of items moved
               lngMovedItems = lngMovedItems + 1
    
            ' No need to run the IF statement on the rest of the mailbox assuming the macro runs from oldest to newest.
            'Else: GoTo Marker
    
            End If
        End If
    Next
    
    ' Display the number of items that were moved.
    Marker:
    MsgBox "Moved " & lngMovedItems & " messages(s)."
    End Sub

    Tuesday, August 13, 2019 7:55 AM