Outlook VSTO - BeforeAttachmentAdd sometimes is just not fired

Michał Kaszta 1 Reputation point
2021-03-02T13:01:53.807+00:00

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 Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,653 questions
0 comments No comments
{count} votes