添加约会到日历时键入不匹配

时间:2018-04-02 19:41:39

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

我正在开始一个需要我将约会添加到共享日历的项目。我以前从未使用过Outlook对象模型,但我已设法通过以下子目录添加到我的默认日历:

Option Explicit

Sub caltest()

Dim O As Outlook.Application
Set O = New Outlook.Application

Dim ONS As Outlook.Namespace
Set ONS = O.GetNamespace("MAPI")

Dim myCalendar As Outlook.Folder
Set myCalendar = ONS.GetDefaultFolder(olFolderCalendar)

Dim myapt As Outlook.AppointmentItem
Set myapt = myCalendar.Items.Add([AppointmentItem])

With myapt
    .Start = DateValue("4/2/2018") + TimeValue("11:30:00")
    .End = DateValue("4/2/2018") + TimeValue("12:00:00")
    .Location = "nowhere"
    .Subject = "test"
    .Body = "something important"
    .Save
End With

End Sub

但是,如果我尝试另一个日历,我会突然遇到类型不匹配错误。我确定这只是我对Folder对象本身的理解上的差距。

Option Explicit

Sub caltest()

Dim O As Outlook.Application
Set O = New Outlook.Application

Dim ONS As Outlook.Namespace
Set ONS = O.GetNamespace("MAPI")

Dim myCalendar As Outlook.Folder
Set myCalendar = ONS.Folders("videoconferencing@example.org")  'changed this line

Dim myapt As Outlook.AppointmentItem
Set myapt = myCalendar.Items.Add([AppointmentItem])  'line throws type mismatch

With myapt
    .Start = DateValue("4/2/2018") + TimeValue("11:30:00")
    .End = DateValue("4/2/2018") + TimeValue("12:00:00")
    .Location = "nowhere"
    .Subject = "test"
    .Body = "something important"
    .Save
End With

End Sub

那条Set myapt行不正确?

P.S。我是自学成才的,所以请随意用错误的语法或其他方法来纠正我。鲍勃。

2 个答案:

答案 0 :(得分:0)

我改变了

Set myCalendar = ONS.Folders("videoconferencing@example.org")

Set myCalendar = ONS.Folders("videoconferencing@example.org").Folders("Calendar")

希望在张贴之前我会尝试过!

答案 1 :(得分:0)

我不确定AppointmentItem在以下行中是什么。

Dim Record as Variant, cell As Range
Record = Sheets("Sheet 2").Range("D29") 'Dropdown cell that can be a data creator or an asterisk

For Each cell In Sheets("Sheet 1").Range("Z3:Z100")
    If cell = Record Then
        'Execute Code
    Else
    End If
Next cell

您的意思是olAppointmentItem(= 1)吗?

Set myapt = myCalendar.Items.Add([AppointmentItem])