none
Outlook 2013 copy received message to multiple subfolders RRS feed

  • Question

  • Outlook 2013 on a Windows 7 Pro 64 bit laptop

    When an identified message arrives, I want to copy it to multiple to subfolders.

    When I used the script below I got the following error message.

    Run-time error ‘-2147221233 (8004010f)’:

    The attempted operation failed. An object could not be found.

    The Debug highlighted the second line in the ‘//loop which is “Set olDestFolder = olInbox.Folders("001Backend") 'Folder”

    Is the problem that I named the subfolder without a path, something else, or is there a better way to do this?

    Option Explicit

    Public Sub MoveItems(olItem As Outlook.MailItem)

        Dim olApp As New Outlook.Application

        Dim olNameSpace As Outlook.NameSpace

        Dim olInbox As Outlook.MAPIFolder

        Dim olDestFolder As Outlook.MAPIFolder

        Dim olItems As Outlook.Items

        Dim CopyItem As Object

        Set olNameSpace = olApp.GetNamespace("MAPI")

        Set olInbox = olNameSpace.GetDefaultFolder(olFolderInbox)

        Set olItems = olInbox.Items

        '// loop

        While TypeName(olItem) <> "Nothing"

            Set olDestFolder = olInbox.Folders("001Backend") 'Folder

            Set CopyItem = olItem.Copy

            olItem.Move olDestFolder

            Set olDestFolder = olInbox.Folders("001Ruby") 'Folder

            olItem.Move olDestFolder

            Set olDestFolder = olInbox.Folders("001Rails") 'Folder

            olItem.Move olDestFolder

            '// Add more folders here

            Set olItem = olItems.FindNext

        Wend

        Set olNameSpace = Nothing

        Set olInbox = Nothing

        Set olDestFolder = Nothing

        Set olItems = Nothing

        Set olItem = Nothing

        Set CopyItem = Nothing

    End Sub

    Saturday, December 12, 2015 9:23 PM

Answers

  • Hello,

    > The attempted operation failed. An object could not be found.

    Make sure such folder exists. For that, you can iterate over all subfolders checking their names. See How to: Enumerate Folders for more information.

    The Folders property of the Folder class returns the Folders collection that represents all the folders contained in the specified Folder. 

    • Proposed as answer by David_JunFeng Monday, December 14, 2015 3:23 AM
    • Marked as answer by David_JunFeng Wednesday, December 23, 2015 2:20 PM
    Sunday, December 13, 2015 4:51 PM
  • >>>Run-time error ‘-2147221233 (8004010f)’:

    The attempted operation failed. An object could not be found.<<<

    According to your description and error, I suggest that you could make sure that there is the special folder in Outlook Inbox folder, you could refer to below code:

    Function IsExistFolder(folderName As String, infolder As Folder) As Boolean
       For Each sfolder In infolder.Folders
          If sfolder.Name = folderName Then
             IsExistFolder = True
          End If
       Next
    End Function

    then you could modify your codes like below:

    If IsExistFolder("001Backend", olInbox) Then
        Set olDestFolder = olInbox.Folders("001Backend") 'Folder
    Else
        Set olDestFolder = olInbox.Folders.Add("001Backend")
    End If

    For more information, click here to refer about Folders Object (Outlook)

    • Marked as answer by David_JunFeng Wednesday, December 23, 2015 2:20 PM
    Monday, December 14, 2015 4:36 AM

All replies

  • Hello,

    > The attempted operation failed. An object could not be found.

    Make sure such folder exists. For that, you can iterate over all subfolders checking their names. See How to: Enumerate Folders for more information.

    The Folders property of the Folder class returns the Folders collection that represents all the folders contained in the specified Folder. 

    • Proposed as answer by David_JunFeng Monday, December 14, 2015 3:23 AM
    • Marked as answer by David_JunFeng Wednesday, December 23, 2015 2:20 PM
    Sunday, December 13, 2015 4:51 PM
  • >>>Run-time error ‘-2147221233 (8004010f)’:

    The attempted operation failed. An object could not be found.<<<

    According to your description and error, I suggest that you could make sure that there is the special folder in Outlook Inbox folder, you could refer to below code:

    Function IsExistFolder(folderName As String, infolder As Folder) As Boolean
       For Each sfolder In infolder.Folders
          If sfolder.Name = folderName Then
             IsExistFolder = True
          End If
       Next
    End Function

    then you could modify your codes like below:

    If IsExistFolder("001Backend", olInbox) Then
        Set olDestFolder = olInbox.Folders("001Backend") 'Folder
    Else
        Set olDestFolder = olInbox.Folders.Add("001Backend")
    End If

    For more information, click here to refer about Folders Object (Outlook)

    • Marked as answer by David_JunFeng Wednesday, December 23, 2015 2:20 PM
    Monday, December 14, 2015 4:36 AM