VBA Macro to Check for Calendar Items in Outlook and Update if Details Changed

Sangdrax

New Member
Joined
Oct 13, 2014
Messages
9
Good Morning All,

I'm trying to make a macro that links an excel list of meetings with an outlook calendar.

I have created a macro so far that is able to create the entries and check to see if the entry exists by a specific date (which doesn't allow for any flexibility).

I now need to make it so it checks by subject line and date/time (which vary on each row in the spreadsheet), and if the item exists, update all the values for the meeting (time/place/body etc.) and if that subject line doesn't exist, then create a new calendar item.

Please see my code below (the red part is where I need to update I think).

Code:
[COLOR=#ff0000]Sub Button1_Click()[/COLOR]
[COLOR=#ff0000]    MsgBox (CheckAppointment("1/02/2015, 09:00:00AM")))[/COLOR]
[COLOR=#ff0000]End Sub[/COLOR]


Public Function CheckAppointment(ByVal argCheckDate As Date) As Boolean
 
  Dim oApp As Outlook.Application
  Dim oNameSpace As Outlook.Namespace
  Dim oApptItem As Outlook.AppointmentItem
  Dim oFolder As Outlook.MAPIFolder
  Dim oMeetingoApptItem As Outlook.MeetingItem
  Dim oObject As Object
  
  On Error Resume Next
  ' check if Outlook is running
  Set oApp = GetObject("Outlook.Application")
  If Err <> 0 Then
    'if not running, start it
    Set oApp = CreateObject("Outlook.Application")
  End If
  
  Set oNameSpace = oApp.GetNamespace("MAPI")
  Set oFolder = oNameSpace.GetDefaultFolder(olFolderCalendar)
  
  CheckAppointment = False
  For Each oObject In oFolder.Items
    If oObject.Class = olAppointment Then
      Set oApptItem = oObject
      If oApptItem.Start = argCheckDate Then
        CheckAppointment = True
      End If
    End If
  Next oObject
    
  Set oApp = Nothing
  Set oNameSpace = Nothing
  Set oApptItem = Nothing
  Set oFolder = Nothing
  Set oObject = Nothing
   
End Function


Sub CreateAppointment()
    Dim myOlApp As Outlook.Application
    Dim myItem As Outlook.AppointmentItem
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long
   
    Set myOlApp = GetObject(, "Outlook.Application")
    Set ws = Sheets("Sheet1")   'Edit to your worksheet name
   
    With ws
        lastRow = .Cells(1, "A").End(xlUp).Row + 1  'Last row of data
    End With
   
    For i = 2 To lastRow  'Starting at 2 assumes column headers on row 1
        'The following line adds one appointment item for each loop
        Set myItem = myOlApp.CreateItem(olAppointmentItem)
        With myItem
            .Subject = ws.Cells(i, "A")
            .Location = ws.Cells(i, "B")
            .Body = ws.Cells(i, "C")
            .Start = ws.Cells(i, "D") + ws.Cells(i, "E")
            .End = ws.Cells(i, "F") + ws.Cells(i, "G")
            .Save
        End With
    Next i
End Sub

If you're able to assist with making this so it checks each calendar event by subject and updates all events accordingly, that'd be greatly appreciated.

Kind Regards,

S
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Code:
[COLOR=#ff0000]Sub Button1_Click()[/COLOR]
    [COLOR=#ff0000]MsgBox (CheckAppointment("1/02/2015, 09:00:00AM"[/COLOR][COLOR=#0000ff], "SUBJECT LINE GOES HERE"[/COLOR][COLOR=#ff0000])))[/COLOR]
[COLOR=#ff0000]End Sub[/COLOR]


[COLOR=#0000ff]Public Function CheckAppointment(ByVal argCheckDate As Date ByVal argCheckSubject) As Boolean[/COLOR]
 
  Dim oApp As Outlook.Application
  Dim oNameSpace As Outlook.Namespace
  Dim oApptItem As Outlook.AppointmentItem
  Dim oFolder As Outlook.MAPIFolder
  Dim oMeetingoApptItem As Outlook.MeetingItem
  Dim oObject As Object
  
  On Error Resume Next
  ' check if Outlook is running
  Set oApp = GetObject("Outlook.Application")
  If Err <> 0 Then
    'if not running, start it
    Set oApp = CreateObject("Outlook.Application")
  End If
  
  Set oNameSpace = oApp.GetNamespace("MAPI")
  Set oFolder = oNameSpace.GetDefaultFolder(olFolderCalendar)
  
  CheckAppointment = False
  For Each oObject In oFolder.Items
    If oObject.Class = olAppointment Then
      Set oApptItem = oObject
      If oApptItem.Start = argCheckDate Then
        CheckAppointment = True
      End If
    End If
  Next oObject
    
  Set oApp = Nothing
  Set oNameSpace = Nothing
  Set oApptItem = Nothing
  Set oFolder = Nothing
  Set oObject = Nothing
   
End Function


Sub CreateAppointment()
    Dim myOlApp As Outlook.Application
    Dim myItem As Outlook.AppointmentItem
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long
   
    Set myOlApp = GetObject(, "Outlook.Application")
    Set ws = Sheets("Sheet1")   'Edit to your worksheet name
   
    With ws
        lastRow = .Cells(1, "A").End(xlUp).Row + 1  'Last row of data
    End With
   
    For i = 2 To lastRow  'Starting at 2 assumes column headers on row 1
        'The following line adds one appointment item for each loop
        Set myItem = myOlApp.CreateItem(olAppointmentItem)
        With myItem
            .Subject = ws.Cells(i, "A")
            .Location = ws.Cells(i, "B")
            .Body = ws.Cells(i, "C")
            .Start = ws.Cells(i, "D") + ws.Cells(i, "E")
            .End = ws.Cells(i, "F") + ws.Cells(i, "G")
            .Save
        End With
    Next i
End Sub
I was able to get it to work by making the adjustments as shown above.

Bryan
 
Upvote 0

Forum statistics

Threads
1,215,226
Messages
6,123,734
Members
449,116
Latest member
alexlomt

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top