Pookiemeister
Well-known Member
- Joined
- Jan 6, 2012
- Messages
- 630
- Office Version
- 365
- 2010
- Platform
- Windows
"Microsoft Office 16.0 Object Library" is checked
VBA Code:
Sub ExtractCalendarEvents()
Dim olApp As Outlook.Application [B]'ERROR HAPPENS HERE[/B]
Dim olNamespace As Outlook.Namespace
Dim olFolder As Outlook.Folder
Dim olItems As Outlook.Items
Dim olAppointment As Outlook.AppointmentItem
Dim i As Integer
' Create a new Excel workbook
Dim xlApp As Excel.Application
Dim xlWorkbook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Set xlApp = New Excel.Application
Set xlWorkbook = xlApp.Workbooks.Add
Set xlSheet = xlWorkbook.Sheets(1)
' Set up Outlook objects
Set olApp = New Outlook.Application
Set olNamespace = olApp.GetNamespace("MAPI")
Set olFolder = olNamespace.GetDefaultFolder(olFolderCalendar)
Set olItems = olFolder.Items
' Add headers to the Excel sheet
xlSheet.Cells(1, 1).Value = "Subject"
xlSheet.Cells(1, 2).Value = "Start"
xlSheet.Cells(1, 3).Value = "End"
' Loop through the calendar items and add them to the Excel sheet
i = 2
For Each olAppointment In olItems
If TypeName(olAppointment) = "AppointmentItem" Then
xlSheet.Cells(i, 1).Value = olAppointment.Subject
xlSheet.Cells(i, 2).Value = olAppointment.Start
xlSheet.Cells(i, 3).Value = olAppointment.End
i = i + 1
End If
Next olAppointment
' Save and open the workbook
xlWorkbook.SaveAs "C:\Path\To\Save\CalendarEvents.xlsx"
xlApp.Visible = True
' Clean up
Set olApp = Nothing
Set olNamespace = Nothing
Set olFolder = Nothing
Set olItems = Nothing
Set xlApp = Nothing
Set xlWorkbook = Nothing
Set xlSheet = Nothing
MsgBox "Events have been extracted and saved to an Excel file.", vbInformation
End Sub