0
votes

I'm starting in on a project that requires me to add appointments to a shared calendar. I've never used the Outlook object model before, but I've managed to get the procedure down for adding to my default calendar via the following sub:

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

However, if I try another calendar, I suddenly get a Type Mismatch error. I'm sure it's just a gap in my understanding of the Folder object itself.

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("[email protected]")  '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

What about that Set myapt line is incorrect?

P.S. I'm self-taught so feel free to correct me on bad syntax or other bits & bobs.

2
Where is the error? Also perhaps a little background on what you're trying to do would be helpful.ashleedawg

2 Answers

0
votes

I changed

Set myCalendar = ONS.Folders("[email protected]")

to

Set myCalendar = ONS.Folders("[email protected]").Folders("Calendar").

Wish I would have tried that before posting!

0
votes

I am not sure what AppointmentItem is on the following line.

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

Did you mean olAppointmentItem ( = 1)?

Set myapt = myCalendar.Items.Add(olAppointmentItem)