如何将Outlook日历导出到Excel每周时间表?

Shw*_*thy -2 excel outlook vba outlook-vba

我的老板在Outlook中安排了(不同项目)的会议日历.他希望我从Outlook日历中编写VBA,以便获取数据(项目,日期和时间),并在Excel工作表中输入每天在每个项目上花费的时间.Excel工作表包含以列形式编写的所有项目,以及一周中每天的一列.

代码应该能够在当天插入每个项目上花费的时间,并在Excel工作表中插入项目.

我之前从未使用过Excel.

R3u*_*3uK 10

一个简单的谷歌搜索,一点点重构:

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/2017")
    ToDate = CDate("12/31/2017")

    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
    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
Run Code Online (Sandbox Code Playgroud)

  • 感谢您提供 Excel VBA 代码。我建议在代码中添加两个`Date`变量`FromDate`和`ToDate`,并将`If (olApt.Start &gt;= FromDate and olApt.Start &lt;= ToDate) Then ...`添加到`For`循环中,以限制导出到相关日期范围的数据量。 (3认同)
  • 另一个改进:将 `.Cells(NextRow, "C").NumberFormat = "HH:MM:SS"` 添加到 `For` 循环,它将正确显示每个约会的持续时间。 (2认同)
  • 这在Office 2016中仍然运行良好!我添加了这一行:`.Cells(NextRow,"E").Value = olApt.Categories`如果你想为不同的项目分类你的会议等. (2认同)