Sub GetApptsFromOutlook()
Application.ScreenUpdating = False
Call GetCalData("4/1/2013", "12/30/13")
Application.ScreenUpdating = True
End Sub
Private Sub GetCalData(StartDate As Date, Optional EndDate As Date)
' -------------------------------------------------
' Notes:
' If Outlook is not open, it still works, but much slower (~8 secs vs. 2 secs w/ Outlook open).
' Make sure to reference the Outlook object library before running the code
' End Date is optional, if you want to pull from only one day, use: Call GetCalData("7/14/2008")
' -------------------------------------------------
Dim olapp As Object
Dim olNS As Outlook.Namespace
Dim myCalItems As Outlook.Items
Dim ItemstoCheck As Outlook.Items
Dim ThisAppt As Outlook.AppointmentItem
Dim MyItem As Object
Dim StringToCheck As String
Dim MyBook As Excel.Workbook
Dim rngStart As Excel.Range
Dim I As Long
Dim NextRow As Long
Set olapp = Outlook.Application
Set olNS = olapp.GetNamespace("MAPI")
Set myCalItems = Outlook.Items
Set ItemstoCheck = Outlook.Items
Set ThisAppt = Outlook.AppointmentItem
Set MyBook = Excel.Workbook
Set rngStart = Excel.Range
' if no end date was specified, then the requestor only wants one day, so set EndDate = StartDate
' this will let us return appts from multiple dates, if the requestor does in fact set an appropriate end date
If EndDate = "12:00:00 AM" Then
EndDate = StartDate
End If
If EndDate < StartDate Then
MsgBox "Those dates seem switched, please check them and try again.", vbInformation
GoTo ExitProc
End If
If EndDate - StartDate > 28 Then
' ask if the requestor wants so much info
If MsgBox("Update blend dates from Outlook?", vbInformation + vbYesNo) = vbNo Then
GoTo ExitProc
End If
End If
' get or create Outlook object and make sure it exists before continuing
On Error Resume Next
Set olapp = GetObject(, "Outlook.Application")
If Err.Number <> 0 Then
Set olapp = CreateObject("Outlook.Application")
End If
On Error GoTo 0
If olapp Is Nothing Then
MsgBox "Cannot start Outlook.", vbExclamation
GoTo ExitProc
End If
Set olNS = olapp.GetNamespace("MAPI")
Set myCalItems = olNS.GetFolderFromID("000000008DE72F48E3406B4FB50A50B93135256301000000335D345800D26C459ED4B2A336C41355").Items
' ------------------------------------------------------------------
' the following code adapted from:
' http://www.outlookcode.com/article.aspx?id=30
'
With myCalItems
.Sort "[Start]", False
.IncludeRecurrences = True
End With
'
StringToCheck = "[Start] >= " & Quote(StartDate & " 12:00 AM") & " AND [End] <= " & _
Quote(EndDate & " 11:59 PM")
Debug.Print StringToCheck
'
Set ItemstoCheck = myCalItems.Restrict(StringToCheck)
Debug.Print ItemstoCheck.Count
' ------------------------------------------------------------------
If ItemstoCheck.Count > 0 Then
' we found at least one appt
' check if there are actually any items in the collection, otherwise exit
If ItemstoCheck.Item(1) Is Nothing Then GoTo ExitProc
Set MyBook = ThisWorkbook
Set rngStart = ThisWorkbook.Sheets(4).Range("A1")
With rngStart
.Offset(0, 1).Value = "Start Time"
.Offset(0, 0).Value = "Subject"
End With
For Each MyItem In ItemstoCheck
If MyItem.Class = olAppointment Then
' MyItem is the appointment or meeting item we want,
' set obj reference to it
Set ThisAppt = MyItem
NextRow = Range("A" & Rows.Count).End(xlUp).Row + 1
With rngStart
.Offset(NextRow, 1).Value = Format(ThisAppt.Start, "MM/DD/YYYY")
.Offset(NextRow, 0).Value = ThisAppt.Subject
End With
End If
Next MyItem
Else
MsgBox "There are no appointments or meetings during" & _
"the time you specified. Exiting now.", vbCritical
End If
ExitProc:
Set myCalItems = Nothing
Set ItemstoCheck = Nothing
Set olNS = Nothing
Set olapp = Nothing
Set rngStart = Nothing
Set ThisAppt = Nothing
End Sub