Can I add a Clear Flag button to the Outlook toolbar? I know that this option exists on the right-click context menu, but I'd like to eliminate a step.
I'm not sure why Microsoft chose not to make the Clear Flag command available for toolbar customization, but you can use an Outlook Visual Basic for Applications (VBA) macro to insert the command. First, take the code for the ClearFlag subroutine that Listing 1 shows and place it in the built-in ThisOutlookSession module. (Press Alt+F11 to open the VBA development environment.) Then, to add the ClearFlag procedure to a toolbar, choose View, Toolbars, Customize. Next, from the Customize dialog box, click the Command tab and drag the Project1.ClearFlag command from the Macros list to any toolbar. You can then right-click the new toolbar button to edit the name or add an icon.
If you're new to Outlook VBA, you should also check your macro security setting. The most recent versions of Outlook set macro security to High, which means your macro won't run unless you digitally sign your VBA code project. Click Tools, Macro, Security to set security to Medium, then restart Outlook. Under the Medium setting, Outlook will prompt you to enable macros when Outlook starts, so you'll always know that VBA is active.
The ClearFlag subroutine in Listing 1 shows some VBA basics and techniques that you might find useful for other macros:
- ActiveExplorer.Selection is a collection of all the items a user has selected in the current folder.
- Whenever you use a For Each...Next loop to iterate the Outlook items in a folder or in a collection, check the Class property of each item before you use any properties or methods specific to a particular Outlook item type. For example, if a user had selected a meeting request and your code tries to set the FlagDueBy property, VBA would generate an error message because a meeting request has no such property.
- The olNoFlag constant is one of many of Outlook's built-in constants. The VBA environment is very helpful about prompting you when you can use one of these constants to complete a code statement.
- The odd expression #1/1/4501# represents the date January 1, 4501, which is the date that Outlook stores for the value that you see in the UI as None.