Hello,
I have written some code that retrieves the number of mails from two inboxes. I've tested it locally however when I am trying to connect to a Shared Inbox, it returns a 0. I am very confused and I am wondering if someone can help me.
The names of the two shared inboxes are 1) FolderA and 2) FolderB. Both have some folders and subfolders in the following combination:
Shared inbox Folder Subfolder
[FolderA, Values]
[FolderA, Margins, PastMargins]
[FolderA, Lesbeque, Research]
[FolderB, Internal Commitments, Attached]
[FolderB, Extern Commitments, New]
Those routes leads to the desired mailbox where I try to extract the count.
I am very much wondering is someone can help me!
I have written some code that retrieves the number of mails from two inboxes. I've tested it locally however when I am trying to connect to a Shared Inbox, it returns a 0. I am very confused and I am wondering if someone can help me.
The names of the two shared inboxes are 1) FolderA and 2) FolderB. Both have some folders and subfolders in the following combination:
Shared inbox Folder Subfolder
[FolderA, Values]
[FolderA, Margins, PastMargins]
[FolderA, Lesbeque, Research]
[FolderB, Internal Commitments, Attached]
[FolderB, Extern Commitments, New]
Those routes leads to the desired mailbox where I try to extract the count.
Code:
Sub GetFromOutlook()
Dim folderlist1, folderlist2 As Variant
Dim num_of_folders As Variant
Dim accounts As Variant
Dim accname As String
Dim count As Integer
Dim sheetName As String
sheetName = "Import"
accounts = Array("FolderA", "FolderB") ' account name should be here
num_of_folders = Array(3, 2)
folderlist1 = Array("Values", "Margins", "Lesbeque", "Intern", "Extern") ' folders list
folderlist2 = Array("NULL", "PastMargins", "Research", "Attached", "New") ' sub folders like test02 in WS1, if there is no sub folder write NULL
lastRow = Worksheets(sheetName).Cells(Rows.count, 1).End(xlUp).Row
colIndex = 1
folder_index = LBound(folderlist1)
For kk = LBound(accounts) To UBound(accounts)
accname = accounts(kk)
For ii = folder_index To (folder_index + num_of_folders(kk) - 1)
If folderlist2(ii) = "NULL" Then
count = GetFolderCount(accname, folderlist1(ii))
Else
count = GetSubFolderCount(accname, folderlist1(ii), folderlist2(ii))
End If
Worksheets(sheetName).Cells(lastRow + 1, colIndex).Value = count
colIndex = colIndex + 1
Next ii
folder_index = folder_index + num_of_folders(kk)
Next kk
End Sub
Function GetFolderCount(ByVal accname As String, ByVal folder1 As String)
Dim OutlookApp As Outlook.Application
Dim oaccount As Outlook.Account
Dim ostore As Outlook.store
Dim OutlookNamespace As Namespace
Dim Folder As MAPIFolder
Dim OutlookMail As Variant
Dim i As Integer
Dim myNameSpace As Outlook.Namespace
Dim sheetName As String
sheetName = "Import"
Set OutlookApp = New Outlook.Application
For Each oaccount In OutlookApp.Session.accounts
If oaccount = accname Then
Set ostore = oaccount.DeliveryStore
Set Folder = ostore.GetDefaultFolder(olFolderInbox).folders(folder1)
i = 0 'Folder.ShowItemCount
For Each OutlookMail In Folder.Items
If TypeOf OutlookMail Is MailItem Then
i = i + 1
End If
Next OutlookMail
Set Folder = Nothing
Set OutlookNamespace = Nothing
Set OutlookApp = Nothing
End If
Next oaccount
GetFolderCount = i
End Function
Function GetSubFolderCount(ByVal accname As String, ByVal folder1 As String, ByVal folder2 As String)
Dim OutlookApp As Outlook.Application
Dim oaccount As Outlook.Account
Dim ostore As Outlook.store
Dim OutlookNamespace As Namespace
Dim Folder As MAPIFolder
Dim OutlookMail As Variant
Dim i As Integer
Dim myNameSpace As Outlook.Namespace
Dim sheetName As String
sheetName = "Import"
Set OutlookApp = New Outlook.Application
For Each oaccount In OutlookApp.Session.accounts
If oaccount = accname Then
Set ostore = oaccount.DeliveryStore
Set Folder = ostore.GetDefaultFolder(olFolderInbox).folders(folder1).folders(folder2)
i = 0 'Folder.ShowItemCount
For Each OutlookMail In Folder.Items
If TypeOf OutlookMail Is MailItem Then
i = i + 1
End If
Next OutlookMail
Set Folder = Nothing
Set OutlookNamespace = Nothing
Set OutlookApp = Nothing
End If
'ostore = Nothing
Next oaccount
GetSubFolderCount = i
End Function
I am very much wondering is someone can help me!
Last edited by a moderator: