Imagine the power your Visual Basic applications would have if they could handle e-mails, tasks, and other Outlook 2000 functions. Well, they can—and without much difficulty. In previous articles, we’ve looked at ways to automate a variety of Outlook chores. Now, we’ll turn our attention to advanced e-mail functions, such as creating drafts and adding attachments. We’ll also explain how to create folders, and we’ll consider some security issues.


Additional reading


E-mails—beyond the basics
When you automate Outlook, the most common action will probably be sending e-mails. So it pays to master some advanced e-mail techniques, including creating drafts and adding attachments.

Listing A contains sample code that creates a mail message with an attachment and sends it.

As with other Outlook objects, Importance property values can be High, Normal, or Low, and Sensitivity property values can be Private, Personal, Confidential, or Normal. The code in Listing A will send an e-mail message to two recipients and include two attached files. If you don’t want to include attachments, don’t call the Attachments.Add method.

To create a draft, you can use the same code, except that instead of calling the Send method, you must call the Save method to save the draft. You can make changes to it later before sending it.

To create a draft without attachments, run the code shown in Listing B.

The resulting draft, shown in Figure A, would be stored in the Drafts folder.

Figure A
Draft e-mail created from Visual Basic

Working with folders
Creating and modifying new folders, as well as adding and modifying folder items, are also common chores. If you are sending e-mails from an application, you may want to store them in a specific folder in the client’s Outlook. For example, you might create a folder with a project name and add all related items to it.

To create a new folder, you have to specify what kind of Outlook objects will be stored there. To demonstrate, we’ll create a new folder for mail items related to a project called PTS.
Dim oFolder As Variant, oFolderUrgentMail As Variant
Set oFolder = olNs.GetDefaultFolder(olFolderInbox)
Set oFolderPTS = oFolder.Folders.Add(“PTS Mail”)

This code will create a new folder under the Inbox folder called PTS Mail, into which mail items can be saved (Figure B).

Figure B
Newly created PTS Mail folder

A note on security
Over the past few years, we’ve seen a proliferation of viruses that can resend themselves to all Outlook contacts stored on a client computer. Outlook has a number of security features designed to prevent this, although they vary depending on the version of Outlook you use.

Outlook 2000 includes a variety of new security features that were not available in Outlook 98. If you open a message that is in HTML format, and the message contains a script, the script will run within the context of the Internet security settings. And if you install the  Outlook E-mail Security Update, Outlook 2000 will completely disable scripts in HTML format messages.

When you automate Outlook 2000 to send messages to users in a client’s address book, the client machine will display a dialog box telling the user that an automated program is trying to access the address book. The user will have to click a button to allow the access or to cancel the process. This way, if someone creates a program to send a virus from a client machine to everyone in the user’s address book, the user will be able to cancel the process.

Of course, regardless of the version of Outlook you are using, you need to have solid antivirus software running on your computer and on your users’ computers and to make sure that the virus definitions are updated on the regular basis to protect you from new viruses.

Outlook at your command
You can add some pretty slick functionality to your applications once you understand Microsoft Outlook 2000’s COM model and master a few Visual Basic techniques. Using the examples above, for example, you could attach a sales report to an e-mail and send it to a user. Combine that with the ability to add a task reminding the user to follow up, and you have a very handy enterprise application.

Subscribe to the Developer Insider Newsletter

From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays

Subscribe to the Developer Insider Newsletter

From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays