private void buttonCloseInspector_Click(object sender, EventArgs e)
        {
            try
            {
                Outlook.MailItem
                    mailItem = null;

                Outlook.Inspector
                    inspector = null;

                try
                {
                    if ((mailItem = OutlookItem as Outlook.MailItem) != null &&
                        (inspector = mailItem.Application.ActiveInspector()) != null)
                    {
                        inspector.Close(Outlook.OlInspectorClose.olDiscard);
                    }
                }
                finally
                {
                    if (inspector != null)
                    {
                        Marshal.ReleaseComObject(inspector);
                        inspector = null;
                    }

                    if (mailItem != null)
                    {
                        Marshal.ReleaseComObject(mailItem);
                        mailItem = null;
                    }
                }
            }
            catch (Exception eException)
            {
                string msg;

                ThisAddIn.WriteToLog(msg = eException.GetType().FullName + Environment.NewLine + "Message: " + eException.Message + Environment.NewLine + (eException.InnerException != null && !string.IsNullOrEmpty(eException.InnerException.Message) ? "InnerException.Message" + eException.InnerException.Message + Environment.NewLine : string.Empty) + "StackTrace:" + Environment.NewLine + eException.StackTrace);
                textBoxLog.Text          = msg;
            }
        }
 public override void Close()
 {
     inspector.Close(RlOutlook.OlInspectorClose.olPromptForSave);
 }
Exemple #3
0
        public string GetExistingSig()
        {
            string signature = "";

            // Create the Outlook application.
            // Create a new mail item.
            Microsoft.Office.Interop.Word.Bookmark  bkm      = null;
            Microsoft.Office.Interop.Word.Bookmarks bkms     = null;
            Microsoft.Office.Interop.Word.Document  document = null;
            Outlook.Inspector inspector = null;
            Outlook.MailItem  oMsg      = null;

            try
            {
                oMsg = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);

                //Add an attachment.
                inspector = oMsg.GetInspector;
                document  = (Microsoft.Office.Interop.Word.Document)inspector.WordEditor;
                document.Bookmarks.ShowHidden = true;
                bkms            = document.Bookmarks;
                bkms.ShowHidden = true;
            }
            catch (System.Exception)
            {
                Console.WriteLine("[!] Couldn't get Outlook COM object");
                return(null);
            }

            try
            {
                try
                {
                    bkm = bkms["_MailAutoSig"];
                }
                catch (System.Exception ex)
                {
                    // skip the exception
                }
                if (bkm != null)
                {
                    Microsoft.Office.Interop.Word.Range bkmRange = bkm.Range;
                    var bkmText = bkmRange.Text;
                    if (string.IsNullOrWhiteSpace(bkmText))
                    {
                        Console.WriteLine("Signature Empty");
                    }
                    else
                    {
                        //At this point, we know there is a signature already present - the text of the signature is stored in bkmText
                        Console.WriteLine("Existing Signature is: " + bkmText);
                    }
                    signature = bkmText.ToString();
                    Marshal.ReleaseComObject(bkmRange); bkmRange = null;
                    Marshal.ReleaseComObject(bkm); bkm           = null;
                }
                else
                {
                    Console.WriteLine("No Signature");
                }
            }
            catch (System.Exception ee)
            {
                Console.WriteLine("Couldn't get existing signature from email body: " + ee.Message);
            }

            try
            {
                oMsg.Close(OlInspectorClose.olDiscard);
                document.Close(WdSaveOptions.wdDoNotSaveChanges);
                inspector.Close(OlInspectorClose.olDiscard);
            }
            catch (System.Exception)
            {
                Console.WriteLine("Couldn't close Outlook inspector.");
            }

            try
            {
            }
            catch (System.Exception)
            {
                Console.WriteLine("Couldn't close Word doc from signature inspection");
            }
            return(signature);
        }