我正在使用交换的 Web 服务并想检索用户“工作时间”。工作时间是日历上的设置,有助于计算忙/闲,但我想获取或计算实际值。
我可以完全访问日历。如果我可以使用 EWS 托管 API,那将是我的首选。我已经在网上搜索过,并查看了GetUserAvailability
操作,但我一直无法找到可以给我这些数据的方法。
我正在使用交换的 Web 服务并想检索用户“工作时间”。工作时间是日历上的设置,有助于计算忙/闲,但我想获取或计算实际值。
我可以完全访问日历。如果我可以使用 EWS 托管 API,那将是我的首选。我已经在网上搜索过,并查看了GetUserAvailability
操作,但我一直无法找到可以给我这些数据的方法。
如果您使用 Exchange 2010 或更高版本,您可以从 IPM.Configuration获取工作时间配置(记录在http://msdn.microsoft.com/en-us/library/ee202895(v=exchg.80).aspx中)。 WorkHours UserConfiguration FAI 对象(文件夹关联项)使用 EWS http://msdn.microsoft.com/en-us/library/office/dd899439(v=exchg.150).aspx中的 GetUserConfiguration 操作。例如
UserConfiguration usrConfig = UserConfiguration.Bind(service, "WorkHours", WellKnownFolderName.Calendar, UserConfigurationProperties.All);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(new MemoryStream(usrConfig.XmlData));
XmlNodeList nlList = xmlDoc.GetElementsByTagName("WorkHoursVersion1");
Console.WriteLine(nlList.Item(0).InnerXml);
我想我会为 VBA 更新这个,我知道这是一个旧线程,但可以帮助人们并节省他们一些时间。我编写了以下内容以在 Excel 中使用以访问 Outlook 日历设置。我欢迎任何关于更好/更整洁代码编写的反馈和提示。
Function GetUserWorkingHours(WHType As String, oCalendarFolder As Object) As String
' Returns user's Calendar Start or End work times
' Uses existing Outlook calendar folder object
' The workinghours data is stored in a hidden Outlook storage binary stream in xml format (no, seriously, it is!)
' ... with a sign on the door saying "beware of the leopard"
'
' Cheshire Catalyst software July 2020
'
Dim olStorage As Object
Dim olPropacc As Object
Dim olBytes() As Byte
Dim a As Variant
Dim xmlString As String ' xml stream text stored here
Dim objDOM As Object ' xml object to parse the xml stream
Dim Result As String ' Holding place for return value
' Loads the hidden Outlook xml store to retrieve WorkingHours
Set olStorage = oCalendarFolder.GetStorage("IPM.Configuration.workhours", 2)
Set olPropacc = olStorage.PropertyAccessor
olBytes = olPropacc.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x7C080102")
' Translate binary stream into text byte by byte (there may be a better way to do this but this way works)
For Each a In olBytes
xmlString = xmlString & Chr(a)
Next a
' Generate the xml object to parse
Set objDOM = CreateObject("Msxml2.DOMDocument.3.0")
' Load the xml stream into the xml parser
objDOM.LoadXML xmlString
' Filter on what we are looking for
Select Case WHType
Case "Start"
Result = objDOM.SelectSingleNode("Root/WorkHoursVersion1/TimeSlot/Start").Text
Case "End"
Result = objDOM.SelectSingleNode("Root/WorkHoursVersion1/TimeSlot/End").Text
Case Else
' Perhaps we should have tested for this before all that messing about with Outlook stores
Result = "Invalid" ' Invalid request
End Select
GetUserWorkingHours = Result
' Tidy up all those objects
Set olStorage = Nothing
Set olPropacc = Nothing
Set objDOM = Nothing
Erase olBytes
End Function
Sub testit()
Dim oOutlook As Object ' Outlook instance
Dim oNS As Object ' Outlook namespace
Dim oCalendar As Object ' Calendar folder of Outlook instance
Set oOutlook = GetObject(, "Outlook.Application")
Set oNS = oOutlook.GetNamespace("MAPI")
Set oCalendar = oNS.GetDefaultFolder(9)
MsgBox ("Start: " & GetUserWorkingHours("Start", oCalendar) & " End: " & GetUserWorkingHours("End", oCalendar))
End Sub