Microsoft Corporation
May 5, 1997
Introduction
Choosing Between VBScript and Visual Basic for Your Outlook Solution
Differences Between VBScript and VBA
Using VBScript to Program Outlook Objects from Outlook
Using Visual Basic to Automate Outlook from Other Applications
Automating Other Applications from Outlook
The first part of this article describes the differences between using Microsoft® Visual Basic® Scripting Edition (VBScript) code to automate Microsoft Outlook™ objects from within Outlook, and using Microsoft Visual Basic for Applications (VBA) to automate Outlook from another Microsoft Office 97 application. The second part of the article describes how to automate other Office 97 applications from within Outlook.
Both VBScript and VBA are members of the Visual Basic language family. VBScript is a subset of VBA that you can use within Outlook. VBA is the programming language that you can use within the other Office 97 applications. All VBScript code will run in VBA, but all VBA code will not run in VBScript.
You can use either VBScript or VBA to program Outlook objects, but keep the following points in mind when you are choosing where to write your code:
For example, if your solution is primarily a Microsoft Excel solution that uses some of the functionality of Outlook, write VBA code in Microsoft Excel to automate Excel and Outlook. If your solution is primarily an Outlook solution that uses some of the functionality of Microsoft Excel, write VBScript code in Outlook to automate Outlook and Excel.
The following table summarizes the differences between writing code to control Outlook in VBScript and writing code to control Outlook in Visual Basic for Applications. For more information, see "Microsoft Outlook Objects" (MSDN Library, Microsoft Office Development), or read "Microsoft Visual Basic Scripting Edition: Frequently Asked Questions."
VBA | VBScript |
You can use all the built-in constants that come with a type library. | You must use the numeric equivalents for the built-in type-library constants. |
You can use sophisticated programming structures such as With . . . End With and For Each . . . Next. | Some sophisticated programming structures are not available. |
You must establish a reference to the Outlook object library and then use CreateObject, GetObject, or Set and New to get a reference to the Outlook Application object. From there you can drill down to objects that represent other Outlook elements, such as folders and items. | You use the Application property to return a reference to the Outlook Application object. To do this, use an Item within an event procedure to return a reference to the object that represents the Outlook item where the event was triggered. |
You can't write event procedures that respond to Outlook items and control events. | All the code you write should be in either an Outlook item or control event procedure, or in a standard procedure that is called from an event procedure. |
You can call procedures written in Visual Basic in many ways, including clicking command bar buttons or commands, using shortcut keys, using the Macros dialog box, or triggering events that have event procedures associated with them. | Procedures written in VBScript are only called in response to events that have event procedures associated with them. This is no other way to run these procedures. |
Code is written in a module in the Visual Basic Editor | Code is written in the Script Editor. |
To automate Outlook objects using VBScript code that runs from Outlook itself, you add event procedures and standard procedures to your custom forms. An event procedure can be triggered by events on either the item created from the form or on controls on the item.
Note In Outlook, code runs only in response to events. Therefore, you can write Sub and Function procedures that don't respond to events, but there is no way to call these procedures directly. The only way to run a procedure that isn't associated with an event is to call it from a valid event procedure.
In form event procedures, you use Item to refer to the item or form where the event occurred. This means that all form event procedure names are composed of the word "Item" followed by an underscore character (_) and the name of the event (for example, Item_Open). In addition, within the event procedure, you use Item to refer to the object that represents the Outlook item where the event occurred.
The following example adds the date and time that the Outlook item was opened to the end of the item's subject line.
Function Item_Open()
Item.Subject = Item.Subject & " [opened " & Now & "]"
End Function
To add an empty form event procedure to your script in the Script Editor, on the Script menu click Event, then click an event name in the list, and then click Add. The appropriate Sub . . . End Sub or Function . . . End Function statement is inserted with its arguments (if any) specified.
To create a Click event procedure for a control on an item, add a procedure whose name is composed of the name of the control followed by an underscore character (_) and the word "Click" to your code in the Script Editor. You must type the complete Sub . . . End Sub statement.
The following procedure disables the ReplyAll function for an item when the button named cmdNoReplyAll is clicked.
Sub cmdNoReplyAll_Open()
Item.Actions("Reply to All").Enabled = False
End Function
Unlike Visual Basic, VBScript does not support the built-in constants supplied by the Outlook type library. Therefore, when you automate Outlook by using VBScript, you must use the numeric values that the built-in constants represent.
If you write your Outlook code in one of the other Office 97 applications to take advantage of the development environment, be sure to replace the constants that are automatically supplied for you with their numeric equivalents when you paste the code into the Script Editor. You can look up the numeric values of built-in Outlook constants using the Object browser, or you can consult the following tables. These tables can also be found under "Microsoft Outlook Constants" in Microsoft Outlook Visual Basic Help.
OlActionCopyLike
Constant | Value |
olReply | 0 |
olReplyAll | 1 |
olForward | 2 |
olReplyFolder | 3 |
olRespond | 4 |
OlActionReplyStyle
Constant | Value |
olOmitOriginalText | 0 |
olEmbedOriginalItem | 1 |
olIncludeOriginalText | 2 |
olIndentOriginalText | 3 |
OlActionResponseStyle
Constant | Value |
olOpen | 0 |
olSend | 1 |
olPrompt | 2 |
OlActionShowOn
Constant | Value |
olDontShow | 0 |
olMenu | 1 |
olMenuAndToolbar | 2 |
OlAttachmentType
Constant | Value |
olByValue | 1 |
olByReference | 4 |
olEmbeddedItem | 5 |
olOLE | 6 |
OlBusyStatus
Constant | Value |
olFree | 0 |
olTentative | 1 |
olBusy | 2 |
olOutOfOffice | 3 |
OlDaysOfWeek
Constant | Value |
olSunday | 1 |
olMonday | 2 |
olTuesday | 4 |
olWednesday | 8 |
olThursday | 16 |
olFriday | 32 |
olSaturday | 64 |
OlDefaultFolders
Constant | Value |
olFolderDeletedItems | 3 |
olFolderOutbox | 4 |
olFolderSentMail | 5 |
olFolderInbox | 6 |
olFolderCalendar | 9 |
olFolderContacts | 10 |
olFolderJournal | 11 |
olFolderNotes | 12 |
olFolderTasks | 13 |
OlFlagStatus
Constant | Value |
olNoFlag | 0 |
olFlagComplete | 1 |
olFlagMarked | 2 |
OlFolderDisplayMode
Constant | Value |
olFolderDisplayNormal | 0 |
olFolderDisplayFolderOnly | 1 |
olFolderDisplayNoNavigation | 2 |
OlFormRegistry
Constant | Value |
olDefaultRegistry | 0 |
olPersonalRegistry | 2 |
olFolderRegistry | 3 |
olOrganizationRegistry | 4 |
OlGender
Constant | Value |
olUnspecified | 0 |
olFemale | 1 |
olMale | 2 |
OlImportance
Constant | Value |
olImportanceLow | 0 |
olImportanceNormal | 1 |
olImportanceHigh | 2 |
OlInspectorClose
Constant | Value |
olSave | 0 |
olDiscard | 1 |
olPromptForSave | 2 |
OlItems
Constant | Value |
olMailItem | 0 |
olAppointmentItem | 1 |
olContactItem | 2 |
olTaskItem | 3 |
olJournalItem | 4 |
olNoteItem | 5 |
olPostItem | 6 |
OlJournalRecipientType
Constant | Value |
olAssociatedContact | 1 |
OlMailingAddress
Constant | Value |
olNone | 0 |
olHome | 1 |
olBusiness | 2 |
olOther | 3 |
OlMailRecipientType
Constant | Value |
olOriginator | 0 |
olTo | 1 |
olCC | 2 |
olBCC | 3 |
OlMeetingRecipientType
Constant | Value |
olOrganizer | 0 |
olRequired | 1 |
olOptional | 2 |
olResource | 3 |
OlMeetingResponse
Constant | Value |
olMeetingTentative | 2 |
olMeetingAccepted | 3 |
olMeetingDeclined | 4 |
OlMeetingStatus
Constant | Value |
olNonMeeting | 0 |
olMeeting | 1 |
olMeetingReceived | 3 |
olMeetingCanceled | 5 |
OlNoteColor
Constant | Value |
olBlue | 0 |
olGreen | 1 |
olPink | 2 |
olYellow | 3 |
olWhite | 4 |
OlRecurrenceType
Constant | Value |
olRecursDaily | 0 |
olRecursWeekly | 1 |
olRecursMonthly | 2 |
olRecursMonthNth | 3 |
olRecursYearly | 5 |
olRecursYearNth | 6 |
OlRemoteStatus
Constant | Value |
olRemoteStatusNone | 0 |
olUnMarked | 1 |
olMarkedForDownload | 2 |
olMarkedForCopy | 3 |
olMarkedForDelete | 4 |
OlResponseStatus
Constant | Value |
olResponseNone | 0 |
olResponseOrganized | 1 |
olResponseTentative | 2 |
olResponseAccepted | 3 |
olResponseDeclined | 4 |
olResponseNotResponded | 5 |
OlSaveAsType
Constant | Value |
olTXT | 0 |
olRTF | 1 |
olTemplate | 2 |
olMSG | 3 |
olDoc | 4 |
OlSensitivity
Constant | Value |
olNormal | 0 |
olPersonal | 1 |
olPrivate | 2 |
olConfidential | 3 |
OlTaskDelegationState
Constant | Value |
olTaskNotDelegated | 0 |
olTaskDelegationUnknown | 1 |
olTaskDelegationAccepted | 2 |
olTaskDelegationDeclined | 3 |
OlTaskOwnership
Constant | Value |
olNewTask | 0 |
olDelegatedTask | 1 |
olOwnTask | 2 |
OlTaskRecipientType
Constant | Value |
olUpdate | 1 |
olFinalStatus | 2 |
OlTaskResponse
Constant | Value |
olTaskSimple | 0 |
olTaskAssign | 1 |
olTaskAccept | 2 |
olTaskDecline | 3 |
OlTaskStatus
Constant | Value |
olTaskNotStarted | 0 |
olTaskInProgress | 1 |
olTaskComplete | 2 |
olTaskWaiting | 3 |
olTaskDeferred | 4 |
OlTrackingStatus
Constant | Value |
olTrackingNone | 0 |
olTrackingDelivered | 1 |
olTrackingNotDelivered | 2 |
olTrackingNotRead | 3 |
olTrackingRecallFailure | 4 |
olTrackingRecallSuccess | 5 |
olTrackingRead | 6 |
olTrackingReplied | 7 |
OlUserPropertyType
Constant | Value |
olText | 1 |
olNumber | 3 |
olDateTime | 5 |
olYesNo | 6 |
olDuration | 7 |
olKeywords | 11 |
olPercent | 12 |
olCurrency | 14 |
olFormula | 18 |
olCombination | 19 |
To control Outlook objects from outside Outlook, you must establish a reference to the Outlook object library from the project in which you are writing your code. To do this, use the References dialog box on the Tools menu in the Visual Basic Editor. Then write code that returns a reference to the Outlook Application object. Through this reference, your code has access to all the objects, properties, methods, and constants defined in the Outlook type library.
There are several ways to return a reference to the Outlook Application object.
You can use the CreateObject function to start a new session of Outlook and return a reference to the Application object that represents the new session.
You can use the GetObject function to return a reference to the Application object that represents a session that's already running.
You can use the New keyword in several types of statements to implicitly create a new instance of the Outlook Application object. You can use the New keyword with the Set statement to set an object variable to the new instance of the Application object. You can also use the New keyword with the Dim, Private, Public, or Static statement to declare an object variable. (The new instance of the Application object is created on the first reference to the variable.)
The following example, run from Microsoft Excel, sets the myOlApp object variable to a new instance of the Outlook Application object using the Set statement and CreateObject function. The code then uses the CreateItem method of the Application object within a For Each . . . Next loop to create one ContactItem object for each row in the specified range on the Contacts worksheet and to set properties for the item, using values from cells in the row.
Sub WriteContactInfo()
Dim myOlApp As Outlook.Application
Dim w As Workbook
Dim s As Worksheet
Dim c As Range
Set s = Worksheets("Contacts")
Set myOlApp = CreateObject("Outlook.Application")
For i = 1 To s.Range("a1").CurrentRegion.Rows.Count
Set c = s.Cells(i, 1)
Set myItem = myOlApp.CreateItem(olContactItem)
With myItem
.FullName = c.Value
.Birthday = c.Offset(0, 1).Value
.Children = c.Offset(0, 2).Value
.Spouse = c.Offset(0, 4).Value
.Save
End With
Next
End Sub
The following example, run from Microsoft Access, sets the myOlApp object variable to an instance of the Outlook Application object using the Set statement and the New keyword. The rest of the code goes to the Contacts folder, adds a new item, and sets the item's properties to values in a Microsoft Access form.
Sub AddNewContact
Set myOlApp = New Outlook.Application
Set myOlNameSpace = myOlApp.GetNamespace("MAPI")
Set objFolder = myOlNameSpace.GetDefaultFolder(olFolderContacts)
Set objForm = Forms!Customers
Set objNewContact = objFolder.Items.Add
With objNewContact
Set prpUserProp = .UserProperties.Add("Priority", olText)
prpUserProp.Value = objForm!Priority
.FirstName = Left(objForm!ContactName, InStr(objForm!ContactName, " ") - 1)
.LastName = Mid(objForm!ContactName, InStr(objForm!ContactName, " ") + 1)
.CompanyName = Nz(objForm!CompanyName, "")
.JobTitle = Nz(objForm!ContactTitle, "")
.Save
End With
End Sub
To automate other applications using Outlook VBScript code, use the CreateObject method of the Application object. The following VBScript example, which is run by clicking the cmdToXL button on an item, reads information about the current item and writes it to a Microsoft Excel worksheet. The form from which the current item was created must be based on the Contact item form.
Sub cmdToXL_click()
Set myXLApp = Item.Application.CreateObject("Excel.Application")
myXLApp .Visible = True
Set c = myXLApp.Workbooks.Add.Worksheets(1).Range("a1")
c.Value = Item.FullName
c.Offset(0, 1).Value = Item.HomeAddressStreet
c.Offset(0, 2).Value = Item.HomeAddressCity
c.Offset(0, 3).Value = Item.HomeAddressState
c.Offset(0, 4).Value = Item.HomeAddressPostalCode
End Sub
This document is for informational purposes only.