Creating a Simple Addin

You can also have code run as soon as the Outlook application opens and the add-in is loaded. To do this, you add code to the Startup event handler of ThisApplication.

1. Right-click ThisApplication in Solution Explorer, and click View Code.

2. Add the CreateTask method in Listing 10.4 to the ThisApplication class, and then add a call to CreateTask in the Startup event handler.

Listing 10.4. Adding code to the Startup event handler of an Outlook add-in

Private Sub ThisApplication_Startup(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Startup CreateTask() End Sub

Private Sub CreateTask()

Dim NewTask As Outlook.TaskItem = TryCast( _

Me.CreateItem(Outlook.OlItemType.olTaskItem), _ Outlook.TaskItem)

If NewTask IsNot Nothing Then

With NewTask

.Subject = "My new task" .DueDate = System.DateTime.Now

.Body = "This is my new Task which is due right now!" .Status = Outlook.OlTaskStatus.olTaskInProgress .Display() End With End If End Sub

3. Add the code in Listing 10.5 to the Shutdown event handler of ThisApplication.

Listing 10.5. Adding code to the Shutdown event handler of an Outlook add-in

Private Sub ThisApplication_Shutdown(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Shutdown

MsgBox("Outlook is closing. No, really!")

End Sub

4. Press F5 to run the code.

When Outlook opens, the Outlook task is created and displayed, as shown in Figure 10.7.

Figure 10.7. Creating an Outlook task

You can then click Save And Close to save the task in Outlook and close the Task item. When you close Outlook, the Shutdown event is raised and the message box in Figure 10.8 is displayed.

Figure 10.8. Shutdown event handler

We chose this text for the message box because one of the issues that VSTO Outlook add-ins have resolved is the problem with Outlook not properly releasing references to Outlook objects in managed shared add-ins.

0 0

Post a comment