CalendarView.SelectedStartTime Property (Outlook)
Returns a Date that represents the start time of a selection in the CalendarView object. Read-only.
Version Information
Version Added: Outlook 2010
Syntax
expression .SelectedStartTime
expression A variable that represents a CalendarView object.
Remarks
The intent of the SelectedStartTime and SelectedEndTime properties is to replicate, programmatically, the way that users create an appointment in the Microsoft Outlook user interface. Typically, a user selects a time range in the calendar view and then creates a new appointment by either double clicking the selection or clicking New Appointment in the Home tab of the ribbon. With these two properties of the CalendarView object, you can obtain the start time and the end time of any selection in that view programmatically. You can then programmatically create the AppointmentItem object, setting the Start and End properties of the AppointmentItem object to the SelectedStartTime and SelectedEndTime properties respectively to reflect any user selection in the calendar view.
If the selection in the calendar view is a time range and is not an item, SelectedStartTime returns a Date value equal to the start time of the selection.
If one or more items are selected in the calendar view, SelectedStartTime returns a Date value equal to the start time of the first item in the selection of the explorer that displays the calendar view. That selection is specified by the Selection property of the Explorer object.
To use this property on a CalendarView object, obtain the CalendarView object from the CurrentView property of the active Explorer object (which can be returned by the Application.ActiveExplorer method). There is a known issue with using this property on an CalendarView object obtained otherwise - using the CurrentView property of the current Folder object (returned by the Application.ActiveExplorer.CurrentFolder property).
Example
The following code samples, in Visual Basic for Applications (VBA) and C#, show how to use the SelectedStartTime and SelectedEndTime properties of the calendar view of the active explorer to initialize the start and end times of a new appointment. The following code sample is in VBA.
Sub CreateAppointmentUsingSelectedTime()
Dim datStart As Date
Dim datEnd As Date
Dim oView As Outlook.view
Dim oCalView As Outlook.CalendarView
Dim oExpl As Outlook.Explorer
Dim oFolder As Outlook.folder
Dim oAppt As Outlook.AppointmentItem
Const datNull As Date = #1/1/4501#
' Obtain the calendar view using
' Application.ActiveExplorer.CurrentFolder.CurrentView.
' If you use oExpl.CurrentFolder.CurrentView,
' this code will not operate as expected.
Set oExpl = Application.ActiveExplorer
Set oFolder = Application.ActiveExplorer.CurrentFolder
Set oView = oExpl.CurrentView
' Check whether the active explorer is displaying a calendar view.
If oView.ViewType = olCalendarView Then
Set oCalView = oExpl.currentView
' Create the appointment using the values in
' the SelectedStartTime and SelectedEndTime properties as
' appointment start and end times.
datStart = oCalView.SelectedStartTime
datEnd = oCalView.SelectedEndTime
Set oAppt = oFolder.items.Add("IPM.Appointment")
If datStart <> datNull And datEnd <> datNull Then
oAppt.Start = datStart
oAppt.End = datEnd
End If
oAppt.Display
End If
End Sub
The following managed code is written in C#. To run a .NET Framework managed code sample that needs to call into a Component Object Model (COM), you must use an interop assembly that defines and maps managed interfaces to the COM objects in the object model type library. For Outlook, you can use Microsoft Visual Studio and the Microsoft Outlook Primary Interop Assembly (PIA). Before you run managed code samples for Microsoft Outlook 2010, ensure that you have installed the Outlook 2010 PIA and have added a reference to the Microsoft Outlook 14.0 Object Library component in Visual Studio. You should use the following code in the ThisAddIn class of a Visual Studio Tools for Office add-in for Outlook, such that the Application object in the code is a trusted Outlook Application object provided by ThisAddIn.Globals. For more information about using the Outlook PIA to develop managed Outlook solutions, see the Outlook 2010 Primary Interop Assembly Reference on MSDN.
private void CreateAppointmentUsingSelectedTime()
{
DateTime dateNull =
new DateTime(4501, 1, 1, 0, 0, 0);
Outlook.Explorer expl = Application.ActiveExplorer();
Outlook.Folder folder = expl.CurrentFolder as Outlook.Folder;
Outlook.View view = expl.CurrentView as Outlook.View;
if (view.ViewType == Outlook.OlViewType.olCalendarView)
{
Outlook.CalendarView calView = view as Outlook.CalendarView;
DateTime dateStart = calView.SelectedStartTime;
DateTime dateEnd = calView.SelectedEndTime;
Outlook.AppointmentItem appt =
folder.Items.Add("IPM.Appointment")
as Outlook.AppointmentItem;
if (dateStart != dateNull && dateEnd != dateNull)
{
appt.Start = dateStart;
appt.End = dateEnd;
}
appt.Display(false);
}
}