【发布时间】:2021-01-22 16:46:52
【问题描述】:
我想将 Outlook 中的所有约会提取到 Excel 文件中。最终目标是使用这些数据进行时间分析。
我的代码提取了单个实例的会议,但未能提取所有重复会议。
我已经看到了这个问题的几个例子,但是我没有成功地挖掘他们的信息来改进下面的代码。
Option Explicit
Sub RetrieveApts
Dim olApp As Object
Dim olNS As Object
Dim olFolder As Object
Dim olApt As Object
Dim oAppointments As Object
Dim FolderItems As Outlook.Items
Dim NextRow As Long
Dim FromDate As Date
Dim ToDate As Date
Dim pos As Integer
Application.ScreenUpdating = False' Turns off performance reducing functionality
Application.CutCopyMode = False' Turns off performance reducing functionality
FromDate = CDate("10/04/2020") 'Hardcoded for now
ToDate = CDate ("10/09/2020")' Long term these date references will be user set via inputs
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")'Sets Outlook Reference
If Err.Number > 0 Then Set olApp = CreateObject("Outlook.Application")'Opens Outlook if Outlook was Closed
Set olNS = olApp.GetNameSpace("MAPI")
Set olFolder = olNS.GetDefaultFolder(9)
NextRow =2
With olFolder.Items
.Sort "[Start]", True
.IncludeRecurrences = True
End With
With Sheets("Sheet1")
'Specifies where to store information
.range("A1:H1").value = _
Array("Subject","Date","Time Spent", "Location", "Required Attendees", "Optional Attendees", "Categorization", "Body")
For Each olApt In olFolder.Items'Begins Examination of Each Calendar Apt
'Checks to see if Apt. within date range
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.RequiredAttendees
.cells(NextRow,"F").Value= olApt.OptionalAttendees
.cells(NextRow,"G").Value= olApt.Categories
.cells(NextRow,"H").Value= olApt.Body
Else
End IF
Next olApt
End With
Set olApt = Nothing
Set olFolder = Nothing
Set olNS = Nothing
Set olApp = Nothing
Application.ScreenUpdating = True
Application.CutCopyMode = True
End Sub
【问题讨论】:
-
一些潜在的响应者可能不想修复错别字。如果您希望他们尝试回答您可以修复,然后直接从您的 VB 编辑器中复制。以
Option Explicit开头。 -
@niton 道歉。尝试手动清理。由于防火墙限制,无法直接从工作笔记本电脑复制粘贴,而且我的个人电脑上实际上没有 excel,所以我不得不手动重新创建。
标签: excel vba outlook calendar