RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Write a Custom Outlook Add-In with Visual Studio Tools for Office : Page 3

Microsoft's Office software is ubiquitous, highly extensible, and mostly underused. Find out how to capitalize on these features using Visual Studio Tools for Office and start extending Office into new and more productive areas.

Creating Outlook Objects
After you complete the loop through the table, if the trigger conditions were exceeded, the strMessage StringBuilder will not be empty, in which case, you'll want to add a new calendar item to Outlook for the following day at 8:30 AM.

Here's the code that does this:

   if (strMessage.Length > 0)
      Outlook.AppointmentItem ai = (Outlook.AppointmentItem)
      ai.Subject = "STOCK ALERT: CALL BROKER";
      ai.Body = strMessage.ToString();
      DateTime dDay = DateTime.Now.AddDays(1).Date;
      DateTime dCalEntry = 
         new DateTime(dDay.Year, dDay.Month, dDay.Day, 
         8, 30, 0);
      ai.Start = dCalEntry;
      ai.Duration = 15;
When using an Outlook add-in project type, you can use the this.CreateItem method to create Outlook objects. VSTO IntelliSense allows you to specify the constructor that defines the type to use. In this case you are creating an appointment item, and thus use the olAppointmentItem to construct it. You also have to do the appropriate cast to an Outlook.AppointmentItem to get access to the relevant methods and properties at design time.

You then set the Subject property of the appointment item to an appropriate subject, and the Body to the string you built earlier during the loop through the table.

To set up the appointment for a fixed time one day hence, you can use the AddDays function on the date returned by the DateTime.Now method to get the appropriate day as a DateTime object.

   DateTime dDay = DateTime.Now.AddDays(1).Date;
Then you create a new DateTime object with the year, month and day values for that date, and fix the hours, minutes and seconds to the specific time that you want.

   DateTime dCalEntry = new DateTime(
      dDay.Year, dDay.Month, dDay.Day, 8, 30, 0);
You can then assign this DateTime as the start point for your appointment. The Duration property allows you to specify the appointment duration (in minutes); in this case the code above sets it to 15 minutes.

You can choose to display the appointment using the Display method, but here it's set to false, because no user interaction is required to put the appointment in your calendar. Finally the code calls the AppointmentItem.Close method, setting the close type argument to the olSave constant, which closes and saves the appointment to your calendar.

Now, whenever you launch Outlook, the add-in checks your stocks and if necessary, sets up an appointment to call your broker in your Outlook Calendar. You can see this in Figure 2, which shows the day view, and Figure 3, which shows the appointment details, including the metadata around the event itself, which you'll need when you call your broker.

Figure 2. Outlook Calendar with Appointment Added: Here's an appointment added automatically by the new Outlook add-in.
Figure 3. Appointment Details: The new appointment contains specific details about the alert, set by the add-in when it created the appointment.
In this article you built a prototype for a real-world scenario that demonstrates the power of the Office platform for building and running applications. It makes far more sense to capitalize on the untapped power of Office already on many users' desktops instead of replicating it in your own applications. As an example, you built an alerting system using the Visual Studio Tools for Office for non critical alerts (i.e. ones that do not need to be immediately addressed). By using the Outlook's calendar's "Appointment Item" functionality, you were able to reduce the amount of work a user needs to do to process an alert.

Of course, you've barely scratched the surface of what Office and VSTO can do, but as you can see, they put very powerful tools at your disposal. The only limit is your imagination!

Laurence Moroney is a freelance enterprise architect who specializes in designing and implementing service-oriented applications and environments using .NET, J2EE, or (preferably) both. He has authored books on .NET and Web services security, and more than 30 professional articles. A former Wall Street architect, and security analyst, he also dabbles in journalism, reporting for professional sports. You can find his blog at http://www.philotic.com/blog.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date