Cannot get Outlook application object to work

I'm trying to write a program that lists the members of an Outlook email distribution list.

I've found that VBA seems to have objects that could make this possible, but I haven't yet been able to get it to work. I've studied this Microsoft article on the Outlook Application object, this one about the GetDefaultFolder method, and many other reference materials, and have come up with the following program:

Set Application = CreateObject("Outlook.Application")
Set myNameSpace = Application.GetNameSpace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderContacts)
myFolder.Display

However, when I run this using wscript or cscript I get the following:

Microsoft VBScript runtime error: Invalid procedure call or argument: 'myNameSpace.GetDefaultFolder'

Can anyone see what the problem is?

2 Answers

The reason your code is not working it because when you are accessing a library with late binding, e.g. by using CreateObject("Outlook.Application"), the enumerations built into the library are not available.

Since you haven't used the Option Explicit directive, Excel has automatically declared olFolderContacts as a variable of type Variant with a value of Empty. That is the invalid argument that the error message is referring to.

If you wish to continue using late binding, either add the following code so the constants are available:

 'v0.1.1 ' Required if late binding Outlook ' (Note that this list excludes newer values available in Outlook 2010 or later) Private Enum OlDefaultFolders olFolderDeletedItems = ------------3 olFolderOutbox = ------------------4 olFolderSentMail = ----------------5 olFolderInbox = ------------------(6) olFolderCalendar = ----------------9 olFolderContacts = --------------(10) olFolderJournal = ----------------11 olFolderNotes = ------------------12 olFolderTasks = ------------------13 olFolderDrafts = ----------------(16) olPublicFoldersAllPublicFolders = 18 olFolderConflicts = --------------19 olFolderSyncIssues = ------------(20) olFolderLocalFailures = ----------21 olFolderServerFailures = --------(22) olFolderJunk = ------------------(23) olFolderRssFeeds = --------------(25) olFolderToDo = ------------------(28) olFolderManagedEmail = ----------(29) End Enum

or hard-code the values:

'v0.1.0
Set OutlookApp = CreateObject("Outlook.Application")
Set myNamespace = OutlookApp.GetNameSpace("MAPI")
Set myFolder = myNamespace.GetDefaultFolder(10)
myFolder.Display

Note that I've changed the name of the variable for the Outlook Application object so as to avoid any confusion with the built-in Application object.


I find it better, at least while developing the code, to use early binding. You do this by adding a reference to the Outlook object library to the project:

Worksheet Screenshot

and using the following code:

'v0.1.0
Dim OutlookApp As Outlook.Application
Set OutlookApp = New Outlook.Application
Dim myNamespace As Outlook.Namespace
Set myNamespace = OutlookApp.GetNameSpace("MAPI")
Dim myFolder As Outlook.MAPIFolder
Set myFolder = myNamespace.GetDefaultFolder(olFolderContacts)
myFolder.Display

A final tip is to always use the Option Explicit directive, and always explicitly declare all your variables.

The best way to ensure this happens is to set the Require Variable Declaration option:

Worksheet Screenshot

That way the VBE will always insert the directive in every module created.

10

I had the same problem and I tried to use Set OA = Outlook.Application to replace Set OA = CreateObject("Outlook.Application")

I am using Microsoft Office 365 and Outlook 365 system. And it works.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like