How do I create a Meeting on the non default calendar of the non default email address in outlook using VBA code?
The code that I have creates the invites in the default calendar of the default email address:
Sub CreateAppointmentOutlook()
Dim oApp As Outlook.Application
Dim oApt As Outlook.AppointmentItem
Dim oRecip As Outlook.Recipient
Dim i As Long
Dim lastRow As Long
Dim ws As Worksheet
Dim wb As ThisWorkbook
Set oApp = New Outlook.Application
Set ws = ActiveWorkbook.Worksheets("Sheet1")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For i = 1 To lastRow
Set oApt = oApp.CreateItem(olAppointmentItem)
oApt.MeetingStatus = olMeeting
Debug.Print (ws.Cells(i, 1).Value)
With oApt
.Subject = "Test"
' do some other stuff
End With
Next i
End Sub
The closest I could come to even attempting to change calendar was this reference. To even begin to try to implement this code in my example I did the below as a test
Sub Whatever()
Dim olApp As Object
Set olApp = GetObject(, "Outlook.Application")
Dim ns As Outlook.Namespace
Set ns = olApp.GetNamespace("MAPI")
Dim Items As Object
Set Items = GetFolderPath("otheremail@contoso.com\Calendar").Items
Debug.Print (Items.Parent.FolderPath)
Debug.Print ("End")
End Sub
But I get a Run-time error '91' : Object variable or With block variable not set on line Set Items = GetFolderPath("otheremail@contoso.com\Calendar").Items
UPDATE
This code runs:
Sub Whatever()
Dim olApp As Object
Set olApp = GetObject(, "Outlook.Application")
Dim oApt As Outlook.AppointmentItem
Dim ns As Outlook.Namespace
Dim oFolder As Outlook.Folder
Set ns = olApp.GetNamespace("MAPI")
Set oFolder = ns.Folders("otheremail@contoso.com")
Dim CalItems As Outlook.Items
Set CalItems = oFolder.Items
End Sub
But how then to create a calendar entry on this other CalItems folder collection?
This code will create a Appointment on non default calendar in a non default account in Outlook. Hope this helps someone else out in future: