none
VB.NET Outlook Calendar Automation RRS feed

  • Question

  • Hi everyone.

    The intention for the code below is for an Outlook user's calendar to be updated with specified date appointment items upon manager approval. This is done via Exhange Server and so the proper user address must be acquired.

    Public Sub AddOutlookAppointment(ByVal employee_ID As Int64)
                Dim beEmployee As TecBE_Employee.clsTecBE_Employee
                beEmployee = employeeManager.fncEmployee(employee_ID)
                Dim oOutlook As New Outlook.Application
                Dim oOutlookNamespace As Microsoft.Office.Interop.Outlook.NameSpace = oOutlook.GetNamespace("MAPI")
                Dim oOutlookRecipient As Recipient = beEmployee
                Dim oAppItem As Outlook.AppointmentItem = oOutlook.CreateItem(OlItemType.olAppointmentItem)
                Dim startDate As Date = CDate(StartDate1.Text)
                Dim endDate As Date = CDate(EndDate1.Text)
                oOutlookRecipient.oAppItem.Subject = "Annual Leave"
                oOutlookRecipient.oAppItem.Start = startDate
                oOutlookRecipient.oAppItem.End = endDate
                oOutlookRecipient.oAppItem.ReminderSet = True
                oOutlookRecipient.oAppItem.BusyStatus = Outlook.OlBusyStatus.olOutOfOffice
                Marshal.FinalReleaseComObject(oOutlookNamespace)
                oOutlookNamespace = Nothing
                oAppItem.Save()
                Marshal.FinalReleaseComObject(oAppItem)
                oAppItem = Nothing
                Marshal.FinalReleaseComObject(oOutlookRecipient)
                oOutlookRecipient = Nothing
                oOutlook.Quit()
                Marshal.FinalReleaseComObject(oOutlook)
                oOutlook = Nothing
                'Cleanup
                GC.Collect()
                GC.WaitForPendingFinalizers()
                GC.Collect()
                GC.WaitForPendingFinalizers()
            End Sub

    My challenge is line 6 and whether the oOutlookRecipient is retrieving the correct mail address. This code will only be executed upon manager clicking an approval button.

    Please let me know if you require further clarification.

    Thanks for your help.

    Wednesday, October 10, 2012 7:47 AM

Answers

  • That initial declaration is doing nothing for you. The recipient object you create in that line is not used at all and is replaced by the recipient added to the appointment.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "jjb1989" <=?utf-8?B?ampiMTk4OQ==?=> wrote in message news:83445720-42c4-46f3-a902-d8d7b728e155...

    Thanks Ken.

    Please have yet another look at the tweaked code:

     Public Sub AddOutlookAppointment(ByVal employee_ID As Int64)
                Dim beEmployee As TecBE_Employee.clsTecBE_Employee
                beEmployee = employeeManager.fncEmployee(employee_ID)
                Dim oOutlook As New Outlook.Application
                Dim oOutlookNamespace As Microsoft.Office.Interop.Outlook.NameSpace = oOutlook.GetNamespace("MAPI")
                Dim oAppItem As Outlook.AppointmentItem = oOutlook.CreateItem(OlItemType.olAppointmentItem)
                Dim oOutlookRecipient As Outlook.Recipient = oOutlookNamespace.CreateRecipient(beEmployee.Email)
                Dim startDate As Date = CDate(StartDate1.Text)
                Dim endDate As Date = CDate(EndDate1.Text)
                oAppItem.Subject = "Annual Leave"
                oAppItem.Start = startDate
                oAppItem.End = endDate
                oAppItem.ReminderSet = True
                oAppItem.BusyStatus = Outlook.OlBusyStatus.olOutOfOffice
                Dim recips As Outlook.Recipients = oAppItem.Recipients
                oOutlookRecipient = recips.Add(beEmployee.Email)
                Marshal.FinalReleaseComObject(oOutlookNamespace)
                oOutlookNamespace = Nothing
                oAppItem.Save()
                Marshal.FinalReleaseComObject(oAppItem)
                oAppItem = Nothing
                Marshal.FinalReleaseComObject(oOutlookRecipient)
                oOutlookRecipient = Nothing
                oOutlook.Quit()
                Marshal.FinalReleaseComObject(oOutlook)
                oOutlook = Nothing
                'Cleanup
                GC.Collect()
                GC.WaitForPendingFinalizers()
                GC.Collect()
                GC.WaitForPendingFinalizers()
            End Sub
        End Class
    The concern I have is if I'm overriding the oOutlookRecipient object without reason to do so. Should I remove the initial oOutlookRecipient declaration?

    Ken Slovak MVP - Outlook
    Thursday, October 11, 2012 3:21 PM
    Moderator

All replies

  • You cannot define or set an Outlook Recipient as a 64-bit integer. That obviously would never work at all if you look at the Outlook object model.
     
    You can create a Recipient object using the NameSpace.CreateRecipient() method, or more usefully you can get the Recipients colleciton of an item such as an email and add a new Recipient to that collection using the Recipients.Add() method.
     
    In either case you need to supply an email address or name to create a Recipient, not a number.
     
    If you were to look at the Outlook object model you would also see that something like this line would never work either:
     
    oOutlookRecipient.oAppItem.Subject
     
    A Recipient object does not have an Appointment property, or a MailItem property, or any other such property. You need to look at the object model and only use what exists in it.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "jjb1989" <=?utf-8?B?ampiMTk4OQ==?=> wrote in message news:88c2c652-bc3b-49e8-95b8-5433c6338fc4...

    Hi everyone.

    The intention for the code below is for an Outlook user's calendar to be updated with specified date appointment items upon manager approval. This is done via Exhange Server and so the proper user address must be acquired.

    Public Sub AddOutlookAppointment(ByVal employee_ID As Int64)
                Dim beEmployee As TecBE_Employee.clsTecBE_Employee
                beEmployee = employeeManager.fncEmployee(employee_ID)
                Dim oOutlook As New Outlook.Application
                Dim oOutlookNamespace As Microsoft.Office.Interop.Outlook.NameSpace = oOutlook.GetNamespace("MAPI")
                Dim oOutlookRecipient As Recipient = beEmployee
                Dim oAppItem As Outlook.AppointmentItem = oOutlook.CreateItem(OlItemType.olAppointmentItem)
                Dim startDate As Date = CDate(StartDate1.Text)
                Dim endDate As Date = CDate(EndDate1.Text)
                oOutlookRecipient.oAppItem.Subject = "Annual Leave"
                oOutlookRecipient.oAppItem.Start = startDate
                oOutlookRecipient.oAppItem.End = endDate
                oOutlookRecipient.oAppItem.ReminderSet = True
                oOutlookRecipient.oAppItem.BusyStatus = Outlook.OlBusyStatus.olOutOfOffice
                Marshal.FinalReleaseComObject(oOutlookNamespace)
                oOutlookNamespace = Nothing
                oAppItem.Save()
                Marshal.FinalReleaseComObject(oAppItem)
                oAppItem = Nothing
                Marshal.FinalReleaseComObject(oOutlookRecipient)
                oOutlookRecipient = Nothing
                oOutlook.Quit()
                Marshal.FinalReleaseComObject(oOutlook)
                oOutlook = Nothing
                'Cleanup
                GC.Collect()
                GC.WaitForPendingFinalizers()
                GC.Collect()
                GC.WaitForPendingFinalizers()
            End Sub

    My challenge is line 6 and whether the oOutlookRecipient is retrieving the correct mail address. This code will only be executed upon manager clicking an approval button.

    Please let me know if you require further clarification.

    Thanks for your help.


    Ken Slovak MVP - Outlook
    Wednesday, October 10, 2012 2:53 PM
    Moderator
  • Thanks again Ken. The following is the tweaked code according to your advice.

     Public Sub AddOutlookAppointment(ByVal employee_ID As Int64)
                Dim beEmployee As TecBE_Employee.clsTecBE_Employee
                beEmployee = employeeManager.fncEmployee(employee_ID)
                Dim oOutlook As New Outlook.Application
                Dim oOutlookNamespace As Microsoft.Office.Interop.Outlook.NameSpace = oOutlook.GetNamespace("MAPI")
                Dim oOutlookRecipient As Recipient = oOutlookNamespace.CreateRecipient(beEmployee.Email)
                Dim oAppItem As Outlook.AppointmentItem = oOutlook.CreateItem(OlItemType.olAppointmentItem)
                Dim startDate As Date = CDate(StartDate1.Text)
                Dim endDate As Date = CDate(EndDate1.Text)
                oAppItem.Subject = "Annual Leave"
                oAppItem.Start = startDate
                oAppItem.End = endDate
                oAppItem.ReminderSet = True
                oAppItem.BusyStatus = Outlook.OlBusyStatus.olOutOfOffice
                Marshal.FinalReleaseComObject(oOutlookNamespace)
                oOutlookNamespace = Nothing
                oAppItem.Save()
                Marshal.FinalReleaseComObject(oAppItem)
                oAppItem = Nothing
                Marshal.FinalReleaseComObject(oOutlookRecipient)
                oOutlookRecipient = Nothing
                oOutlook.Quit()
                Marshal.FinalReleaseComObject(oOutlook)
                oOutlook = Nothing
                'Cleanup
                GC.Collect()
                GC.WaitForPendingFinalizers()
                GC.Collect()
                GC.WaitForPendingFinalizers()
            End Sub

    Concerning the lines you highlighted concerning the recipient object and the Appointment items: Does the above code already carry out the Outlook automation to that specified recipient by default, or are further lines of code required to bind both entities?

    Thank you once again.

    Thursday, October 11, 2012 6:55 AM
  • I don't understand what you are intending to do with that Recipient object you're creating. You're creating it and then releasing it without doing anything with it.
     
    If you actually want to do something with the Recipient, like adding it to the appointment object you need to follow the original advice I gave in my first post:
     
    You can create a Recipient object using the NameSpace.CreateRecipient() method, or more usefully you can get the Recipients collection of an item such as an email and add a new Recipient to that collection using the Recipients.Add() method.
     
    In this case, if you want to add a recipient to the appointment and then send it to the recipient, for example, you would do something like this:
     
    Dim recips As Outlook.Recipients = oAppItem.Recipients
    Dim oOutlookRecipient As Outlook.Recipient = recips.Add(beEmployee.Email)
     
    That adds the recipient to the appointment. You can then save or send the appointment or do whatever else you want with it.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "jjb1989" <=?utf-8?B?ampiMTk4OQ==?=> wrote in message news:b5fef0ea-c743-4085-b210-749a70dcc235...

    Thanks again Ken. The following is the tweaked code according to your advice.

     Public Sub AddOutlookAppointment(ByVal employee_ID As Int64)
                Dim beEmployee As TecBE_Employee.clsTecBE_Employee
                beEmployee = employeeManager.fncEmployee(employee_ID)
                Dim oOutlook As New Outlook.Application
                Dim oOutlookNamespace As Microsoft.Office.Interop.Outlook.NameSpace = oOutlook.GetNamespace("MAPI")
                Dim oOutlookRecipient As Recipient = oOutlookNamespace.CreateRecipient(beEmployee.Email)
                Dim oAppItem As Outlook.AppointmentItem = oOutlook.CreateItem(OlItemType.olAppointmentItem)
                Dim startDate As Date = CDate(StartDate1.Text)
                Dim endDate As Date = CDate(EndDate1.Text)
                oAppItem.Subject = "Annual Leave"
                oAppItem.Start = startDate
                oAppItem.End = endDate
                oAppItem.ReminderSet = True
                oAppItem.BusyStatus = Outlook.OlBusyStatus.olOutOfOffice
                Marshal.FinalReleaseComObject(oOutlookNamespace)
                oOutlookNamespace = Nothing
                oAppItem.Save()
                Marshal.FinalReleaseComObject(oAppItem)
                oAppItem = Nothing
                Marshal.FinalReleaseComObject(oOutlookRecipient)
                oOutlookRecipient = Nothing
                oOutlook.Quit()
                Marshal.FinalReleaseComObject(oOutlook)
                oOutlook = Nothing
                'Cleanup
                GC.Collect()
                GC.WaitForPendingFinalizers()
                GC.Collect()
                GC.WaitForPendingFinalizers()
            End Sub

    Concerning the lines you highlighted concerning the recipient object and the Appointment items: Does the above code already carry out the Outlook automation to that specified recipient by default, or are further lines of code required to bind both entities?

    Thank you once again.


    Ken Slovak MVP - Outlook
    Thursday, October 11, 2012 2:22 PM
    Moderator
  • Thanks Ken.

    Please have yet another look at the tweaked code:

     Public Sub AddOutlookAppointment(ByVal employee_ID As Int64)
                Dim beEmployee As TecBE_Employee.clsTecBE_Employee
                beEmployee = employeeManager.fncEmployee(employee_ID)
                Dim oOutlook As New Outlook.Application
                Dim oOutlookNamespace As Microsoft.Office.Interop.Outlook.NameSpace = oOutlook.GetNamespace("MAPI")
                Dim oAppItem As Outlook.AppointmentItem = oOutlook.CreateItem(OlItemType.olAppointmentItem)
                Dim oOutlookRecipient As Outlook.Recipient = oOutlookNamespace.CreateRecipient(beEmployee.Email)
                Dim startDate As Date = CDate(StartDate1.Text)
                Dim endDate As Date = CDate(EndDate1.Text)
                oAppItem.Subject = "Annual Leave"
                oAppItem.Start = startDate
                oAppItem.End = endDate
                oAppItem.ReminderSet = True
                oAppItem.BusyStatus = Outlook.OlBusyStatus.olOutOfOffice
                Dim recips As Outlook.Recipients = oAppItem.Recipients
                oOutlookRecipient = recips.Add(beEmployee.Email)
                Marshal.FinalReleaseComObject(oOutlookNamespace)
                oOutlookNamespace = Nothing
                oAppItem.Save()
                Marshal.FinalReleaseComObject(oAppItem)
                oAppItem = Nothing
                Marshal.FinalReleaseComObject(oOutlookRecipient)
                oOutlookRecipient = Nothing
                oOutlook.Quit()
                Marshal.FinalReleaseComObject(oOutlook)
                oOutlook = Nothing
                'Cleanup
                GC.Collect()
                GC.WaitForPendingFinalizers()
                GC.Collect()
                GC.WaitForPendingFinalizers()
            End Sub
        End Class
    The concern I have is if I'm overriding the oOutlookRecipient object without reason to do so. Should I remove the initial oOutlookRecipient declaration?
    Thursday, October 11, 2012 3:05 PM
  • That initial declaration is doing nothing for you. The recipient object you create in that line is not used at all and is replaced by the recipient added to the appointment.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "jjb1989" <=?utf-8?B?ampiMTk4OQ==?=> wrote in message news:83445720-42c4-46f3-a902-d8d7b728e155...

    Thanks Ken.

    Please have yet another look at the tweaked code:

     Public Sub AddOutlookAppointment(ByVal employee_ID As Int64)
                Dim beEmployee As TecBE_Employee.clsTecBE_Employee
                beEmployee = employeeManager.fncEmployee(employee_ID)
                Dim oOutlook As New Outlook.Application
                Dim oOutlookNamespace As Microsoft.Office.Interop.Outlook.NameSpace = oOutlook.GetNamespace("MAPI")
                Dim oAppItem As Outlook.AppointmentItem = oOutlook.CreateItem(OlItemType.olAppointmentItem)
                Dim oOutlookRecipient As Outlook.Recipient = oOutlookNamespace.CreateRecipient(beEmployee.Email)
                Dim startDate As Date = CDate(StartDate1.Text)
                Dim endDate As Date = CDate(EndDate1.Text)
                oAppItem.Subject = "Annual Leave"
                oAppItem.Start = startDate
                oAppItem.End = endDate
                oAppItem.ReminderSet = True
                oAppItem.BusyStatus = Outlook.OlBusyStatus.olOutOfOffice
                Dim recips As Outlook.Recipients = oAppItem.Recipients
                oOutlookRecipient = recips.Add(beEmployee.Email)
                Marshal.FinalReleaseComObject(oOutlookNamespace)
                oOutlookNamespace = Nothing
                oAppItem.Save()
                Marshal.FinalReleaseComObject(oAppItem)
                oAppItem = Nothing
                Marshal.FinalReleaseComObject(oOutlookRecipient)
                oOutlookRecipient = Nothing
                oOutlook.Quit()
                Marshal.FinalReleaseComObject(oOutlook)
                oOutlook = Nothing
                'Cleanup
                GC.Collect()
                GC.WaitForPendingFinalizers()
                GC.Collect()
                GC.WaitForPendingFinalizers()
            End Sub
        End Class
    The concern I have is if I'm overriding the oOutlookRecipient object without reason to do so. Should I remove the initial oOutlookRecipient declaration?

    Ken Slovak MVP - Outlook
    Thursday, October 11, 2012 3:21 PM
    Moderator