question

MichaKaszta-6341 avatar image
0 Votes"
MichaKaszta-6341 asked ·

Outlook VSTO - BeforeAttachmentAdd sometimes is just not fired

Hello,

I'm struggling with catching BeforeAttachmentAdd event.
Sometimes it's just not fired, and I don't know why.
There are no exceptions, event is properly registered but id doesn't fire before adding attachment.

Here's my code:

 using Outlook = Microsoft.Office.Interop.Outlook;
 using System;
 using System.Windows.Forms;
 using NG_plugin.Language;
 using System.Diagnostics;
 using NG_plugin.Forms;
 using NG_plugin.Helpers;
    
 namespace NG_plugin
 {
     public partial class NGAddIn
     {
         private Outlook.Application application;
         private Outlook.Inspectors inspectors;
         private Outlook.Explorer explorer;
    
         private void NGAddIn_Startup(object sender, System.EventArgs e)
         {
             this.application = Globals.NGAddIn.Application;
             this.inspectors = this.application.Inspectors;
             this.explorer = application.Explorers.Application.ActiveExplorer();
            
             this.inspectors.NewInspector += NewMailInspector;
         }
    
         internal static void NewMailInspector(Outlook.Inspector inspector)
         {
             bool isProtectAttachmentEnabled = Properties.Settings.Default.ProtectedAttachment_isEnabled;
             
             Outlook.MailItem mailItem = inspector.CurrentItem as Outlook.MailItem;
             if (isProtectAttachmentEnabled && mailItem != null)
             {
                 mailItem.BeforeAttachmentAdd += BeforeAttachmentInspector;                
             }
         }
    
         private static void BeforeAttachmentInspector(Outlook.Attachment attachment, ref bool cancel)
         {
             Outlook.MailItem mailItem = attachment.Parent;            
    
             if (attachment.Parent is Outlook.MailItem)
             {
                 FormPassword formPassword = new FormPassword();
    
                 if (formPassword.ShowDialog() == DialogResult.OK)
                 {
                     string zipFilePath = Zipper.ZipAndProtectAttachment(attachment, formPassword.Password);
    
                     mailItem.Attachments.Remove(mailItem.Attachments.Count);
                     mailItem.Attachments.Add(zipFilePath);
                 }
             }
         }
    
         private void NGAddIn_Shutdown(object sender, System.EventArgs e)
         {
    
         }
    
         #region VSTO generated code
    
         /// <summary>
         /// Required method for Designer support - do not modify
         /// the contents of this method with the code editor.
         /// </summary>
         private void InternalStartup()
         {
             this.Startup += new System.EventHandler(NGAddIn_Startup);
             this.Shutdown += new System.EventHandler(NGAddIn_Shutdown);
         }
    
         #endregion        
     }
 }
office-addins-devoffice-vsto-com-dev
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

0 Answers