Move a mail

octola

New member
Joined
Jul 2, 2020
Messages
4
Programming Experience
Beginner
Hello,
I found a code that allows to move a selected mail to a selected folder (outlook). It works, but when I run the code and then select any mail, it is moved to the "2019" folder. I would like the process to be active only for the first selected mail. Does anyone know if this is possible?

Here is my code :

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using System.IO;
using Microsoft.Office.Interop.Outlook;
 
namespace deplacermail
{
    public partial class ThisAddIn
    {
        private Explorer currentExplorer;
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            currentExplorer = this.Application.ActiveExplorer();
            currentExplorer.SelectionChange += new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(CurrentExplorer_Event);
        }
 
        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            // Remarque*: Outlook ne déclenche plus cet événement. Si du code
            //    doit s'exécuter à la fermeture d'Outlook (consultez https://go.microsoft.com/fwlink/?LinkId=506785)
        }
 
        public void CurrentExplorer_Event()
        {
            Outlook.MAPIFolder inBox = (Outlook.MAPIFolder)this.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox); //sélectionne la boîte de réception
            Outlook.MAPIFolder selectedFolder = this.Application.ActiveExplorer().CurrentFolder; // dossier sélectionné -> inutile?
            string path = selectedFolder.FolderPath;
            Outlook.MAPIFolder destFolder = inBox.Folders["2019"]; // Dossier où l'on veut déplacer le mail
 
                if (this.Application.ActiveExplorer().Selection.Count > 0)
                {
                    Object selObject = this.Application.ActiveExplorer().Selection[1]; // mail sélectionné
                    Outlook.MailItem moveMail = null; //initialisation
                    if (selObject is Outlook.MailItem)
                    {
                        moveMail = selObject as Outlook.MailItem;
                        moveMail.Move(destFolder); // Les mails sont déplacés dans le dossier choisi
 
 
                    }
                }
 
 
 
        }
 
        #region Code généré par VSTO
 
        /// <summary>
        /// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
        /// le contenu de cette méthode avec l'éditeur de code.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
 
        #endregion
    }
}
 
Well, that's the problem with "found code". You need to understand what it does. In this case, it hooks into the selection change event and operates on the selected item. Of course once the mail item is moved, there will be a new selection. And so the cycle repeats.

You will need to change the code so that you do your move logic when a button is pressed, rather than when the selection changes.
 
If you are serious about learning to programme, then you need to stop looking for code online. Start reading documentation on the languages and frameworks, and then start writing your own code. When you write your own code, you know what it does, but when you take code from someone else, you either disseminate your knowledge for whatever language the code is based on, or you need to be prepared to work out the riddle of logic from the user you got it from.

You're on the wrong path for a learning curve.
 
Back
Top Bottom