Chapter 28. Automating Common Tasks

Understanding Automation Options 715

Using Macros 716

Understanding Macros 716

Setting Macro Security 725

MICROSOFT Outlook 2010 is a feature-rich product and, as such, has an option, a wizard, or a graphical tool for accomplishing nearly anything that you require from a personal information manager. If something does come up that the folks at Microsoft haven’t planned for, however, you also have the option of customizing Outlook 2010 by using its built-in support for Microsoft Visual Basic code additions. Through the use of flexible Microsoft Visual Basic for Applications (VBA) scripting options and built-in security controls, you can simplify and automate common tasks.

In this chapter, you’ll learn how to create and use a macro. This includes creating the macro, stepping through a macro to test it, and deleting macros you no longer need. In addition, you’ll find out about implementing security options for macros.

Note

See Chapter 11, for a discussion of Quick Steps, which enable you to process messages automatically.

Outlook 2010 has a number of built-in automation options that allow the application to perform certain tasks for you. For example, the Rules Wizard automatically moves, copies, and forwards email messages. The Out Of Office Assistant acts as an answering service when you’re away.

If a built-in option can accomplish the automated task that you require, it should be your first choice. By using a built-in option instead of a custom one, you minimize problems that can occur if you need to reinstall Outlook 2010 or use Outlook 2010 on multiple machines. Using standardized options also guards against compatibility problems with upgrades to Outlook 2010.

If none of the automation options does the trick, however, you can accomplish just about any customization by using VBA. This chapter focuses on the use of VBA procedures known as macros to automate common tasks.

So just what is a macro? In general terms, a macro is a number of commands grouped together to execute a particular task. Macros are like small programs that operate within other programs. Macros have been around for a long time, and all Microsoft Office 2010 system products support them at some level. In Outlook 2010, macros are implemented as VBA procedures that are not linked to a particular form and are available from anywhere in Outlook 2010. In Outlook 2010, you manage macros by using commands in the Code area on the Developer tab. To display the Developer tab, right-click the ribbon and choose Customize The Ribbon. In the Outlook Options dialog box, place a check beside Developer in the tab list on the right and click OK.

Macros are most useful for tasks that must be performed repeatedly without change. Even so, because a macro contains Visual Basic code, it can be flexible and can respond to variables or user input. With the power of scripting, a macro can accomplish a task in the most efficient way in response to specific conditions.

The following are the three basic programming elements you can work with in an Outlook 2010 macro:

In general, a VBA macro either determines or modifies the value of an object property or calls a method. Macros, then, are nothing more than simple programs that use VBA to access or modify Outlook 2010 information.

The process for creating an Outlook 2010 macro is simple. The process for creating a useful macro, on the other hand, is more complex. In this chapter, we’ll fall back on the most basic of functions, the “Hello World” message box macro. This macro creates a function that displays a message box containing the text Hello World. Clicking OK (the only button) closes the message box and ends the macro.

To create this macro, follow these steps:

After you create a macro, you can edit it by returning to the Macros dialog box as follows:

When you’re creating a macro, it’s often helpful to step through the code, which allows you to watch each line as it is being processed and see problems as they occur. To do this, open the HelloWorldMsgBox macro for editing (as described in the preceding section), and then press F8. Alternatively, open the Macros dialog box, select a macro, and then choose Step Into. The first line of the macro is highlighted, and its code is processed. To process the next line, press F8 again.

Step through the rest of the macro using the F8 key. Notice that clicking OK merely closes the message box rather than creating the email message. This is because later steps are not followed automatically. The new email message is created only after you press F8 through the line of the subprocedure that displays it—in this case, the last line of the macro.

Macros have several advantages, including their power, their flexibility, and their ability to run automatically, even without your knowledge. These advantages have a dark side, however, and poorly written or malicious macros can do significant damage to an Outlook 2010 message store. Because of the potential danger that macros pose, the Outlook 2010 Tools menu offers four security levels for Outlook 2010 macros:

To view or change the security level, on the Developer tab, click Macro Security to open the Macro Settings page of the Trust Center, as shown in Figure 28-8. (You can also access the Trust Center by opening an Outlook 2010 item and, on the Developer tab, in the Code group, clicking Macro Security.) The default setting is Notifications For Digitally Signed Macros; All Other Macros Are Disabled, which is probably the best choice for most users.

Macro security in Outlook 2010 gives you control over when macros can run, which helps prevent malicious code from affecting the system of someone using Outlook 2010. In Outlook 2010, when macro security is set to Notifications For Digitally Signed Macros; All Other Macros Disabled, only digitally signed macros from trusted sources can run. A setting of Notifications For All Macros causes Outlook 2010 to prompt you whether to enable and allow macros to run. A setting of Enable All Macros allows all macros to run, which poses significant risks from malicious code.

If you create your own macros, you probably would like to sign your macros digitally so that they will run on other people’s computers without triggering Outlook 2010 macro security warnings. Outlook 2010, like all versions since Outlook 2000, provides the means to sign VBA projects.

To create a self-signing certificate, follow these steps:

To sign a macro, follow these steps:

On the computer that will be running the macro, verify that you have configured Outlook 2010 macro security for either the Notifications For Digitally Signed Macros; All Other Macros Disabled setting or the Notifications For All Macros setting, and then attempt to run a macro. When Outlook 2010 asks whether you want to trust the macro publisher, click Yes. Your custom macros should now run on that computer without triggering the Outlook 2010 security warnings.