Revision 472 - 2011-10-13 at 20:28:49
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using System.Windows.Forms;
using System.Diagnostics;

namespace CC_wo_attachments
{
    public partial class CC_wo_attachments
    {
        static int smallAttachment = 163840// 160kb

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            Application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
        }

        void Application_ItemSend(object Item, ref bool Cancel)
        {
            Outlook.MailItem msg;
            Outlook.Attachments attachs;
            Outlook.Recipients recips;

            msg = (Outlook.MailItem)Item;
            attachs = msg.Attachments;
            recips = msg.Recipients;

            int cc_bcc_count = recips.Count;
            foreach (Outlook.Recipient r in recips)
            {
                if (r.Type == (int)Outlook.OlMailRecipientType.olTo)
                    cc_bcc_count -= 1;
            }

            if ((attachs.Count > 0) && (cc_bcc_count > 0))
            {
                switch (MessageBox.Show("Exclude attachments for Cc and Bcc recipients?",
                    "Attachment Manager",
                    MessageBoxButtons.YesNoCancel,
                    MessageBoxIcon.Question,
                    MessageBoxDefaultButton.Button3))
                {
                    case DialogResult.No:
                        // Cancel = true; // for debugging purposes
                        break;
                    case DialogResult.Yes:   // send the attachment to ALL recipients
                        Attachments_to_CC_BCC(msg);
                        // Cancel = true; // for debugging purposes
                        break;
                    default:    // cancel the dialog box and let the user edit the message
                        Cancel = true;
                        break;
                }
            }
        }

        public void Attachments_to_CC_BCC(Outlook.MailItem msg)
        {
            string bccRecipients = null, ccRecipients = null;
            string toNotification = "This message was CC'd without attachments to";
            string[] ccNotification = {"You have been copied on this message originally sent to","Attachments have been removed."};

            Outlook.MailItem newMsg;
            Outlook.Attachments attachs, newMsgAttachs;
            Outlook.Recipients recips, newRecips;

            attachs = msg.Attachments;
            recips = msg.Recipients;

            foreach (Outlook.Recipient r in recips)
            {
                switch (r.Type)
                {
                    case (int)Outlook.OlMailRecipientType.olCC:
                        ccRecipients = ccRecipients + String.Format("{0};", r.Address);
                        break;
                    case (int)Outlook.OlMailRecipientType.olBCC:
                        bccRecipients = bccRecipients + string.Format("{0};", r.Address);
                        break;
                }
            }
            msg.CC = null;
            msg.BCC = null;

            newMsg = msg.Copy() as Outlook.MailItem;
            newRecips = newMsg.Recipients;
            newMsgAttachs = newMsg.Attachments;

            for (int d = newMsgAttachs.Count; d > 0; d--)
            {
                if (newMsgAttachs[d].Size > smallAttachment)
                    newMsgAttachs.Remove(d);
            }

            for (int d = newRecips.Count; d > 0; d--)
                newRecips.Remove(d);

            newMsg.CC = ccRecipients;
            newMsg.BCC = bccRecipients;
            newMsg.Recipients.ResolveAll();


            Word.Document toMsg = msg.GetInspector.WordEditor as Word.Document;
            Word.Range toRange = toMsg.Content;
            toRange.InsertBefore(String.Format("{0}:\n{1}\n{2}\n", toNotification, newMsg.CCBorder(toNotification.Length)));

            Word.Document ccMsg = newMsg.GetInspector.WordEditor as Word.Document;
            Word.Range ccRange = ccMsg.Content;
            ccRange.InsertBefore(String.Format("{0}:\n{1}\n{2}\n{3}\n", ccNotification[0], msg.To, ccNotification[1], Border(ccNotification.Max(c => c.Length)*2)));

            newMsg.Display();
            newMsg.Send();
        }

        string Border(int len)
        {
            return new string('-', len);
        }

        private void ThisAddIn_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(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }
}
My tags:
 
Popular tags:
  worldpeace test222 almosteverywikidoeshierarchy world whycaniaddtagsbutnotedit
Powered by Catalyst