简体   繁体   English

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

[英]Outlook appointments being overwritten when created from Excel loop

I loop through information in an Excel sheet to create appointments in Outlook. 我遍历Excel工作表中的信息以在Outlook中创建约会。 It was working when I sent it to my default folder. 当我将其发送到默认文件夹时,它正在工作。

I made changes to upload the data to a specific folder (shared by coworkers). 我进行了更改,以将数据上传到特定文件夹(由同事共享)。

Since then, as I F8 through my code, it saves the appointment for the row being looped through. 从那时起,当我在代码中按F8键时,它保存了要循环通过的行的约会。 However, when I go to the next row, the new appointment replaces the old instead of both being saved. 但是,当我转到下一行时,新约会将替换旧约会,而不是同时保存两者。

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

You seem to be reupdating the same folder at each row iteration. 您似乎在每次行迭代时都重新更新同一文件夹。 Try the following: 请尝试以下操作:

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

Dim appt as Outlook.AppointmentItem was the fix for me! 昏暗的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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM