user2271875
user2271875

Reputation: 325

Outlook/VBA: Getting the Current View's Start and End Date

I've programmed VBA for various Office applications, but I'm new to Outlook, so I'm still learning the various Outlook objects.

In the Outlook calendar view, I'm either in a "Day", "Work Week", "Week", "Month", or "Schedule View" arrangement. I can change between these however I want to see my events. Today being November 29, 2016, displayed above my calendar (Outlook 2016) I see:

This is obviously a date range for my current view (probably the incorrect term).

How do I reference the beginning and/or end dates of this view in VBA? I want to clarify that I do NOT want the beginning and end dates of a selection (which is what I'm finding online), but of the actual view--what I'm seeing at the moment on my Outlook client application.

Thanks in advance for your time and assistance.

Upvotes: 3

Views: 1499

Answers (1)

OpiesDad
OpiesDad

Reputation: 3435

You want the CalendarView.DisplayDates property. This is an array containing each date displayed. Get the first and last value in the array. See here.

Code reproduced and altered for clarity below:

Sub DisplayDayRange()

 Dim objView As CalendarView
 Dim varArray As Variant
 Dim beginDate As Date
 Dim endDate As Date

    ' Check if the current view is a calendar view.

    If Application.ActiveExplorer.CurrentView.ViewType = olCalendarView Then       

        ' Obtain a CalendarView object reference for the     
        ' current calendar view.

        Set objView = Application.ActiveExplorer.CurrentView

        ' Obtain the DisplayedDates value, a string  
        ' array of dates representing the dates displayed           
        ' in the calendar view.

        varArray = objView.DisplayedDates

        ' If the example obtained a valid array, display  
        ' a dialog box with a summary of its contents.

        If IsArray(varArray) Then

            beginDate = varArray(LBound(varArray))
            endDate = varArray(UBound(varArray))
            MsgBox "There are " & (UBound(varArray) - LBound(varArray)) + 1 & " days displayed, from " & beginDate & " to " & endDate  
        End If     
    End If

 End Sub

Upvotes: 3

Related Questions