PDA

View Full Version : [SOLVED:] VBA to export part of calendar to .ics



sijpie
09-24-2013, 11:58 PM
This is not a question. Just some working code to export part of the calendar (in the code set to the next three months) to .ics file



Option Explicit




'--------------------------------------------
Public Sub ExportEntireCalendar()
'
' Export calendar to .ics format. Settings _
what to export can be set below
'--------------------------------------------


Dim oNamespace As Namespace
Dim oFolder As Folder
Dim oCalendarSharing As CalendarSharing

On Error GoTo ErrRoutine
' Get a reference to the Calendar default folder
Set oNamespace = Application.GetNamespace("MAPI")
Set oFolder = oNamespace.GetDefaultFolder(olFolderCalendar)
' Get a CalendarSharing object for the Calendar default folder.
Set oCalendarSharing = oFolder.GetCalendarExporter

With oCalendarSharing
' Set the CalendarSharing object to export the contents of
' the Calendar folder for the next 3 months, excluding attachments
' and private items, in full detail.
.CalendarDetail = olFullDetails
.IncludeWholeCalendar = False
.IncludeAttachments = False
.IncludePrivateDetails = True
.RestrictToWorkingHours = False
.StartDate = Date - 1
.EndDate = Date + 90
End With
' Export calendar to an iCalendar calendar (.ics) file. Set path as required
oCalendarSharing.SaveAsICal "C:\Users\JoeBlogs\Documents\temp\" & _
Format(Date, "yyyy-mm-dd") & " cal.ics"
EndRoutine:
On Error GoTo 0
Set oCalendarSharing = Nothing
Set oFolder = Nothing
Set oNamespace = Nothing
Exit Sub
ErrRoutine:
Select Case Err.Number
Case 287 ' &H0000011F
' The user denied access to the Address Book.
' This error occurs if the code is run by an
' untrusted application, and the user chose not to
' allow access.
MsgBox "Access to Outlook was denied by the user.", _
vbOKOnly, _
Err.Number & " - " & Err.Source
Case -2147467259 ' &H80004005
' Export failed.
' This error typically occurs if the CalendarSharing
' method cannot export the calendar information because
' of conflicting property settings.
MsgBox Err.Description, _
vbOKOnly, _
Err.Number & " - " & Err.Source
Case -2147221233 ' &H8004010F
' Operation failed.
' This error typically occurs if the GetCalendarExporter method
' is called on a folder that doesn't contain calendar items.
MsgBox Err.Description, _
vbOKOnly, _
Err.Number & " - " & Err.Source
Case Else ' Any other error that may occur.
MsgBox Err.Description, _
vbOKOnly, _
Err.Number & " - " & Err.Source
End Select
GoTo EndRoutine
End Sub






this is based on code found on

msdn.microsoft.com/en-us/library/office/ff862395.aspx

sijpie
10-02-2013, 04:33 AM
Gee, I am glad I posted the code here, as Outlook had conveniently deleted the module... I was facing a rewrite when I realised I had put it here!