Hi,
I'm trying to specify a date range to export but it doesn't seem to work?
Any help is appreciated...
Sub RunExportCalendarsToExcel()
'Change the name of the conference room on the next line. The name must match the name of the mailbox.'
ExportCalendarToExcel "Rutherford Cameron", True
'Repeat the next line for each subsequent conference room. Be sure to change the name.'
'ExportCalendarToExcel "Fiddy Hayley"
End Sub
Sub ExportCalendarToExcel(strCalendarName As String, Optional bolClearWorksheet As Boolean)
Dim olkFolder As Outlook.Folder, olkItems As Outlook.Items, olkAppt As Outlook.AppointmentItem, olkRecipient As Outlook.Recipient
Dim excApp As Object, excWkb As Object, excSht As Object, excRng As Object, lngRow As Long, strDat As String, datBeg As Date, datEnd As Date, arrTmp As Variant
Dim arrTitle As Variant
strDat = InputBox("Enter the date range of the calendar to export in the form ""mm/dd/yyyy to mm/dd/yyyy""", SCRIPT_NAME, Date & " to " & Date)
arrTmp = Split(strDat, "to")
datBeg = IIf(IsDate(arrTmp(0)), arrTmp(0), Date) & " 12:00am"
datEnd = IIf(IsDate(arrTmp(1)), arrTmp(1), Date) & " 11:59pm"
'Launch Excel and open the spreadsheet'
Set excApp = CreateObject("Excel.Application")
excApp.Visible = True
'Change the name and path of the spreadsheet on the next line'
Set excWkb = excApp.Workbooks.Open("U:\Calendar_Export.xlsx")
Set excSht = excWkb.Worksheets(1)
If bolClearWorksheet Then
Set excRng = excSht.Range("A1").CurrentRegion
lngRow = excRng.Rows.Count
excApp.Rows(2 & ":" & lngRow).Delete
lngRow = 2
Else
lngRow = excSht.UsedRange.Rows.Count + 1
End If
'Connect to and process the shared calendar'
Set olkRecipient = Session.CreateRecipient(strCalendarName)
Set olkFolder = Session.GetSharedDefaultFolder(olkRecipient, olFolderCalendar)
Set olkItems = olkFolder.Items.Restrict("[Start] >= '" & Format(datBeg, "ddddd h:nn AMPM") & "' AND [Start] <= '" & Format(datEnd, "ddddd h:nn AMPM") & "'")
olkItems.Sort "[Start]"
olkItems.IncludeRecurrences = True
For Each olkAppt In olkItems
arrTitle = Split(olkAppt.Subject, "-")
excSht.Cells(lngRow, 1) = strCalendarName
excSht.Cells(lngRow, 2) = olkAppt.Categories
excSht.Cells(lngRow, 3) = olkAppt.Start
excSht.Cells(lngRow, 4) = olkAppt.End
excSht.Cells(lngRow, 5) = olkAppt.Subject
excSht.Cells(lngRow, 6) = Format(olkAppt.Start, "hh:nn ampm")
excSht.Cells(lngRow, 7) = Format(olkAppt.End, "hh:nn ampm")
excSht.Cells(lngRow, 8) = DateDiff("n", olkAppt.Start, olkAppt.End) / 60
lngRow = lngRow + 1
Next
excSht.Columns("A:H").AutoFit
'Save the spreadsheet and exit Excel'
Set excRng = Nothing
Set excSht = Nothing
'excWkb.Save
Set excWkb = Nothing
excApp.Quit
Set excApp = Nothing
'Clean-up the Outlook objects'
Set olkFolder = Nothing
Set olkItems = Nothing
Set olkAppt = Nothing
End Sub