使用 VBA 从 Excel 数据创建 Outlook 约会

Creating Outlook Appointment from Excel Data with VBA

标题说明了一切。我昨天写了代码,效果很好。我是个白痴,保存不正确,丢失了代码。但是,今天我重写了代码以实现它,但我不确定为什么今天没有创建约会。当我通过我的 Sub F8 时,这些值被正确存储。如果有人能指出我忽略的愚蠢错误,那将是我的救命稻草,因为我自己找不到。

Sub test()

    Dim OL As Outlook.Application, Appoint As Outlook.AppointmentItem, ES As Worksheet, _
    r As Long, i As Long, WB As ThisWorkbook

    Set WB = ThisWorkbook
    Set ES = WB.Sheets("Export Sheet")
    r = ES.Cells(Rows.count, 1).End(xlUp).Row
    Set OL = New Outlook.Application

    For i = 2 To r
        Set Appoint = OL.CreateItem(olAppointmentItem)
        With Appoint
            .Subject = ES.Cells(i, 1).Value
            .Start = ES.Cells(i, 2).Value
            .End = ES.Cells(i, 3).Value
            .Location = ES.Cells(i, 4).Value
            .AllDayEvent = ES.Cells(i, 5).Value
            .Categories = ES.Cells(i, 6).Value & " Category"
        End With
    Next i
    Set OL = Nothing

End Sub

有一个工作示例here

您的循环结尾似乎缺少 .Save

像这样:

For i = 2 To r
    Set Appoint = OL.CreateItem(olAppointmentItem)
    With Appoint
        .Subject = ES.Cells(i, 1).Value
        .Start = ES.Cells(i, 2).Value
        .End = ES.Cells(i, 3).Value
        .Location = ES.Cells(i, 4).Value
        .AllDayEvent = ES.Cells(i, 5).Value
        .Categories = ES.Cells(i, 6).Value & " Category"
        .Save
    End With
Next i