So I have an add in that monitors the sent items folder and uploads emails to an azure storage when a new item hits it, if it meets certain criteria. my issue is that it only works on the default account sent items. i need it to look at all the sent items. eg i have 3 accounts in outlook but it only works on the main account. this is the code that works for the default items:
Dim sentItems As Outlook.Items
Dim sentFolder As Outlook.Folder
Dim oApp As New Outlook.Application
sentFolder = oApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail)
sentItems = sentFolder.Items
AddHandler sentItems.ItemAdd, AddressOf itemadd
where "itemadd" is the function that does the upload work.
what i attempted to get it to do all the folders is
Dim sentfolders As List(Of Outlook.Folder)
Dim sentitemslist As List(Of Outlook.Items)
Dim oApp As New Outlook.Application
Dim accounts As Outlook.Accounts = oApp.Session.Accounts
Dim account As Outlook.Account
Dim i As Integer = 0
For Each account In accounts
sentfolders(i) = account.CurrentUser.Session.Folders(Outlook.OlDefaultFolders.olFolderSentMail) **
sentitemslist(i) = sentfolders(i).Items
AddHandler sentitemslist(i).ItemAdd, AddressOf itemadd
Next
which gives me a 'Object reference not set to an instance of an object.' error on the line marked with **. ive tried various other things on that line to no avail.
Folders()
takes either an integer index or a string (folder name). You are passing olFolderSentMail
enum value. More than that, your code is trying to access the same folder on each iteration of the loop: CurrentUser.Session
will be the same for all accounts.
Also, you never initialize the sentfolders
and sentitemslist
variables - that is why you get Object reference not set to an instance of an object.
error. When using these lists, you need to call Add
rather than set list entry by index.
What you need to do is loop through all stores in the oApp.Session.Stores
collection and call Store.GetDefaultFolder(olFolderSentMail)
for each store. Be prepared to handle (and ignore) exceptions as not all stores have the Sent Items folder; Public Folders or a secondary PST store are two example of stores like that.
Dim sentfolders As New List(Of Outlook.Folder)() ' Initialize the list
Dim sentitemslist As New List(Of Outlook.Items)() ' Initialize the list
Dim oApp As New Outlook.Application
Dim stores As Outlook.Stores = oApp.Session.Stores
Dim store As Outlook.Store
For Each store In stores
Try
' Try to get the Sent Items folder for the store
Dim sentFolder As Outlook.Folder = CType(store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail), Outlook.Folder)
If sentFolder IsNot Nothing Then
sentfolders.Add(sentFolder) ' Add the folder to the list
' Get the items in the Sent Items folder
Dim sentItems As Outlook.Items = sentFolder.Items
sentitemslist.Add(sentItems) ' Add the items to the list
' Add a handler for the ItemAdd event
AddHandler sentItems.ItemAdd, AddressOf itemadd
End If
Catch ex As Exception
' Ignore exceptions (e.g., if a store does not have a Sent Items folder)
Debug.WriteLine($"Store '{store.DisplayName}' does not have a Sent Items folder: {ex.Message}")
End Try
Next