ActiveExplorer.SelectAllItems doesn't fill ActiveExplorer.Selection
I have a problem with the following code, which should process all mails in the current folder and all subfolders, using ActiveExplorer.SelectAllItems and then browsing through ActiveExplorer.Selection.
The weird thing is: when I debug through the code, everything is fine. When I let it run free, ActiveExplorer.Selection is empty (.Count = 0).
Has anybody got a clue?
(For reasons difficult to explain (I will try, if anybody is interested), I must use the "for each item in selection" construct.)
Code:
Dim MySelection
Dim NextFolder As folder
Dim pmoCurItem As Object
Dim arFoldersAndSubfolders(maxFolderIndex) As typeFolderLevel
Dim bFolderFound As Boolean
Dim index As Integer, iFolderIndex As Integer
index = 0
LoadArrayOfFoldersAndSubfolders Outlook.ActiveExplorer.CurrentFolder, 1, arFoldersAndSubfolders, index
iFolderIndex = 0
bFolderFound = True
Outlook.ActiveExplorer.SelectAllItems
Set MySelection = Outlook.ActiveExplorer.Selection
Debug.Print "MySelection.Count(A): " & MySelection.Count 'correct: 2
Do While bFolderFound
For Each pmoCurItem In MySelection
If pmoCurItem.Class = olMail Then
Debug.Print Timer & " | " & pmoCurItem.ReceivedTime & " " & pmoCurItem.SentOnBehalfOfName & " " & pmoCurItem.Subject
'... processing this mail-item ...
Else
Debug.Print "skipping: " & pmoCurItem.Class & " " & pmoCurItem.MessageClass
End If
Next pmoCurItem
Set NextFolder = arFoldersAndSubfolders(iFolderIndex).folder
If NextFolder Is Nothing Then
bFolderFound = False
Debug.Print "end of folders"
Else
Set Outlook.ActiveExplorer.CurrentFolder = NextFolder
Outlook.ActiveExplorer.SelectAllItems
Set MySelection = Outlook.ActiveExplorer.Selection
Debug.Print "MySelection.Count(: " & iFolderIndex & "): " & MySelection.Count 'wrong: 0
End If
iFolderIndex = iFolderIndex + 1
Loop
The results look like this:
"free run":
Quote:
MySelection.Count(A): 2
06.12.2021 12:34:40 Szech, Burkhard (EXT) Verschlüsselte Mail mit xlsx-Anhang
14.09.2021 08:48:32 Szech, Burkhard (EXT) Ver- und Entschlüsselungs-Test - intern
MySelection.Count(0): 0
MySelection.Count(1): 0
MySelection.Count(2): 0
end of folders
debugging through the same code:
Quote:
MySelection.Count(A): 2
06.12.2021 12:34:40 Szech, Burkhard (EXT) Verschlüsselte Mail mit xlsx-Anhang
14.09.2021 08:48:32 Szech, Burkhard (EXT) Ver- und Entschlüsselungs-Test - intern
MySelection.Count(0): 6
skipping: 53 IPM.Schedule.Meeting.Request
skipping: 53 IPM.Schedule.Meeting.Request
06.12.2021 12:29:30 Szech, Burkhard (EXT) Verschlüsselte Mail mit png-Anhang
14.09.2021 08:50:27 Szech, Burkhard (EXT) WG: Ver- und Entschlüsselungs-Test
14.09.2021 08:40:54 Szech, Burkhard (EXT) Ver- und Entschlüsselungs-Test
14.09.2021 08:40:00 Szech, Burkhard (EXT) Ver- und Entschlüsselungs-Test
MySelection.Count(1): 0
MySelection.Count(2): 2
14.09.2021 08:50:00 Szech, Burkhard (EXT) WG: Ver- und Entschlüsselungs-Test
14.09.2021 08:47:00 Szech, Burkhard (EXT) Ver- und Entschlüsselungs-Test - intern
end of folders