VBA help for uploading excel data to Outlook Calendar and skipping already uploaded data

wmark

New Member
Joined
Sep 19, 2017
Messages
2
I am trying to upload a list of due dates to my outlook calendar without any duplicates being uploaded from the file.

Code:
Public Function CheckAppointment(ByVal argCheckDate As Date, ByVal argSubject As Stringb, ByVal argbody As String) 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
    Dim dtCheck As Date
    Dim sbCheck As String
    Dim bdcheck As String
    
    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)


'check "test calendar for event
    
    CheckAppointment = False
    For Each oObject In oFolder.Items
        If oObject.Class = olAppointment Then
            Set oApptItem = oObject
            If oApptItem.Start = argCheckDate And oApptItem.Subject = argSubject And oApptItem.Body = argbody 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

This is the function i'm using to check if the event is in the Calendar

Code:
Public Sub Uploadduedates()

        ActiveSheet.Select
        
        On Error GoTo Err_Execute
        
        Dim olApp As Outlook.Application
        Dim olAppt As Outlook.AppointmentItem
        Dim blnCreated As Boolean
        Dim olNs As Outlook.Namespace
        Dim CalFolder As Outlook.MAPIFolder
        Dim subFolder As Outlook.MAPIFolder
        Dim arrCal As String
        
        Dim X As Long
        
        On Error Resume Next
        Set olApp = Outlook.Application
        
        If olApp Is Nothing Then
            Set olApp = Outlook.Application
            blnCreated = True
            Err.Clear
        Else
            blnCreated = False
        End If
        
        On Error GoTo 0
        
        Set olNs = olApp.GetNamespace("MAPI")
        Set CalFolder = olNs.GetDefaultFolder(olFolderCalendar)
        
        X = 2
        dtCheck = Cells(X, 4).Value
        sbCheck = Cells(X, 2).Value
        bdcheck = Cells(X, 3).Value
        Do Until Trim(Cells(X, 1).Value) = ""
            If CheckAppointment(dtCheck, sbCheck) = False Then
            
            
            
            
                
                arrCal = Cells(X, 1).Value
                Set subFolder = CalFolder.Folders(arrCal)
                


                
                Set olAppt = subFolder.Items.Add(olAppointmentItem)
                


                
                
                With olAppt
                    


                    .Start = Cells(X, 4) + Cells(X, 5)
                    .End = Cells(X, 6) + Cells(X, 7)
                    .Subject = Cells(X, 2)


                    .Body = Cells(X, 3)
                    


                    
                    .ReminderMinutesBeforeStart = Cells(X, 8) 'set reminder in mins 60 x 24 * [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=of]#of[/URL]  days
                 
                    
                    .ReminderSet = True


                    .Save
                    
                    
                End With
          


        End If
        
            X = X + 1
            
            
        Loop
        
        
        MsgBox ("Your Calender(s) have been updated")
        Set olAppt = Nothing
        Set olApp = Nothing
        
        Exit Sub
        
        
Err_Execute:
        MsgBox ("An error occurred - Exporting items to Calendar.")
        
    End Sub

This is my macro to upload my excel file to my calendar. The problem is it will still upload two duplicate entries in my test file.

I'm not sure why.

Can someone assist me?
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
I am trying to upload a list of due dates to my outlook calendar without any duplicates being uploaded from the file.

Code:
Public Function CheckAppointment(ByVal argCheckDate As Date, ByVal argSubject As Stringb, ByVal argbody As String) 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
    Dim dtCheck As Date
    Dim sbCheck As String
    Dim bdcheck As String
    
    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)


'check "test calendar for event
    
    CheckAppointment = False
    For Each oObject In oFolder.Items
        If oObject.Class = olAppointment Then
            Set oApptItem = oObject
            If oApptItem.Start = argCheckDate And oApptItem.Subject = argSubject And oApptItem.Body = argbody 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

This is the function i'm using to check if the event is in the Calendar

Code:
Public Sub Uploadduedates()

        ActiveSheet.Select
        
        On Error GoTo Err_Execute
        
        Dim olApp As Outlook.Application
        Dim olAppt As Outlook.AppointmentItem
        Dim blnCreated As Boolean
        Dim olNs As Outlook.Namespace
        Dim CalFolder As Outlook.MAPIFolder
        Dim subFolder As Outlook.MAPIFolder
        Dim arrCal As String
        
        Dim X As Long
        
        On Error Resume Next
        Set olApp = Outlook.Application
        
        If olApp Is Nothing Then
            Set olApp = Outlook.Application
            blnCreated = True
            Err.Clear
        Else
            blnCreated = False
        End If
        
        On Error GoTo 0
        
        Set olNs = olApp.GetNamespace("MAPI")
        Set CalFolder = olNs.GetDefaultFolder(olFolderCalendar)
        
        X = 2
        dtCheck = Cells(X, 4).Value
        sbCheck = Cells(X, 2).Value
        bdcheck = Cells(X, 3).Value
        Do Until Trim(Cells(X, 1).Value) = ""
            If CheckAppointment(dtCheck, sbCheck) = False Then
            
            
            
            
                
                arrCal = Cells(X, 1).Value
                Set subFolder = CalFolder.Folders(arrCal)
                


                
                Set olAppt = subFolder.Items.Add(olAppointmentItem)
                


                
                
                With olAppt
                    


                    .Start = Cells(X, 4) + Cells(X, 5)
                    .End = Cells(X, 6) + Cells(X, 7)
                    .Subject = Cells(X, 2)


                    .Body = Cells(X, 3)
                    


                    
                    .ReminderMinutesBeforeStart = Cells(X, 8) 'set reminder in mins 60 x 24 * [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=of"]#of[/URL]  days
                 
                    
                    .ReminderSet = True


                    .Save
                    
                    
                End With
          


        End If
        
            X = X + 1
            
            
        Loop
        
        
        MsgBox ("Your Calender(s) have been updated")
        Set olAppt = Nothing
        Set olApp = Nothing
        
        Exit Sub
        
        
Err_Execute:
        MsgBox ("An error occurred - Exporting items to Calendar.")
        
    End Sub

This is my macro to upload my excel file to my calendar. The problem is it will still upload two duplicate entries in my test file.

I'm not sure why.

Can someone assist me?

I figured out the problem after lunch.

Please close this thread.

Thanks.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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