I am working on a script to automate OOO in Outlook by reading an MS Excel sheet.
21-Oct-2016
and 24-Oct-2016
and if this script is run on 20-Oct-2016
, it should be able to set the OOO starting 21-Oct-2016
till 24-Oct-2016
automatically (without having to open MS Outlook)Here's the code in progress:
Sub ReadDataAndSetOOO()
Dim objExcel,ObjWorkbook,objsheet
intRow = 2
Dim startDateValue, endDateValue
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\input.xlsx")
set objsheet = objExcel.ActiveWorkbook.Worksheets(1)
DateToday = FormatDateTime(Date, 1)
DateTomorrow = formatDate(FormatDateTime(DateAdd("d", 1, DateToday), 1))
Wscript.Echo DateTomorrow
Do Until objExcel.Cells(intRow,1).Value = ""
startDateValue = formatDate(FormatDateTime(objsheet.Cells(intRow,1).value,1))
endDateValue = formatDate(FormatDateTime(objsheet.Cells(intRow,2).value))
Wscript.Echo "Start date=" & startDateValue
Wscript.Echo "End date=" & endDateValue
If DateTomorrow = startDateValue Then
'Following line to be replaced by the code to set OOO between start and end date
Wscript.Echo "I am on leave from " & startDateValue & " to " & endDateValue
End If
intRow = intRow + 1
Loop
objExcel.ActiveWorkbook.Close
objExcel.Workbooks.Close
objExcel.Application.Quit
End Sub
Function formatDate(myDate)
d = parse(Day(myDate))
m = parse(Month(myDate))
y = Year(myDate)
formatDate= d & "-" & m & "-" & y
End Function
Function parse(num)
If(Len(num)=1) Then
parse="0"&num
Else
parse=num
End If
End Function
ReadDataAndSetOOO
I referred to this link and some other links, but everywhere, OOO is set immediately and not for required start and end dates.
Any pointers are appreciated.
OOF time range can only be set through EWS, namely using the UserOofSettings verb. It cannot be set using Outlook Object Model or Extended MAPI.
If using Redemption is an option (I am its author), it exposes the RDOOutOfOfficeAssistant object. Since it performs an EWS call, it will need the credentials of the mailbox user.
set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = Application.Session.MAPIOBJECT
Session.Credentials.Add "*.myserver.com", "Domain\UserName", "MyPassword"
set OofAssistant = Session.Stores.DefaultStore.OutOfOfficeAssistant
OofAssistant.BeginUpdate
OofAssistant.StartTime = #12/21/2011#
OofAssistant.EndTime = #01/03/2012 9:00#
OofAssistant.State = 2 'rdoOofScheduled
OofAssistant.ExternalAudience = 1 'rdoOofAudienceKnown
OofAssistant.OutOfOfficeTextInternal = "<html><body>I am on vacation from 12/21/2001 until 01/03/2012. Please contact " & _
"<a href=""mailto:[email protected]"">Joe User</a>" & _
" if you have any questions</body></html>"
OofAssistant.OutOfOfficeTextExternal = "<html><body>I am on <b>vacation</b> until next year. </body></html>"
OofAssistant.EndUpdate