473,668 Members | 2,407 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
  3.  
  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
  8.  
  9.   Dim OutlookStartTime As Date
  10.   Dim OutlookLocation As String
  11.   Dim OutlookSubject As String
  12.   Dim sFilter As Variant
  13.  
  14.   AP_Date = Module1.oldDate
  15.   AP_Start_Time = Module1.oldStart
  16.   AP_Location = Module1.oldLocation
  17.   AP_Subject = Module1.oldName
  18.  
  19.   OutlookStartTime = CDate(AP_Date & " " & AP_Start_Time)
  20.   OutlookLocation = (AP_Location)
  21.   OutlookSubject = (AP_Subject)
  22.  
  23.   Set olApp = CreateObject("Outlook.Application")
  24.   Set objAppointments = objAppts.DeleteItem(olAppointmentItem)
  25.  
  26.   sFilter = "[Start] = '" & OutlookStartTime & _
  27.     "' And [Location] = '" & OutlookLocation & "' " & _
  28.     " And [Subject] = '" & OutlookSubject & "'"
  29.  
  30.   Set objAppointments = objAppointments.Items.Find(sFilter)
  31.  
  32.   If Not TypeName(objAppointments) = "Nothing" Then
  33.     MsgBox ("Appointment Found")
  34.     'objAppointment.Delete
  35.   End If
  36.  
  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 #1
20 17881
NeoPa
32,568 Recognized Expert Moderator MVP
Cyd, you need to post the question with care. Not just dump code in and expect us to work out what the question should be. Describe what you're actually doing and where you get stuck. The declarations seem fine to me, but you don't really provide a question to work with.

General tips on items and how they fit into the Outlook class structure can be found by using Object Explorer within the VBA IDE. You can do it from Access too, but probably easier to do it in the native application. See Debugging in VBA for how to invoke and have a play.
Oct 28 '11 #2
Cyd44
101 New Member
Ok NeoPa Sorry if I am not asking correctly. I will try to explain better.

I have successfully managed to create appointments in Outlook using the following sub:-
Expand|Select|Wrap|Line Numbers
  1. 'Add a new appointment.
  2.  
  3.                 Dim objOutlook As Outlook.Application
  4.                 Dim objAppt As Outlook.AppointmentItem
  5.                 'Dim objRecurPattern As Outlook.RecurrencePattern
  6.  
  7.                 Set objOutlook = CreateObject("Outlook.Application")
  8.                 Set objAppt = objOutlook.CreateItem(olAppointmentItem)
  9.  
  10.                 With objAppt
  11.                     .Start = Me!BookStartDate & " " & Me!BookTime
  12.                     .Duration = Me!TImeConversion
  13.                     .Subject = Me!BookName
  14.                     .Location = Me!BookLocation
  15.                     If Not IsNull(Me!BookNotes) Then .Body = Me!BookNotes
  16.  
  17.                     .Save
  18.                     .Close (olSave)
  19.                     End With
  20.                     'Release the AppointmentItem object variable.
  21.                     Set objAppt = Nothing
  22.  
  23.  
  24.             'Release the Outlook object variable.
  25.               Set objOutlook = Nothing
  26.               MsgBox " Changes have been Saved and  New Appointment Added to Outlook!"
I now wish to turn this around so that find an appointment by using the unique identfiers which are Location, StartDate, BookTime, Location and BookName. I want to delete this appointment if found.

I am unsure as to how I call outlook to find and delete the appointment using the above variables as I do not know the correct variable decalartions for Outlook (IE Setting Object required)

I hope this clarifies my position. I can see now that giving you code that is not working has confused things. When I try to run the delete sub I keep getting a mismatch error on declaring the Start date as both StartDate & StartTime. I do not actually understand why we cannot set the date and time separately.
Oct 28 '11 #3
NeoPa
32,568 Recognized Expert Moderator MVP
It wasn't the code that confused things. It was the lack of any discernable question or even explanation of why the code is there at all. I'm sure I've linked you before to how to ask questions and what to do when posting code that isn't working as expected. The line number and error message are necessary as well as explaining what is going on. All these important elements are again missing from the question. You should also include the code that defines the procedure like the header so that when you refer to a 'Delete Sub' I don't go through all the code again just to find that nothing is identified as a 'Delete Sub'. I guess it's your first bit of code, but I frankly ought not to be spending my time guessing what you should be telling me. I guess also you're referring to line #19, but all the lines are numbered. If you use the numbers that are provided I don't need to try to read your mind.

Assuming then, that it is indeed line #19 of your first posted code that is causing some unspecified problem, I would suggest that you breakpoint your code at line #19 and see what the string representations are of [AP_Date] and [AP_Start_Time] (Debugging in VBA may help with this). I have no way of knowing what data you have in there so I can only guess. If they are a valid date and time stored in Date format though, you may get away with :
Expand|Select|Wrap|Line Numbers
  1. OutlookStartTime = AP_Date + AP_Start_Time
Remember, Include the procedure declaration when posting a procedure (Not always necessary for small snippets but when posting a whole procedure it's very important). Especially if you want to refer to the named procedure in your explanation.
Oct 29 '11 #4
Cyd44
101 New Member
Back to this problem having first worked out how to capture pre-edit data.

My problem here is with lack of knowledge on the syntax required to connect to Outlook and find an appointment for deletion. I have comented the code so that you can see what I am trying to achieve here. Am getting a complie error on GetdefaultFolde r and I am not sure of my logic for the declarations surounding Outlook.

I hope this question makes sense as I think you can see what I am trying to do within the code. All the record field variables work fine and the values are as expected. My major problem is trying to understand the object requirements for OutLook.
My code is as follows:-
Expand|Select|Wrap|Line Numbers
  1. Delete_Old_Appointment:
  2. ' The Following variable assignments will pick-up the old booking data prior to the change in order
  3. ' to find and delete an Outlook booking. Variables have been caputured by the Sub Form_Current()procedure
  4. ' and declared Public in the basMyEmpID Standard Code Module.
  5.  
  6.     Dim goldLocation As String
  7.     Dim goldStart As Date
  8.     Dim goldDate As Date
  9.     Dim goldName As String
  10.  
  11.     goldLocation = basMyEmpID.gstroldLocation
  12.     goldStart = basMyEmpID.gdteoldStart
  13.     goldDate = basMyEmpID.gdteoldDate
  14.     goldName = basMyEmpID.gstroldName
  15.  
  16.     MsgBox (goldLocation & goldStart & goldDate & goldName) ' Take Out when fully developed
  17.  
  18. ' Connection to Outlook Variables
  19.  
  20.     Dim objOlook As outlook.Application
  21.     Dim objFolder As outlook.MAPIFolder
  22.     Dim objOAppt As outlook.AppointmentItem
  23.     Dim objOItems As outlook.Items
  24.     Dim appStart As Variant
  25.     Dim strFilter As String
  26.  
  27.     Set objOlook = CreateObject("Outlook.Application")
  28.     Set objFolder = objOlook.GetNamespace("MAPI").GetDefaultFolder
  29.     Set objOAppt = objFolder.Items
  30.     Set objOItems = objOAppt.Item
  31.  
  32.  ' Set the Filter String to find Outlook Appointment
  33.  
  34.     appStart = goldDate & " " & goldStart ' Joins Date & Time together for OLook
  35.     MsgBox (appStart)
  36.     strFilter = "[Start]= " '" appStart & "'" & " And [Location]= " & "'" & goldLocation & "'" & " And [Subject]= " & "'" & goldName & "'"
  37.  
  38.  ' Find the outlook appointment based upon the above strFilter condition
  39.  
  40.     Set objOAppt = objOItems.Find(strFilter)
  41.     MsgBox (strFilter)
  42.     ' objOAppt.Delete
  43.     '.Close (olSave)
  44.     'Release the AppointmentItem object variable.
  45.     Set objOAppt = Nothing
  46.  
  47.     'Release the Outlook object variable.
  48.     Set objOlook = Nothing
  49.     MsgBox (" Old Appointment Has been deleted in Outlook!")
  50.     GoTo Add_New_Appointment
  51. Exit Sub
Could you please advise how the outlook objects should look?
Oct 30 '11 #5
NeoPa
32,568 Recognized Expert Moderator MVP
I'm still struggling to understand what you're asking for Cyd. Still no line numbers to refer to. However, my best guess is that you're asking about the code in lines #20 through #30, as these seem to declare and set the Outlook objects.

Lines #20 through #25 :
objOlook, objFolder, objOAppt, objOItems and strFilter all appear to be declared perfectly. appStart is declared as a Variant, no doubt in line with what's been said earlier, but the way it is assigned (Line #34) stops any possibility of its being used to determine if a value exists or not as a Null. Line #34 will always return a valid string - in all circumstances.

Lines #27 through #30 :
#27 and #28 look fine, but lines #29 and #30 appear to be assigning each other's values. IE They should probably read :
Expand|Select|Wrap|Line Numbers
  1. Set objOItems = objFolder.Items
  2. Set objOAppt = objOItems(???)
Of course you probably don't know yet which item in objOItems() to select.

Line #36 (Bonus) :
It is not necessary to concatenate literal strings together using the format "xxx" & "yyy". The result is much easier and more logically achieved by simply using a single string "xxxyyy".
Expand|Select|Wrap|Line Numbers
  1. strFilter = "[Start]= '" & appStart & "' AND [Location]= '" & goldLocation & "' AND [Subject]= '" & goldName & "'"
Oct 30 '11 #6
Cyd44
101 New Member
Sorry again if I am confusing you. I am realy struggling with the logic of declaring Outlook objects when I want to find an appointment (rather than create one).

I do not understand how to declare the Set obj variables I am afraid.

I have connected successfully to create an appointment but do not know what I need to do to just open the calendar and find one.

Is there not a generic set of object setting to open appointments to find one.?

I suppose my question should be

How do you set the objects to connect to outlook in order to find an appointment to delete.? I have played around so much now I am totally confused. when I search the web I cannot find a diffinitive answer and have seen so many variations....n on of which answer my basic problem.

I have noted where you suggest change and have done so but what would I set objOAppt to....I have not a clue I am afraid
Oct 30 '11 #7
NeoPa
32,568 Recognized Expert Moderator MVP
It's so much easier to work with posts that are as clear as this one Cyd. Let's see what we can do with it.

First let's check out Application Automation for some of the basics. Frankly, your basics are already not too far off the mark, but it doesn't hurt to synchronise.

Assuming you're up on all the points from there then, your code seems to get the Outlook.Applica tion object ok (Remember it is not visible at this stage). Next it gets the default MAPI folder (This will probably be the Inbox folder). If you want the Calendar folder specifically, then you can use either of :
Expand|Select|Wrap|Line Numbers
  1. Set objFolder = objOlook.GetNamespace("MAPI").GetDefaultFolder(olFolderCalendar)
  2. Set objFolder = objOlook.GetNamespace("MAPI").Folders("Calendar")
After that your new code is fine for setting the Items object but then I suspect you'd need to use its Find() method.

Expand|Select|Wrap|Line Numbers
  1. Set objOItems = objFolder.Items
  2. Set objOAppt = objOItems.Find(strFilter)
Of course, your code doesn't currently have any strFilter set, so you should probably be looking to set it up first, to match your requirements. At this point I need to point you at the Context-Sensitive Help article again as you need to look for yourself. There is a (pretty large) list in there of the available fields that can be matched and it seems none of the values you are searching on have a match to any of the available fields.
Oct 30 '11 #8
NeoPa
32,568 Recognized Expert Moderator MVP
It appears I misread the help page. The list was one of invalid properties to check rather than one of the only valid properties. Nevertheless, look through it yourself for most benefit. It gives some examples and how to avoid the more obvious errors.

NB. Choose (click on) Find method as it applies to the Items object.
Oct 30 '11 #9
Cyd44
101 New Member
Hi NeoPa,

Have looked at all of your recomendation and am still no wiser for my dilema I am afraid. I have already been successful in Creating an appointment on Outlook and there are many items on the web dealing with creation. However, I cannot find any items dealing specifically with deleting an appointment which I find strange as I would have thought this was as normal as Creating.

My Creation script does not bother with MAPI as we are on a single laptop. When I have tried to set MAPI variables for deletion it complains so I have left this out.

I have reached the stage where I have used the same 2 variables for Creating access to Outlook and Appointments as I have used when I add an appointment (the add works fine). This is the coding I have for the deletion now
Expand|Select|Wrap|Line Numbers
  1.  Dim objOlook As Outlook.Application
  2.                 Dim objOAppt As Outlook.AppointmentItem
  3.                 Dim objOItem As Outlook.AppointmentItem
  4.                 Dim appStart As Variant
  5.                 Dim strFilter As String
  6.  
  7.  
  8.                 Set objOlook = CreateObject("Outlook.Application")
  9.                 Set objOItem = objOlook.CreateItem(olAppointmentItem)
  10.  
  11.  ' Set the Filter String to find Outlook Appointment
  12.  
  13.                 appStart = goldDate & " " & goldStart ' Joins Date & Time together for OLook
  14.  
  15.  
  16.                 strFilter = "[Start]= '" & appStart & "' AND [Location]= '" & goldLocation & "' AND [Subject]= '" & goldName & "'"
  17.                 If IsNull(strFilter) Then
  18.                 MsgBox ("No App")
  19.                 Else
  20.                    If IsNull(objOAppt) Then
  21.                    MsgBox ("No OL Appointement found")
  22.                    Else
  23.                    Set objOAppt = objOItem.Items.Find(strFilter)
  24.                    'objOAppt.Delete
  25.                    MsgBox (objOAppt)
  26.                    End If
  27.                 End If
  28.  
On running the code it complains at the
"Set objOAppt = objOItem.Items. Find(strFilter) " giving an error 438 - Object doesnt support this method or property

I have trapped the variables up to this line and they appear to be OK.

Would you have any ideas on this please. I still think it is my Syntax and, in particular, the use of the .Find statement. Had tried to use your suggestion earlier and it did not like this at all. The Context help does not really help I am afraid.
Nov 1 '11 #10

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

Similar topics

0
9090
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...
3
7511
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...
2
1740
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
2
6352
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...
0
1724
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
6
5904
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?
2
10089
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"...
0
1985
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);
0
1355
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
2
4513
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
0
8462
marktang
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...
0
8382
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,...
0
8658
tracyyun
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...
0
7405
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6209
isladogs
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...
0
5682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1787
bsmnconsultancy
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.