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 :
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
}
}