Thursday, January 19, 2017

email - Outlook 2010 - VBA MsgBox if more then 10 unread elements mails

I would like to start Outlook, then Outlook should only show ALL mailboxes, which have also unread emails.




At the moment, my VBA-Script expand all my mailbox folders / accounts.
But the list is unfortunately too long, now.



I am looking for a way to check unread elements.
I have tried it with a simple MsgBox, but does not work.



    Private Sub Application_Startup()
'Folder-Variable definieren
Dim objFolderIMAP01 As Outlook.Folder

Dim objFolderIMAP02 As Outlook.Folder
Dim objFolderIMAP03 As Outlook.Folder

'IMAP-Folder zuweisen (hier in Klammern mit Anführungszeichen den Namen der IMAP-Datendatei eintragen)
Set objFolderIMAP01 = Outlook.Session.Folders("MailBox1")
Set objFolderIMAP02 = Outlook.Session.Folders("MailBox2")
Set objFolderIMAP03 = Outlook.Session.Folders("MailBox3")

'Alle Unterordner selektieren (und damit aufklappen)
Call selectAllFolderRec(objFolderIMAP01)

Call selectAllFolderRec(objFolderIMAP02)
Call selectAllFolderRec(objFolderIMAP03)

'Posteingang als Startordnder auswählen
Call Outlook.ActiveExplorer.SelectFolder(objFolderIMAP01.Folders("Posteingang"))
Call Outlook.ActiveExplorer.SelectFolder(objFolderIMAP02.Folders("Posteingang"))
Call Outlook.ActiveExplorer.SelectFolder(objFolderIMAP03.Folders("Posteingang"))

'Info wenn mehr als 10 Emails ungelesen
If objFolderIMAP01.UnReadItemCount > 10 Then

MsgBox "Mailbox 1 ist voll!"
End If
If objFolderIMAP01.UnReadItemCount > 10 Then
MsgBox "Mailbox 2 ist voll!"
End If
If objFolderIMAP01.UnReadItemCount > 10 Then
MsgBox "Mailbox 3 ist voll!"
End If
End Sub


Sub selectAllFolderRec(objFolder As Outlook.Folder)
Dim lngCounter As Long
Dim bolSkipSelect As Boolean
bolSkipSelect = False
For lngCounter = 1 To objFolder.Folders.Count
If objFolder.Folders(lngCounter).Folders.Count > 0 And objFolder.Folders(lngCounter).Folders.UnReadItemCount > 10 Then
Call selectAllFolderRec(objFolder.Folders(lngCounter))
Else
If bolSkipSelect = False Then
Call Outlook.ActiveExplorer.SelectFolder(objFolder.Folders(lngCounter))

bolSkipSelect = True
End If
End If
Next lngCounter
End Sub

No comments:

Post a Comment