473,883 Members | 1,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deleting an Outlook Appointment in VBA

101 New Member
I am trying to delete an outlook appointment based upon the value of variables for Location, Start Date, Start Time & Name (Subject)

I am at a loss as where to start on declarations for Outlook Objects. I found some code on the Web and tried to alter it. Could you please help with the declarations for finding the appointment in Outlook. My rubbish attempt at coding is as follows:-
Expand|Select|Wrap|Line Numbers
  1. Dim olApp As Outlook.Application
  2.   Dim objAppts As Outlook.AppointmentItem
  4.   Dim AP_Date As Date
  5.   Dim AP_Start_Time As Date
  6.   Dim AP_Location As String
  7.   Dim AP_Subject As String
  9.   Dim OutlookStartTime As Date
  10.   Dim OutlookLocation As String
  11.   Dim OutlookSubject As String
  12.   Dim sFilter As Variant
  14.   AP_Date = Module1.oldDate
  15.   AP_Start_Time = Module1.oldStart
  16.   AP_Location = Module1.oldLocation
  17.   AP_Subject = Module1.oldName
  19.   OutlookStartTime = CDate(AP_Date & " " & AP_Start_Time)
  20.   OutlookLocation = (AP_Location)
  21.   OutlookSubject = (AP_Subject)
  23.   Set olApp = CreateObject("Outlook.Application")
  24.   Set objAppointments = objAppts.DeleteItem(olAppointmentItem)
  26.   sFilter = "[Start] = '" & OutlookStartTime & _
  27.     "' And [Location] = '" & OutlookLocation & "' " & _
  28.     " And [Subject] = '" & OutlookSubject & "'"
  30.   Set objAppointments = objAppointments.Items.Find(sFilter)
  32.   If Not TypeName(objAppointments) = "Nothing" Then
  33.     MsgBox ("Appointment Found")
  34.     'objAppointment.Delete
  35.   End If
  37.   Set objAppointments = Nothing
  38.   Set objAppts = Nothing
  39.   Exit Sub
If I can have some help on declaration I could possibly work out the rest.

Many thanks in advance
Oct 28 '11
20 17952
8,834 Recognized Expert Expert
Here is some that that I simplified and adapted to your general needs. It will DELETE All Outlook Appointments where the Subject = 'Corporate', the Location = 'HQ', and which occur after 11/15/2011. The Msgbox will then tell you how many Appointments were actually Deleted.
  1. Set a Reference to the Microsoft Outlook X.X Object Library.
  2. Execute the following Code substituting you own Values for Lines 11 to 13.
    Expand|Select|Wrap|Line Numbers
    1. Dim objOutlook As Outlook.Application
    2. Dim objNamespace As Outlook.NameSpace
    3. Dim objFolder As Outlook.MAPIFolder
    4. Dim objAppointment As Outlook.AppointmentItem
    5. Dim lngDeletedAppointements As Long
    6. Dim strSubject As String
    7. Dim strLocation As String
    8. Dim dteStartDate As Date
    10. '******************************** Set Criteria for DELETION here ********************************
    11. strSubject = "Corporate"
    12. strLocation = "HQ"
    13. dteStartDate = #11/15/2011#
    14. '************************************************************************************************
    16. Set objOutlook = Outlook.Application
    17. Set objNamespace = objOutlook.GetNamespace("MAPI")
    18. Set objFolder = objNamespace.GetDefaultFolder(olFolderCalendar)
    20. For Each objAppointment In objFolder.Items
    21.   If objAppointment.Subject = strSubject And objAppointment.Location = strLocation And _
    22.      objAppointment.Start > dteStartDate Then
    23.        objAppointment.Delete
    24.          lngDeletedAppointements = lngDeletedAppointements + 1
    25.   End If
    26. Next
    28. MsgBox lngDeletedAppointements & " appointment(s) DELETED.", vbInformation, "DETETE Appointments"
  3. I'm sure that you will now be able to adapt it to your specific needs.
Nov 1 '11 #11
101 New Member
Hi ADezii

Once again, you are a star, this worked fine and did not find an appointment as I did not have one there. I am sure it will delete one once I have added it. Nonetheless your scipt proved spot on. I have been pulling my hair out over this as I could not work out the logic for the decalarations and the finding of an appointment that match.

I have set you as Best answer and thank you so much
Nov 1 '11 #12
8,834 Recognized Expert Expert
You are quite welcome. I actually pulled some Code off the Internet and modified it, so I do not deserve all the credit here. Glad it all worked out for you.

P.S. - NeoPa put much more time into this Thread than I did, he deserves the majority of the credit.
Nov 1 '11 #13
101 New Member
Oops Adezii, I spoke too soon. The script appears to look in the calendar but does not delete one that I created in line with the variables.
I suspect it something to do with the .Start Outlook variable. I have declared my dteStartDate as StartDate & " " & StartTime. I know that the variable comes back as (say) 04/10/2011 09:00:00 but I think that Outlook may be interpreting this in a different way because I have read somewhere that we need to supress seconds?
Is there a .Variable within Outlook for the Start Time or must we put Date & time together to creat .Start?
Nov 1 '11 #14
8,834 Recognized Expert Expert
Expand|Select|Wrap|Line Numbers
  1. 'Extract the Date Component only
  2. AND objApointment.Start = CDate(Format$(dteStartDate, "Short Date"))
Nov 1 '11 #15
101 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. Delete_Old_Appointment:
  3. ' The Following variable assignments will pick-up the old booking data prior to the change in order
  4.     ' to find and delete an Outlook booking. Variables have been caputured by the Sub Form_Current()procedure
  5.     ' and declared Public in the basMyEmpID Standard Code Module.
  7.             Dim goldLocation As String
  8.             Dim goldStart As Date
  9.             Dim goldDate As Date
  10.             Dim goldName As String
  12.             goldLocation = basMyEmpID.gstroldLocation
  13.             goldStart = basMyEmpID.gdteoldStart
  14.             goldDate = basMyEmpID.gdteoldDate
  15.             goldName = basMyEmpID.gstroldName
  19. ' Connection to Outlook Variables
  22.                 Dim objOlook As Outlook.Application
  23.                 Dim objNamespace As Outlook.NameSpace
  24.                 Dim objFolder As Outlook.MAPIFolder
  25.                 Dim objAppointment As Outlook.AppointmentItem
  26.                 Dim objOAppt As Outlook.Items
  27.                 Dim lngDeletedAppointements As Long
  28.                 Dim strSubject As String
  29.                 Dim strLocation As String
  30.                 Dim dteStartDate As Date
  32. '******************************** Set Criteria for DELETION here ********************************
  33.                 strSubject = goldName
  34.                 strLocation = goldLocation
  35.                 dteStartDate = goldDate & " " & goldStart ' goldDate is Short Date goldStart is medium time
  37.                 MsgBox (strSubject & dteStartDate & strLocation)
  38. '************************************************************************************************
  40.               Set objOlook = CreateObject("Outlook.Application")
  41.              Set objNamespace = objOlook.GetNamespace("MAPI")
  42.              Set objFolder = objNamespace.GetDefaultFolder(olFolderCalendar)
  43.              For Each objAppointment In objFolder.Items
  46.             If objAppointment.Subject = strSubject And objAppointment.Location = strLocation Then _
  47.             'objAppointment.Start = CDate(Format$(dteStartDate, "Short Date"))
  51.              objAppointment.Delete
  52.              lngDeletedAppointements = lngDeletedAppointements + 1
  53.             End If
  54. Next
  56. MsgBox lngDeletedAppointements & " appointment(s) DELETED.", vbInformation, "DETETE Appointments"
  58. 'I 'm sure that you will now be able to adapt it to your specific
  60.               GoTo Ender
  61.               Exit Sub
Nov 1 '11 #16
8,834 Recognized Expert Expert
Add the following Debugging Code to verify Object and variable Values (Lines 2, 3, and 4):
Expand|Select|Wrap|Line Numbers
  1. For Each objAppointment In objFolder.Items
  2.   Debug.Print "Subject: " & objAppointment.Subject & vbCrLf & "Location: " & _
  3.                objAppointment.Location & vbCrLf & "Start: " & objAppointment.Start
  4.   Debug.Print "Variable Values ==> " & " | " & strSubject & " | " & strLocation & " | " & dteStartDate
  5.     If objAppointment.Subject = strSubject And objAppointment.Location = strLocation Then _
  6.        objAppointment.Delete
  7.          lngDeletedAppointements = lngDeletedAppointements + 1
  8.     End If
  9.   Debug.Print "************************************************************************"
  10. Next
Sample OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. Subject: Corporate
  2. Location: HQ
  3. Start: 12/26/2010
  4. Variable Values ==>  | Corporate | HQ | 11/15/2011
  5. *****************************************************
  6. Subject: Corporate
  7. Location: HQ
  8. Start: 11/1/2011
  9. Variable Values ==>  | Corporate | HQ | 11/15/2011
  10. *****************************************************
  11. Subject: Subject 1
  12. Location: Location 1
  13. Start: 11/18/2011
  14. Variable Values ==>  | Corporate | HQ | 11/15/2011
  15. *****************************************************
  16. Subject: Subject 2
  17. Location: Location 2
  18. Start: 11/21/2011
  19. Variable Values ==>  | Corporate | HQ | 11/15/2011
  20. *****************************************************
Nov 1 '11 #17
32,584 Recognized Expert Moderator MVP
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.
Nov 2 '11 #18
101 New Member
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.
Nov 2 '11 #19
32,584 Recognized Expert Moderator MVP
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 :-)
Nov 2 '11 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Akira | last post by:
Could someone tell me how to send outlook appointment email from asp page? I would like appointment email just like one outlook can send out. I tried to look for an answer for this, but it seems exchange server has to be installed on web server if I want to use the following code?? Is there way to send outlook appointment email without using outlook.Application object? If not, how can I make the code below work since we have separate...
by: Sam | last post by:
My db looks after the hiring and lending of equipment, the form which books out equipment hired prints a signout sheet and automatically inserts an appointment into outlook advising the operator on the due date that a particular hire is due. I have used microsoft's automated appointment code to do this, what I would also like the code to do is to email an appointment to the person hiring the gear. Knowing that you can email an...
by: Kamyk | last post by:
Hello all! I have the following problem. I know how to create a mail in VB with a subject and people to send, but is it possible to create an Outlook appointment in VB, if it is possible, please be so kind and give me some example? Thank you in advance Marcin from Poland
by: Kamyk | last post by:
Hello all! I have created a function in VB which generate appointment in Outlook. Everything is OK, except one thing. I cannot send it automatically using a button in Access, because I don`t know what is the code to send it automatically. I noticed that if I click on Calendar>"click on selected appointment">menu>Appointment>Invite Atendees everything is going well and this appointment is sent to attendees given on the list. My question...
by: Georges | last post by:
Hi, Can anyone help me with this. I would like to create outlook appointment from a web application. I tried to look at the CDO, WebDAV and MAPI documentations without success. what is the best way of creating an appointment? We are using W2000, Office 2000, Exchange 2000 and .net framework. any ideas would be appreciated. (code example more than welcome) cheers, George
by: chuckdfoster | last post by:
Is there a way to create an New Outlook Appointment from an ASP.NET application? I would also need to check if Outlook is running first. Any suggestions, advice?
by: Ori :) | last post by:
I am writing an application to retrieve appointments from Outlook, I have the appointment items sorted by "Start" field and now I want to find the first record which's "Start" value is later than now (future appointment The relevant piece of my current code Dim oCalendar As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar Dim oItems As Outlook.Items = oCalendar.Item oItems.Sort("Start"...
by: berend.kapelle | last post by:
Hi, i found this code from outlookcode.com http://www.outlookcode.com/codedetail.aspx?id=139 Its written in VB, and i have some problem porting it to C#. The problem is getting the right appointment. An Exception occurs in this line: MAPI.Field oField = (MAPI.Field) oFields.get_Item(CdoAppt_Colors, CdoPropSetID1);
by: keri | last post by:
Hi everyone, I have searched for info on this topic but have not found anything. Basically I have a db that posts appointments to outlook. I would like to be able to set all of the outlook appointment labels (names and colour shading) from my db then assign them to the appointments I put into outlook. It seems that this can not be done simply from VB but if anyone can give me some pointers on where I can start to do this, or anything I
by: Vic Rensburg | last post by:
Dear All, I have a Call Logger database with a scheduler function. I've managed to book the appointments and set reminders in Outlook 2007 via access vba, but I'ma bit stuck in calling the appointment information via using the subject and then amending the date and time. I would really appreciate if you couldprovide some sample code of how this possibly could be achieved. Please help. Many thanks, Vic
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.