none
Desperate for help with outlook addin RRS feed

  • Question

  • Good afternoon all. I am having a terrible time trying to figure out why my outlook addin isn't working. At startup, I need it to read all appointments whose subject contains a certain string, then it needs to send an email reminding the outlook user of the appointment. I've gotten it to work a few times, but when I tried spoofing the sender it blows up on me. I have been trying to get this to work for over 2 months. I'm out of ideas and mentally exhausted as to how to get it to work. ANY help would be appreciated. Also, like 75% of the time this addin loads directly as "inactive" and doesn't fire. Thank you.


    using System;
    using System.Threading;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Outlook = Microsoft.Office.Interop.Outlook;
    using Office = Microsoft.Office.Core;
    using Microsoft.Office.Interop.Outlook;
    using System.Windows.Forms;

    //http://stackoverflow.com/questions/6425776/outlook-2010-how-to-get-a-list-of-all-appointments-including-recurrences
    //https://www.add-in-express.com/creating-addins-blog/2013/06/10/outlook-calendar-appointment-meeting-items/
    //http://www.sperrysoftware.com/outlook/email-reminders.asp
    //myEmailAddress = this.ActiveExplorer().Session.CurrentUser.EmailAddress;
    //Application.Session.CurrentUser.AddressEntry.Address
    //https://www.add-in-express.com/creating-addins-blog/2013/06/10/outlook-calendar-appointment-meeting-items/#enumerate
    //http://www.scrubly.com/blog/how-to-outlook/how-to-install-enable-and-disable-outlook-2013-add-ins/
    //http://stackoverflow.com/questions/5472493/making-vsto-add-in-installable
    //https://msdn.microsoft.com/en-us/library/cc442767.aspx#Download

    namespace OutlookAddIn1
    {
        public partial class ThisAddIn
        {
            //Outlook.Inspectors inspectors;
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                /* //Nvm, needed to auto-add text to an email upon it's creation. Not necessary for what we're doing.
                //This block appears on the msdn page, but Inspectors_NewInspector isn't clear what it needs to be. -UPdate: Yes it does. It's a method.
                inspectors = this.Application.Inspectors;
                inspectors.NewInspector +=
                    new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
                */

                doStuff();
            }

            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);
            }

            //https://msdn.microsoft.com/en-us/library/ms268866.aspx
            
            private void doStuff()
            {
                Outlook.Application app = new Outlook.Application();
                Thread.Sleep(1200); //120 seconds - was 120000

                DateTime firstRun = DateTime.Now; //So we can check every 24 hours? Maybe once initially as well.
                DateTime lastRun = DateTime.Now;//.AddHours(1); //We're going to compare this to firstRun
                bool whileTrue = true;
                //int test = 0;
                
                try
                {
                    while (whileTrue)
                    {
                        if (whileTrue == true)//(firstRun > lastRun.AddDays(1))
                        {
                            Outlook.MAPIFolder calendarFolder = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
                            Outlook.Items outlookCalendarItems = calendarFolder.Items;
                            outlookCalendarItems.IncludeRecurrences = true; //was true

                            List<Outlook.AppointmentItem> lst = new List<Outlook.AppointmentItem>();

                            foreach (Outlook.AppointmentItem item in outlookCalendarItems)
                            {
                                lst.Add(item);
                                //We can probably just handle logic in here without the second for loop that comes next
                            }

                            foreach (Outlook.AppointmentItem x in lst)
                            {
                                //http://stackoverflow.com/questions/16329581/getting-system-runtime-interopservices-comexception-error-in-server-side-generat
                                DateTime startDate = DateTime.Now.AddDays(1);
                                DateTime endDate = DateTime.Now.AddDays(5);
                                DateTime apptDate = x.Start;
                            
                                if (x.Subject.ToLower().Contains("telos"))
                                {
                                    MessageBox.Show("X: " + x.Start + "XYZ: " + x.Subject);

                                    if (x.Start > startDate && x.Start < endDate)
                                    {
                                        //MessageBox.Show("X: " + x.Start + "XYZ: " + x.Subject);
                                       
                                        /*
                                        //Here we generate the email
                                         * */
                                        Microsoft.Office.Interop.Outlook.MailItem email = app.CreateItem((OlItemType.olMailItem));


                                        //Outlook.MailItem mail = (Outlook.MailItem)Globals.ThisAddIn.Application.CreateItem(Outlook.OlItemType.olMailItem);
                                        //Outlook.Recipient recipient = Globals.ThisAddIn.Application.Session.CreateRecipient("someone@example.com");
                                        //email.Sender = recipient.AddressEntry;


                                        //Outlook.Recipient recipient = app.Session.CreateRecipient("someone@example.com");
                                        //email.Sender = recipient.AddressEntry;
                                        //email.SentOnBehalfOfName = "someone@example.com";
                                        email.Display(true); //was false
                                        email.Subject = "You have a new appointment";
                                        email.Importance = Outlook.OlImportance.olImportanceLow;
                                        email.To = Application.Session.CurrentUser.AddressEntry.Address; //Current email address.
                                        email.Body = "This email was automatically generated to remind you have an upcoming appointment on: " + x.Start.ToString();
                                        email.Save();
                                        email.Close(OlInspectorClose.olSave);
                                        //((Outlook._MailItem)email).Send();
                                        //email.Send();
                                        //((Outlook._MailItem)mailItem).Send();
                                        
                                    }
                                }
                            }

                            lastRun = DateTime.Now;
                            whileTrue = false;
                        }
                        else
                        {
                            /*
                            Outlook.MailItem email = new Outlook.MailItem();
                            email.Subject = "This is only a test.";
                            email.To = Application.Session.CurrentUser.AddressEntry.Address; //Current email address.
                            email.Body = "This is only a test.";
                            //email.Send();
                            ((Outlook._MailItem)email).Send();
                             * */
                        }




                    }
                }
                catch (System.Exception e) //Microsoft.Office.Interop.Outlook.Exception e
                {
                    MessageBox.Show(e.InnerException.ToString());
                }
                finally
                {
                    app.Quit();
                }

            }



            #endregion
        }
    }
       

    Sunday, July 5, 2015 3:52 PM

Answers

  • Hello,

    First of all, there is no need to create a new Outlook Application instance in the code. You need to use the Application property of the add-in class.

    > At startup, I need it to read all appointments whose subject contains a certain string

    Don't use the foreach for iterating over all items in the folder. Instead, you need to use the Find/FindNext or Restrict methods of the Items class. You may read more about these methods in the following articles (the sample code is included):

    How To: Retrieve Outlook calendar items using Find and FindNext methods

    How To: Use Restrict method in Outlook to get calendar items

    When you are done I'd recommend using the Resolve or ResolveAll methods of the Recipient(s) class to resolve all recipients against the address book.

    > Also, like 75% of the time this addin loads directly as "inactive" and doesn't fire.

    Microsoft Office applications can disable add-ins that behave unexpectedly. If an application does not load your add-in, the application might have hard disabled or soft disabled your add-in.

    Hard disabling can occur when an add-in causes the application to close unexpectedly. It might also occur on your development computer if you stop the debugger while the Startup event handler in your add-in is executing.

    Soft disabling can occur when an add-in produces an error that does not cause the application to unexpectedly close. For example, an application might soft disable an add-in if it throws an unhandled exception while the Startup event handler is executing.When you re-enable a soft-disabled add-in, the application immediately attempts to load the add-in. If the problem that initially caused the application to soft disable the add-in has not been fixed, the application will soft disable the add-in again. Read more about that in the How to: Re-enable an Add-in That Has Been Disabled article.

    Also Outlook 2013 monitors add-in performance metrics such as add-in startup, shutdown, folder switch, item open, and invoke frequency. Outlook records the elapsed time in milliseconds for each performance monitoring metric. For example, the startup metric measures the time required by each connected add-in during Outlook startup. Outlook then computes the median startup time over 5 successive iterations. If the median startup time exceeds 1000 milliseconds (1 second), then Outlook disables the add-in and displays a notification to the user that an add-in has been disabled. The user has the option of always enabling the add-in, in which case Outlook will not disable the add-in even if the add-in exceeds the 1000 millisecond performance threshold. See Performance criteria for keeping add-ins enabled for more information.


    Sunday, July 5, 2015 5:12 PM