Deleting an Outlook Appointment in VBA

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Cyd44
    New Member
    • Oct 2011
    • 101

    #16
    Fraid not ADezii,

    I am not getting any errors, have also tried taking out all reference to the .Start date and used just Subject & Location but it still comes back with Deleted 0 Appointments. This is crazy isnt it? Here is the code I am using which has been cahnged to the code you kindly gave me (I have made reference to .Start as a comment to see if this was the problem. Result of running the code is 0 Appointments deleted.

    I have a calendar Appointment created for Room 9, Cyd and 4/10/2011 for 9;00 - 10:3O and a table appointment for the same room and name for 9AM to 10:30AM. I am trying to amend the booking so the above details are the details captured for the old booking and this appointment is to be deleted from outlook. It doesnt delet it however. With no error it is diffilcult to know what is wrong.

    Code:
    Delete_Old_Appointment:
    
    ' The Following variable assignments will pick-up the old booking data prior to the change in order
        ' to find and delete an Outlook booking. Variables have been caputured by the Sub Form_Current()procedure
        ' and declared Public in the basMyEmpID Standard Code Module.
        
                Dim goldLocation As String
                Dim goldStart As Date
                Dim goldDate As Date
                Dim goldName As String
     
                goldLocation = basMyEmpID.gstroldLocation
                goldStart = basMyEmpID.gdteoldStart
                goldDate = basMyEmpID.gdteoldDate
                goldName = basMyEmpID.gstroldName
                
            
                
    ' Connection to Outlook Variables
     
                    
                    Dim objOlook As Outlook.Application
                    Dim objNamespace As Outlook.NameSpace
                    Dim objFolder As Outlook.MAPIFolder
                    Dim objAppointment As Outlook.AppointmentItem
                    Dim objOAppt As Outlook.Items
                    Dim lngDeletedAppointements As Long
                    Dim strSubject As String
                    Dim strLocation As String
                    Dim dteStartDate As Date
      
    '******************************** Set Criteria for DELETION here ********************************
                    strSubject = goldName
                    strLocation = goldLocation
                    dteStartDate = goldDate & " " & goldStart ' goldDate is Short Date goldStart is medium time
                
                    MsgBox (strSubject & dteStartDate & strLocation)
    '************************************************************************************************
      
                  Set objOlook = CreateObject("Outlook.Application")
                 Set objNamespace = objOlook.GetNamespace("MAPI")
                 Set objFolder = objNamespace.GetDefaultFolder(olFolderCalendar)
                 For Each objAppointment In objFolder.Items
                
                 
                If objAppointment.Subject = strSubject And objAppointment.Location = strLocation Then _
                'objAppointment.Start = CDate(Format$(dteStartDate, "Short Date"))
            
    
    
                 objAppointment.Delete
                 lngDeletedAppointements = lngDeletedAppointements + 1
                End If
    Next
      
    MsgBox lngDeletedAppointements & " appointment(s) DELETED.", vbInformation, "DETETE Appointments"
    
    'I 'm sure that you will now be able to adapt it to your specific
                    
                  GoTo Ender
                  Exit Sub

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #17
      Add the following Debugging Code to verify Object and variable Values (Lines 2, 3, and 4):
      Code:
      For Each objAppointment In objFolder.Items
        Debug.Print "Subject: " & objAppointment.Subject & vbCrLf & "Location: " & _
                     objAppointment.Location & vbCrLf & "Start: " & objAppointment.Start
        Debug.Print "Variable Values ==> " & " | " & strSubject & " | " & strLocation & " | " & dteStartDate
          If objAppointment.Subject = strSubject And objAppointment.Location = strLocation Then _
             objAppointment.Delete
               lngDeletedAppointements = lngDeletedAppointements + 1
          End If
        Debug.Print "************************************************************************"
      Next
      Sample OUTPUT:
      Code:
      Subject: Corporate
      Location: HQ
      Start: 12/26/2010
      Variable Values ==>  | Corporate | HQ | 11/15/2011
      *****************************************************
      Subject: Corporate
      Location: HQ
      Start: 11/1/2011
      Variable Values ==>  | Corporate | HQ | 11/15/2011
      *****************************************************
      Subject: Subject 1
      Location: Location 1
      Start: 11/18/2011
      Variable Values ==>  | Corporate | HQ | 11/15/2011
      *****************************************************
      Subject: Subject 2
      Location: Location 2
      Start: 11/21/2011
      Variable Values ==>  | Corporate | HQ | 11/15/2011
      *****************************************************

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32645

        #18
        @Cyd
        It seems ADezii is helping you along now, which is fine. For me to proceed I would need you to junk most of what you'd done since my last post as I feel you have skipped important steps in the process of understanding what you're building. Trying out new things is fine, but skipping from one thing to another (which itself is not thoroughly tested and understood) within the thread of a logic process seems to me to be an unreliable approach. Only sound logic will lead you to your goal, and that does not include unfounded stabs in the dark. It can seem slow and laborious, but if you deviate from that path you often find yourself back at the start point.

        I'll leave you in ADezii's very capable hands but I'll still monitor the thread in case I can help in any other ways.

        Comment

        • Cyd44
          New Member
          • Oct 2011
          • 101

          #19
          Hi ADezii,

          Panick over, I manged to get it working at last. I assigned the StartDate and Start time together outside of the If objAppointment statement and simply joined them as
          dteStartDate = StartDate + StartTime then called them within the If stayemen as = dteStartDate. This worked like a treat.

          I am sorry for all of the toing and froing, and NeoPa is quite correct, I was fumbling in the dark instead of standing back. The truth is, my problem was in understanding how to bind to Outlook properly to find and delete an appointment and there were no examples to be found anywhere.

          I do thank you so much as your help as been invaluable.
          I would also like to thank NeoPa and promise him I will think about my question in future before asking.

          Thanks a bunch guys.

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32645

            #20
            It's a long learning process Cyd, and you're clearly developing as time passes. Keep it up and I'll be asking you questions soon :-)

            Comment

            • mnorton123
              New Member
              • Mar 2021
              • 1

              #21
              Have to agree, that's outstanding, very slick, works perfectly.

              Comment

              Working...