Joel R
Joel R

Reputation: 1

How to sync Outlook Calendar with OneNote?

I found VBA code to sync my Outlook calendar with OneNote.

On line 7, I get

User-defined type not defined.

I have One Note 15 and Outlook 16 selected in references.

Sub SyncCalendarWithOneNote()
  Dim olApp As Outlook.Application
  Dim olCalendar As Outlook.Folder
  Dim olItems As Outlook.Items
  Dim olItem As Object
  Dim onApp As OneNote.Application
  Dim onNotebook As OneNote.Notebook
  Dim onSection As OneNote.Section
  Dim onPage As OneNote.Page
  Dim onPageContent As String
  Dim onPageID As String

  ' Connect to Outlook and OneNote
  Set olApp = Outlook.Application
  Set onApp = OneNote.Application

  ' Get the calendar folder and its items
  Set olCalendar = olApp.Session.GetDefaultFolder(olFolderCalendar)
  Set olItems = olCalendar.Items

  ' Loop through the calendar items
  For Each olItem In olItems
    ' Check if the item is an appointment
    If TypeOf olItem Is Outlook.AppointmentItem Then
      ' Get the appointment information
      Dim olSubject As String
      Dim olStart As Date
      Dim olEnd As Date
      olSubject = olItem.Subject
      olStart = olItem.Start
      olEnd = olItem.End

      ' Create a OneNote page for the appointment
      Set onNotebook = onApp.ActiveNotebook
      Set onSection = onNotebook.Sections("Calendar")
      onApp.CreateNewPage Onenote.nsHierarchyScopeSection, onSection.ID, onPageID
      Set onPage = onApp.GetPageContent(onPageID)
      onPageContent = "Subject: " & olSubject & vbCrLf & _
                      "Start: " & olStart & vbCrLf & _
                      "End: " & olEnd
      onApp.UpdatePageContent onPage.ID, onPageContent
    End If
  Next

  ' Clean up
  Set olCalendar = Nothing
  Set olItems = Nothing
  Set olItem = Nothing
  Set onApp = Nothing
  Set onNotebook = Nothing
  Set onSection = Nothing
  Set onPage = Nothing
End Sub

I want to sync Outlook Calendar with OneNote where a new note will be created for each calendar event.

Upvotes: 0

Views: 204

Answers (0)

Related Questions