使用VBA确定选定的Outlook日历日期

时间:2015-07-17 20:05:01

标签: vba calendar outlook

我正在尝试使用用户在其日历上选择的日期填充Outlook日历约会模板的约会开始日期。

某人是否可以提供VBA代码,以便我确定用户在其日历上选择的日期(如果有)?

2 个答案:

答案 0 :(得分:1)

您可能会发现Explorer类的Selection属性很有用。它返回一个Selection对象,其中包含在资源管理器窗口中选择的一个或多个项目。

但您要查找的是SelectedStartTimeSelectedEndTime属性,这些属性用于以编程方式复制用户在Outlook UI中创建约会的方式。通常,用户在日历视图中选择时间范围,然后通过双击选择或单击功能区的“主页”选项卡中的“新建约会”来创建新约会。使用CalendarView对象的这两个属性,您可以以编程方式获取该视图中任何选择的开始时间和结束时间。

然后,您可以以编程方式创建AppointmentItem对象,将AppointmentItem对象的Start和End属性分别设置为SelectedStartTime和SelectedEndTime属性,以反映日历视图中的任何用户选择。 如果日历视图中的选择是时间范围而不是项目,则SelectedStartTime返回等于选择开始时间的Date值。 如果在日历视图中选择了一个或多个项目,则SelectedStartTime将返回Date值,该值等于显示日历视图的资源管理器选择中第一个项目的开始时间。该选择由Explorer对象的Selection属性指定。例如:

 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 

答案 1 :(得分:0)

尝试以下内容。

如果您不确定特定Outlook对象所显示的内容,请尝试OutlookSpy。在这种情况下,单击Explorer,选择CurrentView,单击Browse。

set vExplorer = Application.ActiveExplorer
set vView = vEXplorer.CurrentView
if TypeName(vView) = "CalendarView" Then
  MsgBox vView.SelectedStartTime & " - " & vView.SelectedEndTime
End If