Excel VBA: Outlook Synchronize my work email calendar with my Outlook.com calendar


I have two accounts in my Outlook 2010. The image shows my two calendars in overlay mode.
What Excel VBA code would I use to copy all appointments from my work calendar in green to my Outlook
calendar in blue?

Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ProfessorJimJamMicrosoft Excel ExpertCommented:
Put this code in "ThisOutlookSession" module.

Replace FOLDERNAME with the location of the non-default calendar that you will be putting appointments on. They will then be copied to your "Personal Folders\Calendar"

Public WithEvents myOlItems As Outlook.Items

Private Sub Application_Startup()

    Set fld = GetFolder("FOLDERNAME")
    If Not fld Is Nothing Then
     Set myOlItems = fld.Items
    End If
End Sub

Private Sub myOlItems_ItemAdd(ByVal Item As Object)
    Dim copyAppt As Outlook.AppointmentItem
    Dim movedAppt As Outlook.AppointmentItem
    ' Add new appointment to second calendar
    Set copyAppt = Item.Copy
    Set fld = GetFolder("Personal Folders\Calendar")
    If Not fld Is Nothing Then
        Set movedAppt = copyAppt.Move(fld)
    End If

End Sub

Public Function GetFolder(strFolderPath As String) As MAPIFolder
  ' folder path needs to be something like
  ' " source of code outlookcode DOT com"
  Dim objApp As Outlook.Application
  Dim objNS As Outlook.NameSpace
  Dim colFolders As Outlook.Folders
  Dim objFolder As Outlook.MAPIFolder
  Dim arrFolders() As String
  Dim I As Long
  On Error Resume Next

  strFolderPath = Replace(strFolderPath, "/", "\")
  arrFolders() = Split(strFolderPath, "\")
  Set objApp = CreateObject("Outlook.Application")
  Set objNS = objApp.GetNamespace("MAPI")
  Set objFolder = objNS.Folders.Item(arrFolders(0))
  If Not objFolder Is Nothing Then
    For I = 1 To UBound(arrFolders)
      Set colFolders = objFolder.Folders
      Set objFolder = Nothing
      Set objFolder = colFolders.Item(arrFolders(I))
      If objFolder Is Nothing Then
        Exit For
      End If
  End If

  Set GetFolder = objFolder
  Set colFolders = Nothing
  Set objNS = Nothing
  Set objApp = Nothing
End Function

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks. Sorry for late acceptance
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.