从Excel导出日期到Outlook日历出错



我正试图编写一些代码将日期从excel导出到outlook日历,但我在

一行出现错误
'Get the calendar by name
Set OutlookCalendar = OutlookApp.Session.Folders("Calendar Name").Folders(CalendarName)

我在这里做错了什么?整个代码粘贴在下面。

Sub ExportToOutlook()
Dim OutlookApp As Outlook.Application
Dim OutlookCalendar As Outlook.Folder
Dim OutlookEvent As Outlook.AppointmentItem
Dim StartDate As Date
Dim EndDate As Date
Dim EventTitle As String
Dim EventDescription As String
Dim EventLocation As String
Dim CalendarName As String
Dim tbl As ListObject
Dim LastRow As Long
Dim i As Long
Dim CalendarNameCell As String
Dim EventTitleCell As String
Dim EventDescriptionCell As String
Dim EventLocationCell As String

'Name of the table
Set tbl = ThisWorkbook.Sheets("Pris nightliner").ListObjects("Runde1")
LastRow = tbl.Range.Rows.Count
'Cell that contains the calendar name
CalendarNameCell = "H13"
'Cell that contains the event title
EventTitleCell = "H9"
'Cell that contains the event description
EventDescriptionCell = "H12"
'Cell that contains the event location
EventLocationCell = "F2"
'Create an instance of Outlook
Set OutlookApp = CreateObject("Outlook.Application")
'Get the calendar name
CalendarName = ThisWorkbook.Sheets("Pris nightliner").Range(CalendarNameCell).Value
'Get the calendar by name
Set OutlookCalendar = OutlookApp.Session.Folders("Calendar Name").Folders(CalendarName)
'Get the first and last date
StartDate = tbl.ListColumns("Start Date").DataBodyRange(2).Value
EndDate = tbl.ListColumns("Start Date").DataBodyRange(LastRow).Value
'Get the event title, location, and description
EventTitle = ThisWorkbook.Sheets("Pris nightliner").Range(EventTitleCell).Value
EventDescription = ThisWorkbook.Sheets("Pris nightliner").Range(EventDescriptionCell).Value
EventLocation = ThisWorkbook.Sheets("Pris nightliner").Range(EventLocationCell).Value
'Create a new event in Outlook
Set OutlookEvent = OutlookApp.CreateItem(olAppointmentItem)
With OutlookEvent
.Start = StartDate
.End = EndDate
.Subject = EventTitle
.Location = EventLocation
.Body = EventDescription
.Save
.Move OutlookCalendar
End With
'Release the Outlook objects
Set OutlookEvent = Nothing
Set OutlookCalendar = Nothing
Set OutlookApp = Nothing
End Sub

我正在尝试从excel导出到outlook。

Namespace.Folders集合包含邮箱的顶级文件夹,例如"user@domain.demo"

如果您想使用默认的Calendar文件夹,请使用OutlookApp.Session.GetDefaultFolder(olFolderCalendar)

最新更新