Subscribe to Windows IT Pro
July 17, 2001 12:00 AM

Back to Basics

Windows IT Pro
InstantDoc ID #21522
Rating: (8)
Fundamentals for writing VBA for Outlook macros

My columns since January 2000 about writing practical Visual Basic for Applications (VBA) code for Microsoft Outlook 2000 have covered a lot of ground: automatic message processing, dates and tasks, handling recipients, and many more subjects. Some readers have recently written that they're just getting started with Outlook VBA, though, so this month I want to review some basics.

A little history first: Outlook 2000 is the first version of Outlook to let you write VBA macros and run them inside the program. To control Outlook 98 and Outlook 97 with VBA, you had to write a macro in some other VBA environment, such as Microsoft Word or Microsoft Excel, then run the macro from that program. Outlook 98 and Outlook 97 also didn't include the application-level events that let you run code when you perform common tasks such as starting Outlook, sending a message, or switching between folders. Although Outlook 98 introduced a customizable toolbar, it didn't let you create a toolbar button to run your own code.

Outlook 2000, therefore, is a real breakthrough for power users, administrators, and developers who want to customize the program. Just as the addition of a macro language to Excel helped turn it into the tool of choice for financial analysis, the ability to program so many aspects of Outlook has strengthened its popularity. Software developers have used the new capabilities in Outlook 2000 to produce programs to back up Outlook data, automatically compress attachments in outgoing messages, improve Outlook's handling of mail sent on behalf of other users, work with other users' Calendar folders, and synchronize Microsoft Exchange Server mailbox data to public folders. Individual users use VBA code to refile incoming messages, create related items, perform custom printouts, and customize the way Outlook handles outgoing items and reminders. Outlook 2002 extends VBA to add new objects, properties, and methods that we'll explore in future columns.

If Outlook's programmability raises a red flag, keep in mind that Outlook-related email viruses don't use VBA but rather VBScript, which isn't a secure programming language. You can't digitally sign VBScript code, but you can digitally sign VBA projects and set macro security to let only signed code run. At the conclusion of this column, I show you how to secure your VBA code.

The VBA Editor
Outlook has the same VBA editor as other Microsoft Office programs, so if you've ever worked with Word or Excel macros, the editor should look familiar. To open the editor, which Figure 1, page 88, shows, click Tools, Macro, Visual Basic Editor from Outlook's main menu, no matter what folder you're in. Or you can press Alt+F11.

The biggest difference between VBA for Word or Excel and VBA for Outlook is that the latter includes no macro recorder. You must provide all the code. Fortunately, Web sites and several active discussion forums offer code fragments that you can use in your applications. I maintain an extensive collection of links to Outlook VBA code and other resources at http://www.slipstick.com/dev/vb.htm. You can also download code samples from earlier Outlook VBA on Demand articles at Windows 2000 Magazine's Web site (the first Outlook VBA on Demand column is at http://www.win2000mag.com, InstantDoc ID 7677).

If you download a code sample that comes as a .bas file, you can click File, Import File to import the file into VBA. The import facility puts the code in a new module under Project1.Modules.

If the code needs to run in the built-in ThisOutlookSession module to take advantage of application events, copy it from the imported module into the ThisOutlookSession module, which you'll find under Project1.Microsoft OutlookObjects. Restart Outlook to ensure that the macros in ThisOutlookSession that use application events run automatically.

If you write a new macro that you plan to run from a toolbar button, or copy such a macro from a discussion list, you can either put it in the built-in ThisOutlookSession module or create a new code module with the Insert, Module command. I like to organize modules by topic so that I have all code that deals with task items in one module, code for working with folders in another, and so on. If you collect related macros in one module, you can easily make a backup copy of that module by using the File, Export command to save the module as a .bas file.

If you want to share your macros with other people, you can give them the exported .bas file. They can then import the file into their own Outlook VBA project. If you want to give someone all your Outlook VBA code, you can give him or her a copy of your vbaproject.otm file, which contains all the code that you see in Outlook VBA. You can't back up or copy this file while Outlook is running.

Code on Demand
You can use Outlook VBA to write two types of code. One type of code runs automatically from the ThisOutlookSession module. You can find many examples of this type of code in earlier Outlook VBA on Demand columns. The other type of code runs on demand when you click a button that you've added to the toolbar or press Alt+F8 to select a macro from the Macros dialog box.

Not every procedure you write is eligible for a toolbar button. To be able to run a procedure as a macro from the toolbar or the Macros dialog box, you must ensure that it meets these criteria:

  • The macro is a subroutine, not a function.
  • The macro is a Public subroutine. (Public is the default for subroutines that you don't label Public or Private.)
  • The macro doesn't have any arguments.

Related Content:

ARTICLE TOOLS

Comments
  • Anonymous User
    7 years ago
    Jul 14, 2005

    The code above for getting the output of a webpage is excelent... Thanks you.

  • Anonymous User
    7 years ago
    Apr 08, 2005

    I am trying to catch an email when it arrives into Outlook 2002 and then parse the email and save the data (From, To, Subject, etc.) into a SQL Server 2000 DB and am having trouble.

  • Anonymous User
    7 years ago
    Apr 08, 2005

    I am importing the email details from Microsoft Office Outlook 2003. But every time a detail is used from my messages in the inbox, it warns me that a virus may be trying to access my messages and I have to click yes(to allow the program to access it) for every detail that I import. I've tried signing my project but still no success.

  • Anonymous User
    7 years ago
    Mar 14, 2005

    AM SENDING DATA IN OUTLOOK AND MY LANGUAGE IS VBSCRIPT. NOW AM USING THE FIELD CHOOSER TO GET THE CONTACTS BUT WHEN I TRY SENDING THE DATA, ITS TELLING ME THE DESTINATION CC,TO, IS NULL.
    PLEASE NOTE IN THIS ONE BUTTON FOR SEND ITS SAVING DATA IN ACCESS. AM JUST USING THE FIELD CHOOSER TO GET THE CONTACTS FROM THE CONTACT LIST.I DISCOVER THAT THIS CONTROL IS NOT BEING RECOGNISED

  • Anonymous User
    7 years ago
    Jan 31, 2005

    I need help please, i need to measure the response time (service level agreement) so i wish to take the time between the request and the answer. How can i do that for every client?

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.