简体   繁体   中英

Vb.net Outlook pick first in sent mail folder getfirst from last weeks items instead of the first item

In my Outlook addin a sub is run when an item is added to the sent mail folder. this item is then archived to a user defined folder (which is done when the mail items opens). In the code below it shows how I get the first items in the send item folder.

Public Sub mySentItems_ItemAdd() Handles mySentItems.ItemAdd

    'variables
    Dim AppOutlook As New Outlook.Application
    Dim ns As Outlook.NameSpace = AppOutlook.Session
    Dim siFolder As Outlook.Folder = CType(ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail), Outlook.Folder)

    'load the newly added mail as mailitem
    Dim mailitem As MailItem = siFolder.Items.GetFirst

    MsgBox(mailitem.Subject.ToString)


End Sub

It worked fine a few weeks ago but now it doesnt get the first item in the folder, instead it gets the first item in the folder from the sub folder "Last week". In the image below the item I get is marked with yellow, the item I want is underlined with a black line. does anyone know how I can solve this problem?

在此处输入图片说明

First of all, there is no need to create a new Outlook Application instance:

Dim AppOutlook As New Outlook.Application

Instead, you should use the Application property of your add-in class.

Anyway, the Items.ItemAdd event provides an argument which represents an item added to the folder.

Public WithEvents myOlItems As Outlook.Items 

Public Sub Initialize_handler()  
 Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderContacts).Items  
End Sub  

Private Sub myOlItems_ItemAdd(ByVal Item As Object)  
 Dim myOlMItem As Outlook.MailItem  
 Dim myOlAtts As Outlook.Attachments  
 Set myOlMItem = myOlApp.CreateItem(olMailItem)  
 myOlMItem.Save  
 Set myOlAtts = myOlMItem.Attachments  
 ' Add new contact to attachments in mail message  
 myOlAtts.Add Item, olByValue  
 myOlMItem.To = "Sales Team"  
 myOlMItem.Subject = "New contact"  
 myOlMItem.Send  
End Sub

Ok I figured it out, the last added item is not the first item in the list but the last item so instead of:

    Dim mailitem As MailItem = siFolder.Items.GetFirst

I needed to use

    Dim mailitem As MailItem = siFolder.Items.GetLast

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM