Using Visual Basic with Outlook

Microsoft Outlook

Show All

Using Visual Basic with Outlook

You can use Visual Basic to customize and extend Microsoft Outlook. Outlook allows you to control Outlook by using Visual Basic, Visual Basic for Applications and VBScript. Which you use depends on what you want your program to do.

Visual Basic is a full-featured programming language you can use to create stand-alone applications or dynamic-link libraries (DLLs) that extend other applications. Visual Basic for Applications is a subset of Visual Basic that is run within an application to extend its capabilities. VBScript is a simplified version of Visual Basic for Applications and is run within an Outlook item. In all cases, these programming languages control Outlook through its object model.

Learn about the Outlook object model.

If you want to create a separate application that accesses data stored by Outlook and uses Outlook to send and receive messages, use Visual Basic to create the application (you can also use other programming languages, such as C++, to control Outlook through its object model). You can also use Visual Basic to create a DLL that can extend Outlook as a COM add-in.

You use Visual Basic for Applications in one of two ways: You can use Visual Basic for Applications in other applications (such as Microsoft Excel or Microsoft Word) to automate Outlook, or you can use Visual Basic for Applications within Outlook to control Outlook. If you expect your users to be using another application most of the time, and you want to give them the ability to send a message using Outlook or to access information stored by Outlook, write Visual Basic for Applications programs in that application that control Outlook through the Outlook object model. If, on the other hand, you want to write Visual Basic code that customizes how Outlook works (like a macro), use Visual Basic for Applications within Outlook.

You can extend the functionality of Outlook forms by using VBScript. VBScript programs are stored within a form. Because the program code is contained within the form, it can be sent with an item to another user. An important consideration in choosing which kind of the Visual Basic programming language you will use is the type of events you want your program to respond to. Because VBScript code is associated with a particular item, code that responds to events in specific items (such as when a particular item is opened or a value in a field is changed) is easiest to write using VBScript. If, on the other hand, you want your program to respond to events that occur in the application, in Windows Explorer, in folders, or in all items, then you should write your program using Visual Basic or Visual Basic for Applications.

Code written for Visual Basic or Visual Basic for Applications often does not work in VBScript without modification. For example, you must replace all built-in constants written in Visual Basic for Applications with the literal numeric values of those constants in VBScript. And VBScript uses only the Variant data type.

Learn about constants and variables in VBScript.

In Outlook Visual Basic for Applications and VBScript, when you reference the Application object to use CreateObject or GetObject, you simply use Application. For example, the following code displays the Tasks folder:

Set olMAPI = Application.GetNameSpace("MAPI")
olMAPI.GetDefaultFolder(13).Display

In Visual Basic or Visual Basic for Applications in other applications, you must explicitly create the Application object:

Set myOlApp = CreateObject("Outlook.Application")
Set olMAPI = myOlApp.GetNameSpace("MAPI")
olMAPI.GetDefaultFolder(olFolderTasks).Display