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

时间:2017-12-14 13:33:17

标签: vba excel-vba outlook outlook-calendar excel

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

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

1 个答案:

答案 0 :(得分:1)

有一个工作示例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