Solved

delete outlook calendar appointment from access

Posted on 2013-06-27
5
585 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

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.
Many people use more than one email account and so it becomes difficult for them to manage them when they use separate accounts,  so, in this article, I have shared an easy way to add Other Mail Accounts in your Google Inbox. It helps to combine all…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…

831 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