Search code examples
vbaoutlookmeeting-request

Skip meeting request in for each selection loop


I am working on a simple Marco that use to download selected emails' attachments. It was really a simple logic but I am still stuck.

I found out that my for each loop is always stop when it met the meeting request email. (It almost took my whole day to figure it out that the meeting request is The Barricade.) The problem can be fixed by deletion of the meeting request. And yet, it is really annoying for a lazybones like me.

Therefore, I really curious that is there any method can let the for each loop just ignore/auto unselect meeting requests?

And I have already tried detect the email subject/context to seprate the meeting requests and normal mails. But it seem like it would just exit the for each loop when it encountered the meeting request.

So currently I don't have any idea about how to fixing it.


Solution

  • is there any method can let the for each loop just ignore/auto unselect meeting requests?

    In the loop you need to check whether the Class property equals to the olMail value:

    For x = 1 To Item.Count 
       If Items.Item(x).Class = OlObjectClass.olMail Then 
    
       End If
    Next x
    

    For example, here is a sample code which shows how to handle different item types in Outlook:

    Sub GetSelectedItems() 
     Dim myOlExp As Outlook.Explorer 
     Dim myOlSel As Outlook.Selection 
     Dim mySender As Outlook.AddressEntry 
     Dim oMail As Outlook.MailItem 
     Dim oAppt As Outlook.AppointmentItem 
     Dim oPA As Outlook.PropertyAccessor 
     Dim strSenderID As String 
     Const PR_SENT_REPRESENTING_ENTRYID As String = _ 
     "http://schemas.microsoft.com/mapi/proptag/0x00410102" 
     Dim MsgTxt As String 
     Dim x As Long 
     
     MsgTxt = "Senders of selected items:" 
     Set myOlExp = Application.ActiveExplorer 
     Set myOlSel = myOlExp.Selection 
     For x = 1 To myOlSel.Count 
       If myOlSel.Item(x).Class = OlObjectClass.olMail Then 
         ' For mail item, use the SenderName property. 
         Set oMail = myOlSel.Item(x) 
         MsgTxt = MsgTxt & oMail.SenderName & ";" 
       ElseIf myOlSel.Item(x).Class = OlObjectClass.olAppointment Then 
         ' For appointment item, use the Organizer property. 
         Set oAppt = myOlSel.Item(x) 
         MsgTxt = MsgTxt & oAppt.Organizer & ";" 
       Else 
         ' For other items, use the property accessor to get sender ID, 
         ' then get the address entry to display the sender name. 
         Set oPA = myOlSel.Item(x).PropertyAccessor 
         strSenderID = oPA.GetProperty(PR_SENT_REPRESENTING_ENTRYID) 
         Set mySender = Application.Session.GetAddressEntryFromID(strSenderID) 
         MsgTxt = MsgTxt & mySender.Name & ";" 
       End If 
     Next x 
     
     Debug.Print MsgTxt 
    
    End Sub
    

    Remember that Outlook folders may contain different kind of items - mail, appointment, document, note and etc.