Article ID: 168095
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, on the Visual Basic Editor Tools menu, click References.
Select "Microsoft Outlook 8.0 Object Library" from the list and click OK.
The object library file is Msoutl8.olb and by default installs to the
C:\Program Files\Microsoft Office\Office folder.
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 on- line Outlook programming help using the object browser and Help system.
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)
Once you are programmatically at the folder that contains the items you
wish to either use or create, you must use appropriate code to accomplish
your programming task. See the section below for some common programming
examples.
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
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 = the collection of all contacts
Set AllContacts = objFolder.Items
' Add a new contact 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
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 = 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
166738 OL97: How to Install Visual Basic Help
166368 OL97: How To Get Help Programming with Outlook
Additional query words: OutSol OutSol97
Keywords: kbdswnet2003swept kbcode kbhowto kbprogramming KB168095