Copy Move item won't work

Chooriang

Member
Outlook version
Outlook 2010 64 bit
Email Account
Outlook.com (as MS Exchange)
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.

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
 

Chooriang

Member
Outlook version
Outlook 2010 64 bit
Email Account
Outlook.com (as MS Exchange)
No matter if strType is hardcoded or passed to it but it does work if I wait.
No error message at all.
 
Similar threads
Thread starter Title Forum Replies Date
Commodore Move turns into "copy" Using Outlook 3
Commodore Folders always closed in move/copy items dialog box Using Outlook 3
K Outlook Rules: Move a Copy Using Outlook 4
B Copy/Move Exchange inbox to Pop inbox Using Outlook 4
JimR Copy or move contacts into BCM? BCM (Business Contact Manager) 1
P I have Outlook 2007 on a two year old computer running Vista. How do I copy my Outlook Contacts list and move them over to Outlook 2010 on a brand new Using Outlook 1
B OL 2010: Move to/copy to folder: how to expand list of options? Using Outlook 2
A Cannot copy this folder because it may contain private items Using Outlook 0
B VBA to convert email to task, insert text of email in task notes, and attach copy of original email Outlook VBA and Custom Forms 4
C Copy Outlook contact field value to another field Outlook VBA and Custom Forms 1
J Copy to calendar function no longer working in outlook 365 Using Outlook 5
F Copy and replace not update contact in another pst Using Outlook 0
B Outlook Business Contact Manager with SQL to Excel, User Defined Fields in BCM don't sync in SQL. Can I use VBA code to copy 1 field to another? BCM (Business Contact Manager) 0
N Outlook rules don't create a copy for bcc'ed emails Using Outlook 3
geofferyh Outlook 2010 How to Copy Outlook Attachment to a Specific Folder? Outlook VBA and Custom Forms 3
S Custom Form, copy user field data to message body Outlook VBA and Custom Forms 12
R Copy Outlook Public Folders to a File Server Shared Folder Using Outlook 0
oliv- HOW TO COPY /USE FOLDERS ICONS Outlook VBA and Custom Forms 2
E Copy e-mail body from outlook and insert into excel Outlook VBA and Custom Forms 3
R Sending email copy (*.msg file) of sent email if subject line contains specific string. Outlook VBA and Custom Forms 1
O Copy mails from many subfolders to 1 foldr Using Outlook 2
K ind specific Subject line from outlook and copy the content of the email body to exce Outlook VBA and Custom Forms 0
K How to find specific header and copy the mail body Using Outlook 0
J Copy or Export Outlook Mail to Excel Outlook VBA and Custom Forms 6
G Copy Contact field to Appointment Custom Form Field Outlook VBA and Custom Forms 2
G How to Copy Multi Select Listbox Data to Appointment Outlook VBA and Custom Forms 3
Carrie Dickey Outlook 2016 created two calendars titled Calendar1 - appear to be a copy Using Outlook 2
P How to copy and append data from Outlook 2016 message into Excel 2016 workbook Using Outlook 0
Stilgar Relsik Create a rule to copy text from an email and paste it in the subject line. Using Outlook 1
R Macro to copy email to excel - Runtime Error 91 Object Variable Not Set Outlook VBA and Custom Forms 11
H Macro to Copy Specific content from Mail Body and Paste to Excel Outlook VBA and Custom Forms 4
M How to keep reccurence during copy tasks to calendar? Using Outlook 1
Diane Poremsky Copy New Appointments to Another Calendar using VBA New Slipstick.com Articles 0
Diane Poremsky Use a macro to copy data in Outlook email to Excel workbook New Slipstick.com Articles 0
C Copy Task to Non-Microsoft PIM "Rainlendar" Using Outlook 0
G VBA Copy draft email to a new email - attachments not copided Using Outlook 7
C Copy email to excel runtime error 5020 Using Outlook 5
I Copy email from folder to folder - FAILS Using Outlook 5
M Copy new appointments created in multiple shared calendars to another exchange calendar Outlook VBA and Custom Forms 1
Q Why can't I copy image with embedded hyperlink from email to Word Using Outlook 0
I How to make a copy of a task Using Outlook 8
F copy data in Custom Field to other folder Outlook VBA and Custom Forms 2
A Outlook: copy & paste Outlook VBA and Custom Forms 9
V Copy and paste body and subject and send multiple emails Outlook VBA and Custom Forms 3
Diane Poremsky Use a macro to copy data in Outlook email to Excel workbook New Slipstick.com Articles 0
joflo copy contents from outlook to excell - please help. Using Outlook 4
L Copy email body fields to excel Using Outlook 0
G outlook 13 copy & paste without losing formatting Using Outlook 1
C Copy Cell value from Excel and paste into current email Outlook VBA and Custom Forms 10
C How to copy same text header to multiple emails with custom text column Using Outlook 10

Similar threads

Top