I am trying to list the events of a subscribed internet calendar from gmail.
The code only lists events created in the Outlook app in the Calender Folder.
Here is the code I found on Stack Overflow:
Option Explicit
Sub ListAppointments()
Dim olApp As Object
Dim olNS As Object
Dim olFolder As Object
Dim olApt As Object
Dim NextRow As Long
Dim FromDate As Date
Dim ToDate As Date
FromDate = CDate("08/25/2018")
ToDate = CDate("12/31/2019")
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
If Err.Number > 0 Then Set olApp = CreateObject("Outlook.Application")
On Error GoTo 0
Set olNS = olApp.GetNamespace("MAPI")
Set olFolder = olNS.GetDefaultFolder(9) 'olFolderCalendar 9
NextRow = 2
With Sheets("Sheet1") 'Change the name of the sheet here
.Range("A1:D1").Value = Array("Project", "Date", "Time spent", "Location")
For Each olApt In olFolder.Items
If (olApt.Start >= FromDate And olApt.Start <= ToDate) Then
.Cells(NextRow, "A").Value = olApt.Subject
.Cells(NextRow, "B").Value = CDate(olApt.Start)
.Cells(NextRow, "C").Value = olApt.End - olApt.Start
.Cells(NextRow, "C").NumberFormat = "HH:MM:SS"
.Cells(NextRow, "D").Value = olApt.Location
.Cells(NextRow, "E").Value = olApt.Categories
NextRow = NextRow + 1
Else
End If
Next olApt
.Columns.AutoFit
End With
Set olApt = Nothing
Set olFolder = Nothing
Set olNS = Nothing
Set olApp = Nothing
End Sub
I am almost positive that the issue lies here:
Set olFolder = olNS.GetDefaultFolder(9) 'olFolderCalendar 9
The name of the folder I want to access is basic. I tried both of these:
Set olFolder = olNS.GetDefaultFolder(9).Folders("basic").Items
Set olFolder = olNS.GetDefaultFolder(9).Parent.Folders("basic").Items
None worked.
Run-time error '-2147221233 (8004010f)':
The Attempted operation failed. An Object Could Not Be Found.
Here is the basic folder I want to grab the events from.
EDIT:
I ultimately found: olNS.GetDefaultFolder(9).Parent = email@gmail.com
and its child is one of my calendars "Calendar" seen in the picture. The parent of "basic" calendar is Internet Calendars. How can I set olFolder to the directory '\Internet Calendars\basic' instead of '\email@gmail.com\Calendar'?