如何使用vb在Outlook中创建约会?

时间:2017-04-06 20:17:46

标签: vb.net outlook

我有一个winforms应用程序,我正在尝试创建一个方法来创建一个新的Outlook约会。 我使用以下代码,但在创建对象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

2 个答案:

答案 0 :(得分:1)

需要访问Outlook Application个对象,因为您自己的Application没有合适的CreateItem方法:

这些方面的东西:

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 :(得分:0)

这可能与您最初的想法有点不同,但我认为它会帮助您开始朝着正确的方向发展。

如果要在Outlook中使用Excel创建约会,请运行以下脚本(在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

'代码来自此链接:  http://officetricks.com/add-appointment-to-outlook-calendar-through-excel-macro-vba/

这是一个设置应该是什么样的屏幕。

enter image description here