Solved

delete outlook calendar appointment from access

Posted on 2013-06-27
5
560 Views
Last Modified: 2013-08-21
I have looked at a number of 'solutions' for this on here and other web sites, but none seem to work for me. I have code below to add an appointment from access (2007) to shared calendar. I need to be able to delete appointment from access. Any help would be appreciated.

Set outobj = CreateObject("outlook.application")
Set objApp = CreateObject("Outlook.Application")
Set objNS = objApp.GetNamespace("MAPI")
Set objRecip = objNS.CreateRecipient(strName)

Set objFolder = objNS.GetSharedDefaultFolder(objRecip, olFolderCalendar)

If Not objFolder Is Nothing Then
    Set objAppt = objFolder.Items.Add
    
    If objAppt Is Nothing Then
        Set objAppt = objApp.CreateItem(olAppointmentItem)
    End If
    
    
Else
    MsgBox "no access to the folder meaning it is not shared"
End If

 With objAppt
    .Start = Format(AppStartDate, "Short Date") & " " & Format(Appstarttime, "Short Time")
    .End = Format(AppEndDate, "Short Date") & " " & Format(Appendtime, "Short Time")
    .Location = AppLocation
    .Subject = AppSubject & " - " & AppBusiness
    Body = "Client: " & AppBusiness & vbCrLf & "No of Attendees " & AppNoOfDelegates
    Body = Body & vbCrLf & "Contact Name: " & ContactName
    Body = Body & vbCrLf & "Contact Tele: " & ContactNumber
    Body = Body & vbCrLf & "Contact Email: " & ContactEmail
   .Body = Body
            
    .Save

Open in new window



Above adds the appointment to the shared calendar, but how can I search for and delete appointment (using vba from access)
0
Comment
Question by:foxpc123
  • 3
  • 2
5 Comments
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
Hi, foxpc123.

This should do it.  Pass this routine the name of the recipient and the subject of the appointment.  I'm assuming that the subject is unique (i.e. only one appointment with the subject).  If the subject isn't unique, then we'll need to make some changes to the code.

Sub DeleteAppointment(strName As String, strSubj As String)
    Const olFolderCalendar = 9
    Dim olkApp As Object, olkSes As Object, olkRcp As Object, olkFld As Object, olkApt As Object
    Set olkApp = CreateObject("Outlook.Application")
    Set olkSes = olkApp.GetNamespace("MAPI")
    olkSes.Logon olkApp.DefaultProfileName
    Set olkRcp = olkSes.CreateRecipient(strName)
    Set olkFld = olkSes.GetDefaultFolder(olFolderCalendar).Items
    Set olkApt = olkFld.Find("[Subject] = '" & strSubj & "'")
    If TypeName(olkApt) <> "Nothing" Then
        olkApt.Delete
    End If
    olkSes.Logoff
    Set olkApt = Nothing
    Set olkFld = Nothing
    Set olkRcp = Nothing
    Set olkSes = Nothing
    Set olkApt = Nothing
End Sub

Open in new window

0
 
LVL 3

Author Comment

by:foxpc123
Comment Utility
Thanks BlueDevilFan. Will give it a try. However the Subject wasn't unique as it was a title of training course. However I can add a unique ID number to identify it.
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
You're welcome.

If the subject isn't unique, then the best we could do is delete all appointments with that subject or delete those within a certain date range.
0
 
LVL 3

Author Comment

by:foxpc123
Comment Utility
BlueDevilFan,

Tried your suggestion, didn't seem to work. OlkApp was always set to nothing, even though strname was set to shared calendar name and strSubj was set to subject on appointment.

Is your solution for a shared calendar ?

Not sure about:
Const olFolderCalendar = 9

Open in new window

and
 Set olkFld = olkSes.GetDefaultFolder(olFolderCalendar).Items

Open in new window


Your code deletes the appointment from the default calendar and not the shared calendar (name stored in strName)
0
 
LVL 76

Accepted Solution

by:
David Lee earned 500 total points
Comment Utility
foxpc123,

Sorry to be so slow to get back to you.  

I don't know why olkApp would be set to Nothing.  What version of Outlook are you using?

You're right, I was getting the local calendar instead of a shared calendar.  I've corrected the code.  Please give this version a try.

Sub DeleteAppointment(strName As String, strSubj As String)
    Const olFolderCalendar = 9
    Dim olkApp As Object, olkSes As Object, olkRcp As Object, olkFld As Object, olkApt As Object
    Set olkApp = CreateObject("Outlook.Application")
    Set olkSes = olkApp.GetNamespace("MAPI")
    olkSes.Logon olkApp.DefaultProfileName
    Set olkRcp = olkSes.CreateRecipient(strName)
    Set olkFld = olkSes.GetSharedDefaultFolder(olkRcp, olFolderCalendar).Items
    Set olkApt = olkFld.Find("[Subject] = '" & strSubj & "'")
    If TypeName(olkApt) <> "Nothing" Then
        olkApt.Delete
    End If
    olkSes.Logoff
    Set olkApt = Nothing
    Set olkFld = Nothing
    Set olkRcp = Nothing
    Set olkSes = Nothing
    Set olkApt = Nothing
End Sub

Open in new window

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Following basic email etiquette rules will help you write a professional email and achieve a good, lasting impression with your contacts.
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now