Results 1 to 2 of 2

Thread: OL2003: Get Mail items from Folder

  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.

  2. #2

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

    Re: OL2003: Get Mail items from Folder

    I think I've solved this although I may not be the best solution.

    I'm using find to search for the marked up emails in the PermanentlyDelete Function

    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. ' Resources:
    7. ' [url]http://weblogs.asp.net/whaggard/archive/2004/11/12/256661.aspx[/url]
    8. ' [url]http://www.outlookcode.com/[/url]
    9.  
    10. Sub Alerts()
    11.     Dim nmsName As Outlook.NameSpace: Set nmsName = Application.GetNamespace("MAPI")
    12.     Dim fldPFolder As Outlook.MAPIFolder: Set fldPFolder = nmsName.GetDefaultFolder(olFolderInbox).Parent
    13.     Dim sTPath As String: sTPath = "Alerts"
    14.     Dim oFolder As Outlook.MAPIFolder: Set oFolder = getFolder(sTPath, fldPFolder)
    15.     Dim oMail As Outlook.MailItem
    16.     Dim bDoPermDel As Boolean: bDoPermDel = False
    17.     Dim sCat As String: sCat = "NetworkAlert"
    18.        
    19.     Do While oFolder.Items.Count > 0
    20.         DoEvents
    21.         Set oMail = oFolder.Items(1)
    22.         DoEvents
    23.         With oMail
    24.             .UnRead = False
    25.             .Categories = sCat
    26.             .Save
    27.             .Delete
    28.         End With
    29.         Set oMail = Nothing
    30.         DoEvents
    31.         bDoPermDel = True
    32.     Loop
    33.  
    34.  
    35.     If bDoPermDel = True Then Call PermanentlyDelete(sCat)
    36.            
    37.     Set oFolder = Nothing
    38.     Set fldPFolder = Nothing
    39.     Set nmsName = Nothing
    40. End Sub
    41.  
    42. Function getFolder(ByVal ssPath As String, PFolder As MAPIFolder) As MAPIFolder
    43.     Dim fldFolder As Outlook.MAPIFolder
    44.     Dim thisLevel
    45.     Dim nextLevel
    46.     Dim t As Integer: t = InStr(ssPath, "\")
    47.     If t = 0 Then
    48.         thisLevel = ssPath
    49.     Else
    50.         thisLevel = Left(ssPath, t - 1)
    51.         nextLevel = Mid(ssPath, t + 1)
    52.     End If
    53.    
    54.     For Each fldFolder In PFolder.Folders
    55.         If LCase(fldFolder.Name) = LCase(thisLevel) Then
    56.             If thisLevel = ssPath Then
    57.                 Set getFolder = fldFolder
    58.                 Exit Function
    59.             Else
    60.                 Set getFolder = getFolder(nextLevel, fldFolder)
    61.                 Exit Function
    62.             End If
    63.             DoEvents
    64.         End If
    65.     Next
    66. End Function
    67.  
    68. Function PermanentlyDelete(ByVal sCategory As String) As Boolean
    69.     On Error GoTo ErrorHandler
    70.     Dim bRet As Boolean: bRet = True
    71.     Dim oNS As Outlook.NameSpace: Set oNS = Application.GetNamespace("MAPI")
    72.     Dim oFolder As Outlook.MAPIFolder: Set oFolder = oNS.GetDefaultFolder(olFolderDeletedItems)
    73.     Dim oItems As Outlook.Items: Set oItems = oFolder.Items
    74.     Dim sSearch: sSearch = "[Categories] = " & Quote(sCategory)
    75.    
    76.     Set oFolder = Nothing
    77.     Set oNS = Nothing
    78.     ' New and improved: Only get msgs with the appropriate category
    79.     Set oItems = oItems.Restrict(sSearch)
    80.     DoEvents
    81.     If oItems.Count > 0 Then
    82.         Do While oItems.Count > 0
    83.             DoEvents
    84.             oItems.Remove (1)
    85.             DoEvents
    86.         Loop
    87.     End If
    88.    
    89.     PermanentlyDelete = bRet
    90.     Set oItems = Nothing
    91. Exit Function
    92. ErrorHandler:
    93.     If Err.Number <> 0 Then
    94.         Select Case Err.Number
    95.             Case 13: Resume Next ' Type Mismatch
    96.             Case Else
    97.                 bRet = False
    98.                 MsgBox ("Error No: " & Err.Number & vbNewLine & "Error Desc: " & Err.Description)
    99.                 PermanentlyDelete = bRet
    100.         End Select
    101.     End If
    102. End Function
    103.  
    104. Function Quote(MyText)
    105.     Quote = Chr(34) & MyText & Chr(34)
    106. End Function

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