none
using VB to create appointment items for one specific username in company network RRS feed

  • Question

  • Hi guys,

    An Annual Leave form has been created upon which I would like the Outlook code to retrieve the specified username and have his/her Outlook account updated. How can I go around gathering username?

    This is the code I have so far:

     Public Sub AddOutlookAppointment()
                Dim username As String = Session("username")
                If username Is Nothing Then
                    username = User.Identity.Name
                    Session("username") = username
                End If
                Dim oOutlook As New Outlook.Application
                Dim oOutlookNamespace As Microsoft.Office.Interop.Outlook.NameSpace = oOutlook.GetNamespace("MAPI")
                Dim oAppItem As Outlook.AppointmentItem = oOutlook.CreateItem(Outlook.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
                oOutlook.Quit()
                Marshal.FinalReleaseComObject(oOutlook)
                oOutlook = Nothing
                'Cleanup
                GC.Collect()
                GC.WaitForPendingFinalizers()
                GC.Collect()
                GC.WaitForPendingFinalizers()
            End Sub

    Wednesday, September 26, 2012 11:20 AM

Answers

  • I'm not quite sure where you get things like oOutlookRecipient.CreateItem() (which doesn't exist)?
     
    You need to look at the Outlook object model in the Object Browser and see what exists and is available to you before you write any code.
     
    CreateItem() is a method of the Outlook.Application object:
     
    Dim oAppItem As Outlook.AppointmentItem = oOutlook.CreateItem(OlItemType.olAppointmentItem)
    Once your appointment item is created you can set the oAppItem.Start and oAppItem.End properties.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "jjb1989" <=?utf-8?B?ampiMTk4OQ==?=> wrote in message news:1d44fc0a-8e91-4220-967f-b90e642fe642...

    Thanks Ken. I added the Recipient object referencing the NameSpace. I'd like to know what I must be missing in the code to get the appointment items set up (first test failed). Lines 5 and 6 are the only tweaks I have made to the code:

    Public Sub AddOutlookAppointment()
                Dim oOutlook As New Outlook.Application
                Dim oOutlookNamespace As Microsoft.Office.Interop.Outlook.NameSpace = oOutlook.GetNamespace("MAPI")
                Dim oOutlookRecipient As Recipient = oOutlookNamespace.CurrentUser
                Dim oAppItem As Outlook.AppointmentItem = oOutlookRecipient.CreateItem(OlItemType.olAppointmentItem)
                Dim startDate As Date = CDate(StartDate1.Text)
                Dim endDate As Date = CDate(EndDate1.Text)
    Thanks again.

    Ken Slovak MVP - Outlook
    Thursday, September 27, 2012 1:43 PM
    Moderator

All replies

  • I don't know what your "Session" is, but after your code gets the NameSpace object use oOutlookNameSpace.CurrentUser to return a Recipient object. You can then get the Name property from the Recipient object or use the AddressEntry property to get the AddressEntry representing that user and read any of the AddressEntry or other Recipient properties.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "jjb1989" <=?utf-8?B?ampiMTk4OQ==?=> wrote in message news:96fd814f-a49f-4132-9bfb-008c71adb5cc...

    Hi guys,

    An Annual Leave form has been created upon which I would like the Outlook code to retrieve the specified username and have his/her Outlook account updated. How can I go around gathering username?

    This is the code I have so far:

     Public Sub AddOutlookAppointment()
                Dim username As String = Session("username")
                If username Is Nothing Then
                    username = User.Identity.Name
                    Session("username") = username
                End If
                Dim oOutlook As New Outlook.Application
                Dim oOutlookNamespace As Microsoft.Office.Interop.Outlook.NameSpace = oOutlook.GetNamespace("MAPI")
                Dim oAppItem As Outlook.AppointmentItem = oOutlook.CreateItem(Outlook.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
                oOutlook.Quit()
                Marshal.FinalReleaseComObject(oOutlook)
                oOutlook = Nothing
                'Cleanup
                GC.Collect()
                GC.WaitForPendingFinalizers()
                GC.Collect()
                GC.WaitForPendingFinalizers()
            End Sub


    Ken Slovak MVP - Outlook
    Wednesday, September 26, 2012 2:42 PM
    Moderator
  • Thanks Ken. I added the Recipient object referencing the NameSpace. I'd like to know what I must be missing in the code to get the appointment items set up (first test failed). Lines 5 and 6 are the only tweaks I have made to the code:

    Public Sub AddOutlookAppointment()
                Dim oOutlook As New Outlook.Application
                Dim oOutlookNamespace As Microsoft.Office.Interop.Outlook.NameSpace = oOutlook.GetNamespace("MAPI")
                Dim oOutlookRecipient As Recipient = oOutlookNamespace.CurrentUser
                Dim oAppItem As Outlook.AppointmentItem = oOutlookRecipient.CreateItem(OlItemType.olAppointmentItem)
                Dim startDate As Date = CDate(StartDate1.Text)
                Dim endDate As Date = CDate(EndDate1.Text)
    Thanks again.
    Thursday, September 27, 2012 9:53 AM
  • I'm not quite sure where you get things like oOutlookRecipient.CreateItem() (which doesn't exist)?
     
    You need to look at the Outlook object model in the Object Browser and see what exists and is available to you before you write any code.
     
    CreateItem() is a method of the Outlook.Application object:
     
    Dim oAppItem As Outlook.AppointmentItem = oOutlook.CreateItem(OlItemType.olAppointmentItem)
    Once your appointment item is created you can set the oAppItem.Start and oAppItem.End properties.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "jjb1989" <=?utf-8?B?ampiMTk4OQ==?=> wrote in message news:1d44fc0a-8e91-4220-967f-b90e642fe642...

    Thanks Ken. I added the Recipient object referencing the NameSpace. I'd like to know what I must be missing in the code to get the appointment items set up (first test failed). Lines 5 and 6 are the only tweaks I have made to the code:

    Public Sub AddOutlookAppointment()
                Dim oOutlook As New Outlook.Application
                Dim oOutlookNamespace As Microsoft.Office.Interop.Outlook.NameSpace = oOutlook.GetNamespace("MAPI")
                Dim oOutlookRecipient As Recipient = oOutlookNamespace.CurrentUser
                Dim oAppItem As Outlook.AppointmentItem = oOutlookRecipient.CreateItem(OlItemType.olAppointmentItem)
                Dim startDate As Date = CDate(StartDate1.Text)
                Dim endDate As Date = CDate(EndDate1.Text)
    Thanks again.

    Ken Slovak MVP - Outlook
    Thursday, September 27, 2012 1:43 PM
    Moderator