【问题标题】:Grab Outlook calendar with Python使用 Python 获取 Outlook 日历
【发布时间】:2020-09-02 13:13:36
【问题描述】:

我有一个 VBA 脚本,它可以抓取 MS Outlook 项目(特别是 Outlook 约会)并在 Excel 表格中输出结果。它只是抓住接下来 7 天的会议。

我想知道是否有办法解决这个问题 a) 转换成等效的 python 脚本 OR b) 调用运行此 vba 脚本并将结果输出到 python 列表中

阻力最小的路径是什么?

Sub FindAppts()

Application.EnableEvents = False
    Dim myStart As Date
    Dim myEnd As Date
    Dim ws1 As Worksheet
    Dim CalRawOutput As Range
    Dim o As Outlook.Application
    Dim ons As Outlook.Namespace
    Dim oCalendar As Outlook.Folder
    Dim oItems As Outlook.Items
    Dim oItemsInDateRange As Outlook.Items
    Dim oFinalItems As Outlook.Items
    Dim oAppt As Outlook.AppointmentItem
    Dim strRestriction As String
    Dim lastrownum As Integer
    Dim OutputRange As Range

Set ws1 = ActiveWorkbook.Sheets("Calendar")
lastrownum = ws1.Cells(ws1.Rows.Count, "B").End(xlUp).row
Set OutputRange = ws1.Range(Cells(53, 2).Address(), Cells(lastrownum, 5).Address())
OutputRange.ClearContents


    myStart = Date
    myEnd = DateAdd("d", 7, myStart)

    'Construct filter for the next 7-day date range
    strRestriction = "[Start] >= '" & _
    Format$(myStart, "mm/dd/yyyy hh:mm AMPM") _
    & "' AND [End] <= '" & _
    Format$(myEnd, "mm/dd/yyyy hh:mm AMPM") & "'"
    'Check the restriction string
    Set o = New Outlook.Application
    Set ons = o.GetNamespace("MAPI")
    Set oCalendar = ons.GetDefaultFolder(olFolderCalendar)
    Set oItems = oCalendar.Items
    oItems.IncludeRecurrences = True
    oItems.Sort "[Start]"
    'Restrict the Items collection for the 7-day date range
    Set oItemsInDateRange = oItems.Restrict(strRestriction)
        & "0x0037001E" & Chr(34) & " like '%team%'"
    oItemsInDateRange.Sort "[Start]"
r = 1
Set CalRawOutput = ws1.Range("B53:E100")
    For Each oAppt In oItemsInDateRange


            CalRawOutput.Cells(r, 1).Value = oAppt.Subject
            CalRawOutput.Cells(r, 2).Value = oAppt.Start
            CalRawOutput.Cells(r, 3).Value = oAppt.End
            CalRawOutput.Cells(r, 4).Value = oAppt.Location
            r = r + 1
    Next
Application.EnableEvents = True
End Sub

【问题讨论】:

    标签: python python-3.x excel vba outlook


    【解决方案1】:

    Application.Run 方法允许运行宏或调用函数。这可用于运行用 Visual Basic 或 Microsoft Excel 宏语言编写的宏,或运行 DLL 或 XLL 中的函数。第一个参数可以是带有宏名称的字符串、指示函数所在位置的 Range 对象或已注册 DLL (XLL) 函数的寄存器 ID。如果使用字符串,则将在活动工作表的上下文中评估该字符串。

    Application.Run "FindAppts"
    

    如果 VBA 子文件位于特定文件中,则需要使用以下构造:

    Application.Run "'My Work Book.xls'!FindAppts"
    

    所以,基本上你只需要从 Python 自动化 Excel 并使用 Run 方法来运行你的代码。

    但 Excel 对象模型对于所有类型的应用程序或编程语言都是通用的。您可以将代码移植到 Python,请参阅Automation Excel from Python 了解更多信息。例如,看看Python Win32 extensionsopenpyxl

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-02
      • 1970-01-01
      • 1970-01-01
      • 2016-09-25
      • 2022-07-14
      • 2022-11-16
      相关资源
      最近更新 更多