Search code examples
vbaoutlookenumerationis-empty

vba code suddenly encounters problems with enumeration


This visual basic code related to outlook used to work without problems

Sub cmdExample()

    Dim myOlApp As Object
    Set myOlApp = CreateObject("Outlook.Application")
    Set myoSession = myOlApp.Session
    Set myoCalendar = myoSession.GetDefaultFolder(olFolderCalendar)
End Sub

Now I obtain the runtime error 5 (Invalid procedure call or argument)

Reason found from debugging: at runtime olFolderCalendar is empty (by the way, same problem for other enumerations like olAppointmentItem, olBusy). My workaround in the above code is calling .GetDefaultFolder(9).

However I would rather use the enumerations and I would like to understand why all of them are empty.


Solution

  • If you want to pass literal constants instead of numbers you need to add a COM reference to Outlook. Read more about that in the How to Add an Object Library Reference in VBA article.

    enter image description here

    Also, you may try to use the Logon method before getting the folder. See NameSpace.Logon for more information.