Article ID: 181202
Article Last Modified on 8/28/2007
Dim objOL as Object
Set objOL = CreateObject("Outlook.Application")
To use early binding, you first need to reference the available Outlook
object library. To do this from Visual Basic (VB) or Visual Basic for
Applications, follow these steps:
Set ol = New Outlook.ApplicationUsing early binding has two important advantages. First, code using early binding runs faster than code using late binding (CreateObject/GetObject). Second, because you reference the Outlook object library, you can get online Outlook programming help using the object browser and Help system.
182394 OL98: How to Use Command Bars in Outlook Solutions
Set ol = New Outlook.Application
Set olns = ol.GetNamespace("MAPI")
Once you set the Namespace object, you are ready to set the next object to
a folder within the MAPI Namespace. One common way of doing this is by
specifying Outlook's "default" folders, which are the folders at the same
folder level as the Inbox that receives incoming mail. The following code
will set the objFolder object to the default Contacts folder:
Set ol = New Outlook.Application
Set olns = ol.GetNamespace("MAPI")
Set objFolder = olns.GetDefaultFolder(olFolderContacts)
For more information about referencing folders and items, please see
the following article in the Microsoft Knowledge Base:
182614 OL98: Programming Examples for Referencing Items and Folders
Sub CreateNewDefaultOutlookTask()
Dim ol As Object
Dim NewTask As Object
' Set the Application object.
Set ol = New Outlook.Application
' You can only use CreateItem for default items.
' Vbaoutl.hlp lists other Outlook constants to create other items.
Set NewTask = ol.CreateItem(olTaskItem)
' Display the new task form so the user can fill it out.
NewTask.Display
End Sub
Create a New Contact Using a Custom Form:
Sub CreateNewContactFromCustomForm()
Dim ol As Object
Dim olns As Object
Dim objFolder As Object
Dim AllContacts As Object
Dim NewContact As Object
' Set the Application object.
Set ol = New Outlook.Application
' Set the Namespace object.
Set olns = ol.GetNamespace("MAPI")
' Set the default Contacts folder.
Set objFolder = olns.GetDefaultFolder(olFolderContacts)
' Set objAllContacts equal to the collection of all contacts.
Set AllContacts = objFolder.Items
' Add a new contact to the AllContacts collection using the
' "IPM.Contact.MyForm" form.
Set NewContact = AllContacts.Add("IPM.Contact.MyForm")
' Display the new contact form.
NewContact.Display
End Sub
Sample Subroutine to Loop Through All the Default Contacts:
Sub GetOutlookContacts()
Dim ol As Object
Dim olns As Object
Dim objFolder As Object
Dim objAllContacts As Object
Dim Contact As Object
' Set the Application object.
Set ol = New Outlook.Application
' Set the Namespace object.
Set olns = ol.GetNamespace("MAPI")
' Set the default Contacts folder.
Set objFolder = olns.GetDefaultFolder(olFolderContacts)
' Set objAllContacts equal to the collection of all contacts.
Set objAllContacts = objFolder.Items
' Loop through each contact.
For Each Contact In objAllContacts
' Display the Fullname field for the contact.
MsgBox Contact.FullName
Next
End Sub
Additional query words: OutSol OutSol98
Keywords: kbdtacode kbhowto kbprogramming kbusage KB181202