【发布时间】: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