Import Outlook Appointments into Excel

Thanek

New Member
Joined
Apr 5, 2013
Messages
45
I have a comprehensive spreadsheet for project management that I would like to link back to a Public outlook calendar. The calendar dates are constantly being shuffled but the projects remain the same. Is there a way to reference a calendar entry so that the Start Date fills in a particular cell?
 
A little bit late but you could try a variation on the below. The code uses early binding and therefore requires a reference to the Outlook Object Library. If you wish to use late binding you can use the comments instead. Note that if there was no instance of Outlook already running neither version will close Outlook again. To do so you'd need to add in a outApp.Quit statement and perhaps call it depending on a variable that tested if it was open already.

The code opens the first appointment in the calendar that has the same subject that is typed into cell A1 and then returns the start date - if you want the time as well then remove the format function.

Code:
Sub ImportCalendarDate()
Dim outApp As Outlook.Application 'Object
Dim outCalendar As Outlook.Folder 'Object
Dim outAppt As Outlook.AppointmentItem 'Object


    'On Error Resume Next
    
    Set outApp = Outlook.Application 'GetObject(, "Outlook.Application")
'    If outApp Is Nothing Then
'        Set outApp = CreateObject("Outlook.Application")
'    End If
    
    'On Error GoTo 0
    
    Set outCalendar = outApp.Session.GetDefaultFolder(olFolderCalendar) '9
    Set outAppt = outCalendar.Items(Cells(1, 1).Value)
    Debug.Print Format(outAppt.Start, "dd/mm/yyyy")
    Set outAppt = Nothing
    Set outCalendar = Nothing
    Set outApp = Nothing
    
End Sub

Hope this is of some help as a starting point

Simon
 
Upvote 0
Thanks for the reply,

I have already implemented a code that paths to a public exchange calendar. I have been trying to modify it to late binding with little experience or luck. Is late binding possible with the below?

Code:
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
 
Upvote 0
The code is a bit of a mess but I've had a quick breeze through it. The code should now work with late binding, although it probably isn't the best way of doing so.

Anyway, taking your code as is, here is a hatchet job to get it to work.

Code:
Option Explicit

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 Object
Dim myCalItems As Object
Dim ItemstoCheck As Object
Dim ThisAppt As Object
Dim MyItem As Object
Dim StringToCheck As String
Dim MyBook As Workbook
Dim rngStart As Range
Dim NextRow As Long


On Error Resume Next
Set olapp = GetObject(, "Outlook.Application")


If olapp Is Nothing Then Set olapp = CreateObject("Outlook.Application")
On Error GoTo 0
Set olNS = olapp.GetNamespace("MAPI")


' 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


Set myCalItems = olNS.GetDefaultFolder(9).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 = 26 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


Function Quote(MyText)
    Quote = Chr(34) & MyText & Chr(34)
End Function

Hope this helps

Simon
 
Upvote 0
Is there a reason this line would throw a compile error for the object library in excel 2003?

Code:
.Offset(NextRow, 1).Value = Format(ThisAppt.Start, "MM/DD/YYYY")
 
Upvote 0
You're probably missing a library reference somewhere, if you click on Tools>References you should be able to find a reference that is preceded by the word missing. If not perhaps you could add VBA. before format (like in the example code below). But that shouldn't be necessary

Also, I'm not convinced that piece of code is doing what you want. Perhaps you could try something like

Code:
NextRow = Sheets(4).UsedRange.Rows.Count    With rngStart
        .Offset(NextRow, 1).Value = VBA.Format(ThisAppt.Start, "MM/DD/YYYY")
        .Offset(NextRow, 0).Value = ThisAppt.Subject
    End With

Also, you might want to place something like a Sheets(4).UsedRange.Delete statement near the beginning so as you're starting from scratch each time.

Hope this helps - let me know how you get on

Simon
 
Upvote 0
Wow you're quick with replies! Thank you so much for your input! The VBA.Format did seem to alleviate that issue at the only terminal I have to test on older versions of MS Office.

Yeah, this code was just the first thing that I could adapt to my cause. Since my first request I've found it easier to import the list of appointments and match them up in Excel. I would be more than willing to use a simpler code such as your suggested snippet if it could search within a date range and return a list in excel of all appt subject lines with the start date. Oh, and be version independent.
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top