MyDaftQuestions
MyDaftQuestions

Reputation: 4691

How can I move mailItem from 1 store to another

I'm trying to move a mail item form 1 store to another, using Outlook 2010 and C#

I'm gotten quite far, but not sure how I perform the 'move'. I'm assuming it's saveas and then delete

My attempt (code greatly reduced)

         foreach (var mail in folder.Items)
           {
            //I am in the correct folder, and all I want to do is move all items to the 'inbox' of the store. I have already gotten the destination store and saved it as a variable called store

              Microsoft.Office.Interop.Outlook.MailItem mailItem = (Microsoft.Office.Interop.Outlook.MailItem)mail; //got the item

             mailItem.SaveAs(store.FilePath, Microsoft.Office.Interop.Outlook.OlSaveAsType.olMSG); // throws exception
             mailItem.Delete();
           }

I'm not sure if this is the best approach, but the line mailItem.SaveAs(store.FilePath, Microsoft.Office.Interop.Outlook.OlSaveAsType.olMSG); throws an exception:

The operation failed

I see no more detail other than that

Upvotes: 1

Views: 753

Answers (1)

Eugene Astafiev
Eugene Astafiev

Reputation: 49405

The SaveAs method saves the Microsoft Outlook item to the specified path and in the format of the specified file type. If the file type is not specified, the MSG format (.msg) is used. So, the method is used to save items on the disk, not another store in Outlook.

You can use the Move method of the MailItem class to move a Microsoft Outlook item to a new folder. For example:

Sub MoveItems() 
 Dim myNameSpace As Outlook.NameSpace 
 Dim myInbox As Outlook.Folder 
 Dim myDestFolder As Outlook.Folder 
 Dim myItems As Outlook.Items 
 Dim myItem As Object 

 Set myNameSpace = Application.GetNamespace("MAPI") 
 Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox) 
 Set myItems = myInbox.Items 
 Set myDestFolder = myInbox.Folders("Personal Mail") 
 Set myItem = myItems.Find("[SenderName] = 'Eugene'") 
 While TypeName(myItem) <> "Nothing" 
  myItem.Move myDestFolder 
  Set myItem = myItems.FindNext 
 Wend 
End Sub

Upvotes: 1

Related Questions