admin管理员组文章数量:1332896
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.
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.
Share Improve this question edited Nov 21, 2024 at 5:54 Zorbacles asked Nov 21, 2024 at 5:44 ZorbaclesZorbacles 32 bronze badges1 Answer
Reset to default 0Folders()
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
本文标签: vbnetMonitoring all sent items folders in add inStack Overflow
版权声明:本文标题:vb.net - Monitoring all sent items folders in add in - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742311928a2451040.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论