Share via

Is there a way to automate the import/ export function for an outlook calendar?

Anonymous
2024-12-19T14:26:26+00:00

I regularly export an outlook calendar from a shared account to a CSV file which is incorporated into an excel dashboard. I managed to get a VBA code working with the help of MS copilot which exports a CSV file, however it only seems to fully populate the calendar from 2012, there are inputs from the last couple of years, but not the full daily calendar as it usually exports through the import/ export wizard. I notice that when it is done manually, you are prompted about date ranges and recurring instances being incompatible wth CSV, is this something that cannot be done without the wizard compiling it? The code I currently have working is below:

Sub ExportSpecificCalendarToCSV()

    Dim olApp As Outlook.Application

    Dim olNamespace As Outlook.NameSpace

    Dim olFolder As Outlook.MAPIFolder

    Dim olItems As Outlook.Items

    Dim olItem As Object

    Dim csvFile As String

    Dim output As String

    Dim calendarName As String

    Dim emailAddress As String

    Dim currentYear As Integer

    ' Initialize Outlook application

    Set olApp = New Outlook.Application

    Set olNamespace = olApp.GetNamespace("MAPI")

    ' Specify the calendar name and email address

    calendarName = "On Call Engineer" ' Change this to the name of your calendar

    emailAddress = "******@outlook.co,uk" ' Change this to your email address

    ' Get the specific calendar folder

    On Error Resume Next

    Set olFolder = olNamespace.Folders(emailAddress).Folders("Calendar").Folders(calendarName)

    On Error GoTo 0

    ' Check if the folder exists

    If olFolder Is Nothing Then

        MsgBox "Calendar not found!", vbExclamation, "Error"

        Exit Sub

    End If

    Set olItems = olFolder.Items

    ' Sort items by start date

    olItems.Sort "[Start]"

    ' Set the CSV file path

    csvFile = Environ("USERPROFILE") & "\Desktop\Exam dates on shift\ OUTLOOK calendar.csv"

    ' Create CSV header

    output = "Subject,Start Date,Start Time,End Date,End Time,Location,Body" & vbCrLf

    ' Get the current year

    currentYear = Year(Date)

    ' Loop through calendar items

    For Each olItem In olItems

        If TypeName(olItem) = "AppointmentItem" Then

            If Year(olItem.Start) = currentYear Then

                output = output & Chr(34) & olItem.Subject & Chr(34) & "," & _

                         Format(olItem.Start, "yyyy-mm-dd") & "," & _

                         Format(olItem.Start, "hh:mm AM/PM") & "," & _

                         Format(olItem.End, "yyyy-mm-dd") & "," & _

                         Format(olItem.End, "hh:mm AM/PM") & "," & _

                         Chr(34) & olItem.Location & Chr(34) & "," & _

                         Chr(34) & Replace(olItem.Body, vbCrLf, " ") & Chr(34) & vbCrLf

            End If

        End If

    Next olItem

    ' Write to CSV file

    Dim fileNum As Integer

    fileNum = FreeFile

    Open csvFile For Output As #fileNum

    Print #fileNum, output

    Close #fileNum

    ' Notify user

    MsgBox "Calendar exported to " & csvFile, vbInformation, "Export Complete"

End Sub

I did edit it to only export the current year from the calendar, however the CSV file only had 5 rows populated, which with a calendar of 365 days with a subject on each day, is clearly not right. Any help or guidance would be appreciated

Microsoft 365 and Office | Install, redeem, activate | Other | Other

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Anonymous
    2024-12-20T03:13:04+00:00
    • Hello, Joe Eason.

    Welcome to Microsoft community.

    -

    Based on your question is related to VBA , as a premium user, you can consider posting it on Stack Overflow(English only).

    Please post VBA programming questions to Stack Overflow using the vba tag and other relevant tags.

    Please note that Stack Overflow has some guidelines, such as requiring a descriptive title, a complete and concise problem statement, and enough details to reproduce your problem. Feature requests or overly broad questions will be considered off-topic.

    Disclaimer: Microsoft does not provide any warranties and/or guarantees, implied or otherwise, and is not responsible for any information or technology-related support you obtain from third-party linked websites.

    Hope the above information can help you find the direction to the problem.

    Best Regards,

    Hannah.Z- MSFT |Microsoft Community Support Specialist

    Was this answer helpful?

    0 comments No comments