使用VBA确定选定的Outlook日历日期 [英] Determining selected Outlook Calendar date with VBA

查看:47
本文介绍了使用VBA确定选定的Outlook日历日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用用户在日历中选择的日期填充Outlook日历约会模板的约会开始日期.

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.

有人可以提供允许我确定用户在日历上选择的日期(如果有)的VBA代码吗?

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

推荐答案

您可能会找到

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.

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

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.

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

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 

这篇关于使用VBA确定选定的Outlook日历日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆