【发布时间】:2019-12-16 01:25:44
【问题描述】:
我正在尝试列出从 gmail 订阅的 Internet 日历的事件。
代码仅列出在日历文件夹中的 Outlook 应用中创建的事件。
这是我在 Stack Overflow 上找到的代码:
Option Explicit
Sub ListAppointments()
Dim olApp As Object
Dim olNS As Object
Dim olFolder As Object
Dim olApt As Object
Dim NextRow As Long
Dim FromDate As Date
Dim ToDate As Date
FromDate = CDate("08/25/2018")
ToDate = CDate("12/31/2019")
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
If Err.Number > 0 Then Set olApp = CreateObject("Outlook.Application")
On Error GoTo 0
Set olNS = olApp.GetNamespace("MAPI")
Set olFolder = olNS.GetDefaultFolder(9) 'olFolderCalendar 9
NextRow = 2
With Sheets("Sheet1") 'Change the name of the sheet here
.Range("A1:D1").Value = Array("Project", "Date", "Time spent", "Location")
For Each olApt In olFolder.Items
If (olApt.Start >= FromDate And olApt.Start <= ToDate) Then
.Cells(NextRow, "A").Value = olApt.Subject
.Cells(NextRow, "B").Value = CDate(olApt.Start)
.Cells(NextRow, "C").Value = olApt.End - olApt.Start
.Cells(NextRow, "C").NumberFormat = "HH:MM:SS"
.Cells(NextRow, "D").Value = olApt.Location
.Cells(NextRow, "E").Value = olApt.Categories
NextRow = NextRow + 1
Else
End If
Next olApt
.Columns.AutoFit
End With
Set olApt = Nothing
Set olFolder = Nothing
Set olNS = Nothing
Set olApp = Nothing
End Sub
我几乎肯定问题出在这里:
Set olFolder = olNS.GetDefaultFolder(9) 'olFolderCalendar 9
我要访问的文件夹的名称是基本的。这两个我都试过了:
Set olFolder = olNS.GetDefaultFolder(9).Folders("basic").Items
Set olFolder = olNS.GetDefaultFolder(9).Parent.Folders("basic").Items
都没有用。
运行时错误“-2147221233 (8004010f)”:
尝试的操作失败。找不到对象。
编辑:
我最终发现:olNS.GetDefaultFolder(9).Parent = email@gmail.com 和它的孩子是我在图片中看到的日历“日历”之一。 “基本”日历的父级是 Internet 日历。如何将 olFolder 设置为目录 '\Internet Calendars\basic' 而不是 '\email@gmail.com\Calendar'?
【问题讨论】:
-
“这些都不起作用”不能传达有用的信息。说明两次尝试中的每一次会发生什么。
-
对。运行时错误“-2147221233 (8004010f)”:尝试的操作失败。找不到对象。
-
在原始代码中,对象 olFolder 是一个 Outlook 文件夹。
Set olFolder = olNS.GetDefaultFolder(9)。您过早地在Set olFolder = olNS.GetDefaultFolder(9).Parent.Folders("basic").Items中引入了项目。丢弃 .Items -
修改它以查看 olNS.GetDefaultFolder(9).Parent 下的日历。 stackoverflow.com/questions/33655041/…
-
好的,这就是我最终找到的:olNS.GetDefaultFolder(9).Parent = email@gmail.com 和它的孩子是我在图片中看到的日历“日历”之一。 “基本”日历的父级是 Internet 日历。如何将 olFolder 设置为目录 '\\Internet Calendars\basic' 而不是 '\\email@gmail.com\Calendar'