Retrieve Outlook Calendar Working Hours
Solution 1:
If your using Exchange 2010 or later you can get the working hours configuration (documented in http://msdn.microsoft.com/en-us/library/ee202895(v=exchg.80).aspx ) from the IPM.Configuration.WorkHours UserConfiguration FAI object (Folder Associated Items) using the GetUserConfiguration operation in EWS http://msdn.microsoft.com/en-us/library/office/dd899439(v=exchg.150).aspx . eg
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);
Solution 2:
I thought I would update this for VBA, I know it is an old thread but may help people and save them some time. I wrote the following for use in Excel to get to Outlook Calendar settings. I would welcome any feedback and tips on better/neater code writing.
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