Results 1 to 2 of 2

Thread: OL2003: Get Mail items from Folder

Threaded View

  1. #1

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    OL2003: Get Mail items from Folder

    Hi,

    I'm trying to permanently delete items from a mail folder see below for what I have so far

    At the moment I have to loop through each message in the deleted folder. Is there a way of just getting messages with a specific category/ies?

    vb Code:
    1. Option Explicit
    2.  
    3. ' To use the Alerts and helpdesk functions you must create two categories
    4. ' "NetworkAlert" and "HelpDesk". These are used to mark the messages, so we can just delete
    5. ' the specific messages.
    6.  
    7. Sub Alerts()
    8.     Dim nmsName As Outlook.NameSpace: Set nmsName = Application.GetNamespace("MAPI")
    9.     Dim fldPFolder As Outlook.MAPIFolder: Set fldPFolder = nmsName.GetDefaultFolder(olFolderInbox).Parent
    10.     Dim sTPath As String: sTPath = "Alerts"
    11.     Dim oFolder As Outlook.MAPIFolder: Set oFolder = getFolder(sTPath, fldPFolder)
    12.     Dim oMail As Outlook.MailItem
    13.     Dim bDoPermDel As Boolean: bDoPermDel = False
    14.    
    15.     For Each oMail In oFolder.Items
    16.         oMail.UnRead = False
    17.         oMail.Categories = "NetworkAlert"
    18.         oMail.Save
    19.         oMail.Delete
    20.         DoEvents
    21.         bDoPermDel = True
    22.     Next
    23.  
    24.     If bDoPermDel = True Then
    25.         Call PermanentlyDelete("NetworkAlert")
    26.     End If
    27.            
    28.     Set oMail = Nothing
    29.     Set oFolder = Nothing
    30.     Set fldPFolder = Nothing
    31.     Set nmsName = Nothing
    32. End Sub
    33.  
    34. Function getFolder(ByVal ssPath As String, PFolder As MAPIFolder) As MAPIFolder
    35.     Dim fldFolder As Outlook.MAPIFolder
    36.     Dim thisLevel
    37.     Dim nextLevel
    38.     Dim t As Integer: t = InStr(ssPath, "\")
    39.     If t = 0 Then
    40.         thisLevel = ssPath
    41.     Else
    42.         thisLevel = Left(ssPath, t - 1)
    43.         nextLevel = Mid(ssPath, t + 1)
    44.     End If
    45.    
    46.     For Each fldFolder In PFolder.Folders
    47.         If LCase(fldFolder.Name) = LCase(thisLevel) Then
    48.             If thisLevel = ssPath Then
    49.                 Set getFolder = fldFolder
    50.                 Exit Function
    51.             Else
    52.                 Set getFolder = getFolder(nextLevel, fldFolder)
    53.                 Exit Function
    54.             End If
    55.             DoEvents
    56.         End If
    57.     Next
    58. End Function
    59.  
    60. Function PermanentlyDelete(ByVal sCategory As String) As Boolean
    61.     On Error GoTo ErrorHandler
    62.     Dim bRet As Boolean: bRet = True
    63.     Dim nmsName As Outlook.NameSpace: Set nmsName = Application.GetNamespace("MAPI")
    64.     Dim oFolder As Outlook.MAPIFolder: Set oFolder = nmsName.GetDefaultFolder(olFolderDeletedItems)
    65.     Dim oMail As Outlook.MailItem
    66.    
    67.     ' TODO: Attempt to only get emails for the specified category so we don't loop the entire deleted folder each time
    68.     For Each oMail In oFolder.Items
    69.         If oMail.Categories = sCategory Then
    70.             oMail.Delete
    71.             DoEvents
    72.         End If
    73.         DoEvents
    74.     Next
    75.    
    76.     PermanentlyDelete = bRet
    77.     Set oMail = Nothing
    78.     Set oFolder = Nothing
    79.     Set nmsName = Nothing
    80. Exit Function
    81. ErrorHandler:
    82.     If Err.Number <> 0 Then
    83.         bRet = False
    84.         MsgBox ("Error No: " & Err.Number & vbNewLine & "Error Desc: " & Err.Description)
    85.         PermanentlyDelete = bRet
    86.     End If
    87. End Function

    Any help would be appreciated

    Cheers Al
    Last edited by aconybeare; Mar 18th, 2008 at 07:11 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width