Solved

delete outlook calendar appointment from access

Posted on 2013-06-27
5
578 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
ID: 39283849
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
ID: 39283908
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
ID: 39283915
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
ID: 39285928
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
ID: 39310006
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
Large Outlook files lead to various unwanted errors and corruption issues. Furthermore, large outlook files can also make Outlook take longer to start-up, search, navigate, and shut-down. So, In this article, i will discuss a method to make your Out…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

770 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