Results 1 to 2 of 2

Thread: [VSTO] Outlook Calendar Help

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2014
    Posts
    1

    [VSTO] Outlook Calendar Help

    Hello vbforums

    I don't know where else to turn with some help with one of my projects.
    I tried the .net forums on Microsoft, but it seems that they don't know either.

    Anyway, I have a VSTO written in VBNET, which I need to work with Calendars.

    There are a number of Calendars which I need to work with.
    Here is the process I need to accomplish

    1) Have a dropdown ribbon control listing all Calendars under My Calendars in Outlook. (needs to programmatic, so any new calendars created will show there) - this can load on a button click instead of on load.
    2) When selecting a calendar from the dropdown ribbon control, have a button which then polls the selected calendar for it's information (subject/ content/ attendees) for the current date/ time (the calendars constantly have an appointment, 24/7)
    3) Have a small form appear with that information

    The code I currently have works, but only on the default (my) calendar. And I can't seem to figure out how to get the information from other calendars in the list.

    Here is my current code (please excuse the mess, it will get cleaned up once complete).
    There are a number of variables called elsewhere.

    Code:
    Private Sub newcal2()
            Try
    
           
            ' Create the Outlook application.
            Dim oApp As New Outlook.Application()
    
    
                'get namespace
            Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")
    
            ' Get the Calendar folder.
                Dim oCalendar As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar)
    
            ' Get the Items collection in the folder.
            Dim oItems As Outlook.Items = DirectCast(oCalendar.Items, Outlook.Items)
    
            'Log on by using a dialog box to choose the profile.
            oNS.Logon(Missing.Value, Missing.Value, True, True)
    
    
            ' Get the Items (Appointments) collection from the Calendar folder.
            oItems = oCalendar.Items
            oItems.IncludeRecurrences = True
    
                ' Get current appointment
            Dim filter As [String] = "[Start] <= '" + DateTime.Now.ToString("yyyy/MM/dd hh:mm AMPM") + "'"
            Dim restrictedItems As Outlook.Items = oItems.Restrict(filter)
            ' Take the last item on the list - should be current or next appointment
            restrictedItems.Sort("[Start]")
            Dim oAppt As Outlook.AppointmentItem = restrictedItems.GetLast()
    
    
            ' Done. Log off.
                oNS.Logoff()
    
                
    
            Dim todaysappointments As String = ""
            Dim currentTime As DateTime = DateTime.Now
            For Each item As Outlook.AppointmentItem In oItems
                If item.Start <= currentTime AndAlso item.[End].Subtract(New TimeSpan(0, 10, 0)) > currentTime Then
                    todaysappointments = todaysappointments & item.RequiredAttendees.ToString() & vbCrLf & item.Subject.ToString() & vbCrLf & item.Body.ToString()
                        'MsgBox(todaysappointments)
                        cname = item.RequiredAttendees.ToString()
                        title = item.Subject.ToString()
                        number = item.Body.ToString()
                        Dim window As New message
                        window.Show()
    
    
                End If
            Next
            Catch ex As System.Exception
                MsgBox(ex.ToString())
            End Try
        End Sub
    So basically, this code above needs to run on a selected calendar from a dropdown ribbon (it doesn't have to be in a dropdown, it could be in a popup listbox, or somewhere, where the user can select the calendar).

    I hope you can help

    Thanks!

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,964

    Re: [VSTO] Outlook Calendar Help

    to get all the folders of type calendar from your folder tree, you can try like
    Code:
    Sub getcals(f As MAPIFolder)
    Dim fol As MAPIFolder, cal As MAPIFolder
    For Each fol In f.Folders
        If fol.DefaultItemType = olAppointmentItem Then Debug.Print fol.Name
        getcals fol
    Next
    End Sub
    Sub init()
    Dim f As MAPIFolder
    For Each f In GetNamespace("mapi").Folders
        getcals f
    Next
    End Sub
    replace debug.print with additem for a listbox or combobox on a userform or whatever
    it may be better if you put save the path rather than the name as it is possible to have more than one calendar with same name, also easier to have the full path (or better folder entryID) when getting a folder object, could put the entryID into a hidden column of a combobox
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width