2017-04-03 1 views
0

J'essaie d'obtenir le fichier d'attachement dans un plugin Outlook avant que le fichier ne soit attaché à un mailItem.Ajouter un événement d'attachement Outlook AddIn

private void Inspectors_NewInspector(Outlook.Inspector Inspector) 
     { 

      if (Inspector.CurrentItem is Outlook.MailItem) 
      { 

    Outlook.MailItem mail = (Outlook.MailItem)Inspector.CurrentItem; 
        Inspector.AttachmentSelectionChange += Inspector_AttachmentSelectionChange; 
        Application.AttachmentContextMenuDisplay += Application_AttachmentContextMenuDisplay; 
        mail.BeforeAttachmentAdd += Mail_BeforeAttachmentAdd; 
        mail.AttachmentAdd += Mail_AttachmentAdd; 
        mail.BeforeAttachmentWriteToTempFile += Mail_BeforeAttachmentWriteToTempFile; 
        mail.BeforeAttachmentSave += Mail_BeforeAttachmentSave; 
}} 

Quand je crée un nouveau message dans Outlook, mon code passe par cette méthode, mais l'événement est jamais déclenché lorsque j'ajouter une pièce jointe à mon email.

Une idée?

+1

La réponse est ici: [gestionnaire d'événements pas ajouté à de nouveaux articles de courrier] (http://stackoverflow.com/questions/24576890/event-handler-not-being-added-to-new-mail-items) –

+0

Wow ... votre droite! Merci beaucoup –

+0

Glad it helps;) –

Répondre

1

Vous devez déclarer l'objet source au niveau de la classe (portée mondiale) pour l'empêcher de dwiping par le collecteur des ordures, par exemple:

Outlook.MailItem mail = null; 
    Outlook.Inspector inspector = null; 

    private void Inspectors_NewInspector(Outlook.Inspector Inspector) 
    { 
     inspector = Inspector; 
     object oMail = inspector.CurrentItem; 
     if (oMail is Outlook.MailItem) 
     { 

       mail = (Outlook.MailItem)oMail.CurrentItem;    
       inspector.AttachmentSelectionChange += Inspector_AttachmentSelectionChange; 
       Application.AttachmentContextMenuDisplay += Application_AttachmentContextMenuDisplay; 
       mail.BeforeAttachmentAdd += Mail_BeforeAttachmentAdd; 
       mail.AttachmentAdd += Mail_AttachmentAdd; 
       mail.BeforeAttachmentWriteToTempFile += Mail_BeforeAttachmentWriteToTempFile; 
       mail.BeforeAttachmentSave += Mail_BeforeAttachmentSave; 
     } 
    }