从Excel循环创建时,Outlook约会被覆盖

时间:2017-12-19 18:42:29

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

我遍历Excel工作表中的信息以在Outlook中创建约会。当我将它发送到我的默认文件夹时,它正在工作。

我进行了更改以将数据上传到特定文件夹(由同事共享)。

从那以后,就像我通过我的代码一样,它保存了循环的行的约会。但是,当我转到下一行时,新约会将替换旧约,而不是两者都被保存。

Sub ExportToOutlook        

Dim OL as Outlook.Application, Appoint as Outlook.AppointmentItem, ES as Worksheet, _ 
        r as Long, i as Long, WB as ThisWorkook, oFolder as Object, o NameSpace as Namespace

    Set WB = ThisWorkbook
    Set ES = WB.Sheets("Export Sheet")
    r = ES.Cells(Rows.count,1).End(xlUp).Row
    Set OL = New Outlook.Application
    Set oNameSpace = OL.GetNamespace("MAPI")
    Set oFolder = oNameSpace.GetFolderFromID("Insert the ID").Items.Add(olAppointmentItem)

For i = 2 to r
    With oFolder
        .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

Set OL = Nothing

End Sub

2 个答案:

答案 0 :(得分:1)

您似乎在每行迭代时重新更新相同的文件夹。请尝试以下方法:

Sub ExportToOutlook        

Dim OL as Outlook.Application, Appoint as Outlook.AppointmentItem, ES as Worksheet, _ 
        r as Long, i as Long, WB as ThisWorkook, oFolder as Object, o NameSpace as Namespace

    Set WB = ThisWorkbook
    Set ES = WB.Sheets("Export Sheet")
    r = ES.Cells(Rows.count,1).End(xlUp).Row
    Set OL = New Outlook.Application
    Set oNameSpace = OL.GetNamespace("MAPI")
    Set oFolder = oNameSpace.GetFolderFromID("Insert the ID")

For i = 2 to r
    Dim appt as MailItem
    Set appt = oFolder.Items.Add(olAppointmentItem)
    With appt
        .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

Set OL = Nothing

End Sub

答案 1 :(得分:0)

Dim appt作为Outlook.AppointmentItem是我的修复!

Sub ExportToOutlook2()

    Dim OL As Outlook.Application, ES As Worksheet, _
    r As Long, i As Long, WB As ThisWorkbook, oFolder As Object, oNameSpace As Namespace

    Set WB = ThisWorkbook
    Set ES = WB.Sheets("Export Sheet")
    r = ES.Cells(Rows.count, 1).End(xlUp).Row
    Set OL = New Outlook.Application
    Set oNameSpace = OL.GetNamespace("MAPI")
    Set oFolder = oNameSpace.GetFolderFromID("00000000579E67EAD9C2C94591E62A3CF21135F801001241364BFDA9AF49A3D3384A976997C50036FCD700060000")

    For i = 2 To r
        Dim appt As Outlook.AppointmentItem
        Set appt = oFolder.Items.Add(olAppointmentItem)
        With appt
            .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
            .Save
        End With
    Next i

    Set OL = Nothing

    End Sub