Visual Basic > Visual Basic Forums > Visual Basic Language > Error when creating an appointment in outlook with vba
Ask a questionAsk a question
 

AnswerError when creating an appointment in outlook with vba

  • Friday, November 06, 2009 10:04 PMTrullion Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Working in Access 2003 with VBA, I am trying to create an appointment, turn it into a meeting and then send out  invites to people.  Below is the code I am using.

    Public Function CreateAppointment(myRecipient as String, mySubject As String, myLocation As String, myStartTime As Date, numMinutesLong As Integer) As Boolean
        Dim olApp As Outlook.Application
        Dim olObj As Outlook.AppointmentItem
       
        Set olApp = CreateObject("Outlook.Application")
        Set olObj = olApp.CreateItem(olAppointmentItem)

        With olObj
            .Recipients.Add myRecipient
            .Subject = mySubject
            .Location = myLocation
            .Start = myStartTime
            .Duration = numMinutesLong
            .MeetingStatus = olMeeting
            .Display
        End With
        Set olApp = Nothing
        Set olObj = Nothing
    End Function

    When I try to run it, the appointment is displayed with everything filled out but when I send it I get the message "Operation failed".  Anyone have any ideas?


    Do or do not. There is no try...

Answers

  • Friday, November 06, 2009 10:30 PMTrullion Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Haha...after spending two days working on the problem, I finally break down and ask for help on the forums...less than 30 mins later I find the solution....code posted below. Note the line with the .PickFolder in it. The code will probably throw an error if the user picks something besides a calendar.

    The difference here is that first code tried to create an appointment item whilst this one allows the user to select a calendar, and then adds an item.

    Public Function CreateAppointment(mySubject As String, myLocation As String, myStartTime As Date, numMinutesLong As Integer) As Boolean
        Dim olApp As Outlook.Application
        Dim olObj As Outlook.AppointmentItem
       
        Set olApp = CreateObject("Outlook.Application")
        
        Dim olNameSpace As Outlook.NameSpace
        Dim myFolder As Outlook.MAPIFolder
        Set olNameSpace = olApp.GetNamespace("MAPI")
        Set myFolder = olNameSpace.PickFolder
       
        Set olObj = myFolder.Items.Add
       
        With olObj
            .Recipients.Add "Scott Espen"
            .Subject = mySubject
            .Location = myLocation
            .Start = myStartTime
            .Duration = numMinutesLong
            .MeetingStatus = olMeeting
            .Display
        End With
        Set olApp = Nothing
        Set olObj = Nothing
        Set olNameSpace = Nothing
        Set myFolder = Nothing
    End Function


    Do or do not. There is no try...
    • Marked As Answer byTrullion Friday, November 06, 2009 10:33 PM
    •  

All Replies

  • Friday, November 06, 2009 10:30 PMTrullion Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Haha...after spending two days working on the problem, I finally break down and ask for help on the forums...less than 30 mins later I find the solution....code posted below. Note the line with the .PickFolder in it. The code will probably throw an error if the user picks something besides a calendar.

    The difference here is that first code tried to create an appointment item whilst this one allows the user to select a calendar, and then adds an item.

    Public Function CreateAppointment(mySubject As String, myLocation As String, myStartTime As Date, numMinutesLong As Integer) As Boolean
        Dim olApp As Outlook.Application
        Dim olObj As Outlook.AppointmentItem
       
        Set olApp = CreateObject("Outlook.Application")
        
        Dim olNameSpace As Outlook.NameSpace
        Dim myFolder As Outlook.MAPIFolder
        Set olNameSpace = olApp.GetNamespace("MAPI")
        Set myFolder = olNameSpace.PickFolder
       
        Set olObj = myFolder.Items.Add
       
        With olObj
            .Recipients.Add "Scott Espen"
            .Subject = mySubject
            .Location = myLocation
            .Start = myStartTime
            .Duration = numMinutesLong
            .MeetingStatus = olMeeting
            .Display
        End With
        Set olApp = Nothing
        Set olObj = Nothing
        Set olNameSpace = Nothing
        Set myFolder = Nothing
    End Function


    Do or do not. There is no try...
    • Marked As Answer byTrullion Friday, November 06, 2009 10:33 PM
    •  
  • Saturday, November 07, 2009 12:15 AMbdbodger Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    That is no help here this is not the VBA forum this is the VB.net forum .
    coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read .