Creat, check and delete duplicate event from Excel to Outlook

Lexlut

New Member
Joined
Sep 7, 2015
Messages
1
Hi everybody and thx to read my post (and my poor english).

I Just created a macro in order to :
1) check if an appoitment (by subject) is currently existing in my calendar
2) delete it if it was found
3) Creat a new one with the correct date (or the same if it was correct)

After a lot of forum visited, I created a prety simple code that do the job...But I have sometime the following error when the macro delete an appointment:
Execution error '-2147352567 (80020009)'
Automation ErrorException Occurred

If no appointment is found, the code add correctly the 3 appointment ( 1) )
The following times I run the macro, it do that:
2) delete the 1st and error
3) creat the 1st and delete the 2nd and error
4) delete and creat the 1st, creat the 2nd and delete the last and error
5) delete the 1st and error
6) Creat the 1st and delete the 2nd and error
7) creat all and back to the step 1)


Code:
Sub NouveauRDV_Calendrier()
Dim mbReply As VbMsgBoxResult
Dim OkApp As New Outlook.Application
Dim Rdv As Outlook.AppointmentItem

 flag = 5
While Cells(flag, 8) <> "" 'flag = 5 To 10
      'Test for duplicate event
    Set myolApp = CreateObject("Outlook.Application")
    Set myNameSpace = myolApp.GetNamespace("MAPI")
    Set myolApp.ActiveExplorer.CurrentFolder = myNameSpace.GetDefaultFolder(olFolderCalendar)
    Set outlookitems = myolApp.ActiveExplorer.CurrentFolder.Items
    Cpte = outlookitems.Count

  
        For x = 1 To Cpte
            'same subject => delete
            If outlookitems(x).Subject = Cells(flag, 1) Then
                'outlookitems(x).Delete
                    outlookitems(x).Delete
            End If

        Next x

'in every case, I creat the new appointment
Set Rdv = OkApp.CreateItem(olAppointmentItem)


With Rdv
    .MeetingStatus = olMeeting
    .Subject = Cells(flag, 1)
    .Body = Cells(flag, 2)
    .Location = Cells(flag, 5)
    .Start = Cells(flag, 8) & " 09:00:00" 
    .Duration = 60 'minutes
    .Categories = "AUDIT"
    .Save
End With
flag = flag + 1

Wend
Set OkApp = Nothing

End Sub

I already actived the Object Library
I'm in Excel 2013
Outlook 2013
Windows 7

About my sheet, I have 3 lines (1 for each appoitment) with subjet in Row A and date in Row 8, etc.
My appointment are in line 5, 6 and 7

I hope my post is complete and clear as possible :)
Thx for helping

Lexlut
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

Forum statistics

Threads
1,223,275
Messages
6,171,127
Members
452,381
Latest member
Nova88

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