VBA for Outlook - How to create an array for outlook appointment RRS feed

  • General discussion

  • Hi VBA experts.

    I am writing a VBA that deals with outook appointments.

    I've got the following code:

    Set objOutlook = Outlook.Application
    Set objNamespace = objOutlook.GetNamespace("MAPI")

    Set objFolder = objNamespace.GetDefaultFolder(olFolderCalendar)

    to save all current calendar appointments into objFolder. Now, If there is are several outlooks you identified from this object array(is it?) that you know the indeces for, say 81,82,86,87, how would you save them into another sub appointment array then sort them by their appointment start date? I see that by default, these Outlooks have been ordered in the chronological order.

    Achieving this in my code would help me tremendously.

    Thank you!

    Thursday, January 9, 2014 2:53 AM

All replies

  • Hello William,

    I have noticed the following line of code above:

    Set objOutlook = Outlook.Application

    You can't assign a type to a variable. If you run the VBA code inside Outlook you can use the global Application property. So, you need to write instead:

    Set objOutlook = Application

    But if you are trying to automate Outlook outside, i.e. from another Office application, you need to create an instance or just get the running one, for example:

    ' "late" binding
    Dim objOL as Object
    Set objOL = CreateObject("Outlook.Application")
    ' "early" binding
    Set ol = New Outlook.Application

    Please take a look at the How to automate Outlook from another program article for more information on this.

    Sorry, but I don't quite understand the rest of your message.

    > Now, If there is are several outlooks you identified from this object array(is it?)

    Do you have several Outlook instances with different accounts opened? Could you please explain in details.

    Anyway, you can use the Find/FindNext or Restrict method of the Items class (see the corresponding property of the Folder class in Outlook) to retrieve all Outlook appointment items from the folder that meets the predefined condition. You can read more about these methods in the How To: Retrieve Outlook calendar items using Find and FindNext methods and How To: Use Restrict method in Outlook to get calendar items articles.

    To get a single item from a folder by index you can use the Item method of the Items class:

    Dim items As Outlook.items
    Dim appointment As Outlook.AppointmentItem
    Set appointment = items.Item(81)
    Finally, the Functions For VBA Arrays article explains how to work with arrays in VBA.

    Thursday, January 9, 2014 9:28 AM