Hello,
I need to copy the message from a lot of search folders but the code fails to do so if the code is executed right after executing the advanced search code (on newly created search folder).
What could be wrong? Does the newly created search folder need to be resolved before we can work with its items? If so, how?
Thank you in advance.
I need to copy the message from a lot of search folders but the code fails to do so if the code is executed right after executing the advanced search code (on newly created search folder).
What could be wrong? Does the newly created search folder need to be resolved before we can work with its items? If so, how?
Thank you in advance.
Code:
Sub GetResult(strAddress As String, strType As String)
Dim oStore As Outlook.Store
Dim oFolder As Outlook.folder, tFolder As Outlook.folder
Dim oNS As Outlook.NameSpace
Dim oItems As Outlook.Items
Dim mItem As Outlook.MailItem
Dim i As Integer
Dim oExplorer As Outlook.Explorer
Set oNS = Application.GetNamespace("MAPI")
'On Error Resume Next
Set tFolder = oNS.Session.GetDefaultFolder(olFolderInbox).Parent.Folders(strType)
If Err <> 0 Then
Err.Clear
Set tFolder = oNS.Session.GetDefaultFolder(olFolderInbox).Parent.Folders.Add(strType)
End If
'On Error GoTo 0
'On Error Resume Next
Set oStore = oNS.Session.DefaultStore
Set oExplorer = Application.ActiveExplorer
Set oFolder = oStore.GetSearchFolders(strAddress)
If Not oFolder Is Nothing Then
Set oExplorer.CurrentFolder = oFolder
oExplorer.Activate
'DoEvents
With oFolder
If .Items.Count > 0 Then
Set oItems = .Items
oItems.Sort "[ReceivedTime]", True
DoEvents
Debug.Print strAddress, .Items.Count, oItems(1).EntryID, oItems(1).Parent.storeID, oItems(1).Parent.FolderPath, oItems(1).Subject
Set mItem = oNS.GetItemFromID(oItems(1).EntryID, oItems(1).Parent.storeID)
'Set mItem = oItems(1).Copy
mItem.Copy
mItem.Move tFolder
End If
End With
'oFolder.Delete
End If
'On Error GoTo 0
End Sub