Converting an Outlook Macro to a VSTO Addin

In this section you will create a simple VBA macro for Outlook and convert the code to a VSTO add-in. The VBA code is in the ItemSend event handler of the Application object in a VBA project, as shown in Listing 10.1.

Listing 10.1. VBA macro that checks for a subject on an outgoing e-mail

Private Sub Application_ItemSend(ByVal Item As Object, _ Cancel As Boolean)

Dim sendMessage As Integer

If Item.Class = olMail Then If Item.Subject = "" Then sendMessage = MsgBox("Subject text is missing." & _ vbCrLf & vbCrLf & "Do you want to send this" & _ " message?", vbYesNo, "Missing Subject")

If sendMessage = vbNo Then Cancel = True Item.Display End If End If End If End Sub

To convert this to a VSTO add-in, you'll first create an add-in project, as described earlier in this chapter.

1. Create an Outlook add-in project with VSTO.

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

3. In the Class Name drop-down, select (ThisApplication Events), and then select ItemSend in the MethodName drop-down, as shown in Figure 10.6.

Figure 10.6. ItemSend event handler

4. Add the code in Listing 10.2 to the ItemSend event handler. Note that the only change necessary to the code is displayed in bold font.

Listing 10.2. VSTO add-in that checks for a subject on an outgoing e-mail

Private Sub ThisApplication_ItemSend(ByVal Item As Object, _ ByRef Cancel As Boolean) Handles Me.ItemSend

Dim sendMessage As Integer

If Item.Class = Outlook.OlObjectClass.olMail Then If Item.Subject = "" Then sendMessage = MsgBox("Subject text is missing." & _ vbCrLf & vbCrLf & "Do you want to send " _ & "this message?", vbYesNo, "Missing Subject") If sendMessage = vbNo Then Cancel = True Item.Display() End If End If End If End Sub

5. Press F5 to run the code.

6. Create an e-mail without filling in the Subject text box, and then click Send. The message box (indicating that there's no Subject text) should appear and give you the opportunity to add the text or send the message as is.

You can also make further modifications to the code to use some of the features of Visual Basic 2005, as shown in Listing 10.3.

Listing 10.3. Further modifying the VSTO add-in code

Private Sub ThisApplication_ItemSend(ByVal Item As Object, _ ByRef Cancel As Boolean) Handles Me.ItemSend

Dim myMailItem As Outlook.MailItem = _ TryCast(Item, Outlook.MailItem)

If myMailItem IsNot Nothing AndAlso myMailItem.Subject = _ String.Empty Then

If MsgBox("Subject text is missing." & vbCrLf & _ vbCrLf & "Do you want to send this message?", _ MsgBoxStyle.YesNo, "Missing Subject") = _ MsgBoxResult.No Then

Cancel = True myMailItem.Display()

End If End If End Sub

Here, we cast the Item (which is of type Object) to a MailItem. We used TryCast because if the attempt to cast the object fails, TryCast returns Nothing instead of an InvalidCastException error that we'd have to handle. With TryCast you can test to be sure that the object is not Nothing before performing additional operations on it.

The code also uses AndAlso to first check whether myMailItem is Nothing and, if it isn't, whether the Subject property is empty; only then does it execute the remainder of the code. Instead of using nested If statements or using the And operator (which would always check both conditions), we use AndAlso, which checks the second condition only if the first condition is True, thus preventing the exception that would otherwise occur. Finally, the code checks the DialogResult of the message box directly. In this way, we don't need to create a separate variable for an integer that represents the VBA constants vbNo and vbYes.

0 0

Post a comment