How to create an appointment in Outlook using vb?

I have a winforms application and I am trying to create a method that will create a new Outlook appointment. I am using the following code, but I will mark an error in the creation of the object newAppointment

.

Private Sub AddAppointment()
    Dim newAppointment As Outlook.AppointmentItem = Me.Application.CreateItem _
         (Outlook.OlItemType.olAppointmentItem)
    Try
        With newAppointment
            .Start = Date.Now.AddHours(2)
            .End = Date.Now.AddHours(3)
            .Location = "ConferenceRoom #2345"
            .Body = _
                "We will discuss progress on the group project."
            .Subject = "Group Project"
            .AllDayEvent = False
            .Recipients.Add("Roger Harui")
            Dim sentTo As Outlook.Recipients = .Recipients
            Dim sentInvite As Outlook.Recipient
            sentInvite = sentTo.Add("Holly Holt")
            sentInvite.Type = Outlook.OlMeetingRecipientType.olRequired
            sentInvite = sentTo.Add("David Junca")
            sentInvite.Type = Outlook.OlMeetingRecipientType.olOptional
            sentTo.ResolveAll()
            .Save()
            .Display(True)
        End With
    Catch ex As Exception
        MessageBox.Show("The following error occurred: " & _
            ex.Message)
    End Tryenter code here
End Sub

      

+3


source to share


2 answers


Object access is required Outlook Application

because your own Application

does not have a suitable method CreateItem

:

Something like this:



Imports Microsoft.Office.Interop

....

Private Sub AddAppointment()
    '--- Check if Outlook is already up and running
    If Process.GetProcessesByName("outlook").Count > 0 Then
        '--- all ready ---
    Else
        '--- start Outlook ---
        Process.Start("outlook")
        '--- more elaborate wait might be a good idea here ---
        System.Threading.Thread.Sleep(500)
    End If

    '--- initialize required Application object ---
    '--- assuming it is not available as variable already ---
    Dim objOutlook As Outlook.Application
    objOutlook = CreateObject("Outlook.Application")

    Dim newAppointment As Outlook.AppointmentItem =
        objOutlook.CreateItem(Outlook.OlItemType.olAppointmentItem)
    Try
        With newAppointment
            .Start = Date.Now.AddHours(2)
            .End = Date.Now.AddHours(3)
            .Location = "ConferenceRoom #2345"
            .Body = "We will discuss progress on the group project."
            .Subject = "Group Project"
            .AllDayEvent = False
            .Recipients.Add("Roger Harui")
            Dim sentTo As Outlook.Recipients = .Recipients
            Dim sentInvite As Outlook.Recipient
            sentInvite = sentTo.Add("Holly Holt")
            sentInvite.Type = Outlook.OlMeetingRecipientType.olRequired
            sentInvite = sentTo.Add("David Junca")
            sentInvite.Type = Outlook.OlMeetingRecipientType.olOptional
            sentTo.ResolveAll()
            .Save()
            .Display(True)
        End With
    Catch ex As Exception
        MessageBox.Show("The following error occurred: " & ex.Message)
    End Try
End Sub

      

+1


source


It may be a little off what you originally thought, but I think it will help you get started in the right direction.

If you want to create an appointment in Outlook using Excel then run the script below (in Excel).

Private Sub Add_Appointments_To_Outlook_Calendar()

    'Include Microsoft Outlook nn.nn Object Library from Tools -> References
    Dim oAppt As AppointmentItem
    Dim Remind_Time As Double

    i = 2
    Subj = ThisWorkbook.Sheets(1).Cells(i, 1)

    'Loop through entire list of Reminders to be added
    While Subj <> ""
        Set oAppt = Outlook.Application.CreateItem(olAppointmentItem)

        oAppt.Subject = Subj
        oAppt.Location = ThisWorkbook.Sheets(1).Cells(i, 2)
        oAppt.Start = ThisWorkbook.Sheets(1).Cells(i, 3)
        Remind_Time = ThisWorkbook.Sheets(1).Cells(i, 4) * 1 * 60
        oAppt.ReminderMinutesBeforeStart = Remind_Time
        oAppt.AllDayEvent = True
        oAppt.Save

        i = i + 1
        Subj = ThisWorkbook.Sheets(1).Cells(i, 1)
    Wend
    MsgBox "Reminder(s) Added To Outlook Calendar"

End Sub

      



'The code comes from this link:   http://officetricks.com/add-appointment-to-outlook-calendar-through-excel-macro-vba/

Here is a screen of what the installation should look like.

enter image description here

0


source







All Articles