【发布时间】:2022-05-01 13:32:28
【问题描述】:
我正在尝试使用用户在其日历上选择的日期填充 Outlook 日历约会模板的约会开始日期。
有人可以提供 VBA 代码,让我可以确定用户在他们的日历上选择的日期(如果有的话)吗?
【问题讨论】:
我正在尝试使用用户在其日历上选择的日期填充 Outlook 日历约会模板的约会开始日期。
有人可以提供 VBA 代码,让我可以确定用户在他们的日历上选择的日期(如果有的话)吗?
【问题讨论】:
您可能会发现 Explorer 类的 Selection 属性很有帮助。它返回一个 Selection 对象,其中包含在资源管理器窗口中选择的一个或多个项目。
但您正在寻找的是 SelectedStartTime 和 SelectedEndTime 属性,它们用于以编程方式复制用户在 Outlook UI 中创建约会的方式。通常,用户在日历视图中选择一个时间范围,然后通过双击选择或单击功能区主页选项卡中的新约会来创建新约会。使用 CalendarView 对象的这两个属性,您可以通过编程方式获取该视图中任何选择的开始时间和结束时间。
然后您可以通过编程方式创建 AppointmentItem 对象,将 AppointmentItem 对象的 Start 和 End 属性分别设置为 SelectedStartTime 和 SelectedEndTime 属性,以反映日历视图中的任何用户选择。 如果日历视图中的选择是一个时间范围而不是一个项目,则 SelectedStartTime 返回一个日期值,该值等于选择的开始时间。 如果在日历视图中选择了一个或多个项目,则 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
【讨论】:
试试下面的方法。
如果您不确定特定 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
【讨论】: