5

I am trying to populate the appointment start date of an Outlook Calendar appointment template with the date the user has selected on their calendar.

Can someone offer VBA code that allows me determine what (if any) date the user has selected on their calendar?

Dave
  • 53
  • 1
  • 3

2 Answers2

3

You may find the Selection property of the Explorer class helpful. It returns a Selection object that contains the item or items that are selected in the explorer window.

But what you are looking for is the SelectedStartTime and SelectedEndTime properties which are used to replicate programmatically the way that users create an appointment in the Outlook UI. 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. For example:

 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 
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
2

Try something like the following.

If you are not sure what is exposed by a particular Outlook object, try OutlookSpy (I am its author). In this case, click Explorer, select CurrentView, click Browse.

set vExplorer = Application.ActiveExplorer
set vView = vExplorer.CurrentView
if TypeName(vView) = "CalendarView" Then
  MsgBox vView.SelectedStartTime & " - " & vView.SelectedEndTime
End If
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78