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("")
    Set olSharedBox = olNS.GetSharedDefaultFolder(mbOwner, olFolderInbox)
    For intCount = olSharedBox.Items.Count To 1 Step -1
        Set objVariant = olSharedBox.Items.Item(intCount)
        If objVariant.Class = olMail Then
             intDateDiff = DateDiff("d", objVariant.SentOn, Now)
            ' Set number of days
            If intDateDiff > 14 Then
              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
    ' Display the number of items that were moved.
    MsgBox "Moved " & lngMovedItems & " messages(s)."
    End Sub

    Tuesday, August 13, 2019 7:55 AM