首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Pywin32 (win32com.client)获取Outlook组日历

使用Pywin32 (win32com.client)获取Outlook组日历
EN

Stack Overflow用户
提问于 2022-01-19 22:27:52
回答 1查看 419关注 0票数 0

我试图弄清楚如何访问我所属的不同Outlook组的日历。我使用win32com和Python3.9来完成这个任务,并且希望避免使用RESTful/auth令牌路径,因为这将是一个简单的脚本插入几个日历约会的相当大的开销。

我可以使用以下代码获得我自己的预约:

代码语言:javascript
运行
复制
import win32com.client

application = win32com.client.Dispatch('Outlook.Application')
namespace = application.GetNamespace('MAPI')

cal = namespace.GetDefaultFolder(9)
    
for item in cal.Items:
    print(item.Subject)

在我的个人日历中返回每次约会的主题行。

我还可以使用GetSharedDefaultFolder获得相同的信息:

代码语言:javascript
运行
复制
application = win32com.client.Dispatch('Outlook.Application')
namespace = application.GetNamespace('MAPI')

recipient = namespace.createRecipient("{my_email}")
resolved = recipient.Resolve()

sharedCalendar = namespace.GetSharedDefaultFolder(recipient, 9)

for item in sharedCalendar.Items:
    print(item.Subject)

我读到你想把创建所需日历的人的电子邮件/用户作为收信人传递给我,但我没有这方面的运气。

在尝试使用创建者的电子邮件时,我会收到以下错误:

代码语言:javascript
运行
复制
pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'The operation failed because of a registry or installation problem. Restart Outlook and try again. If the problem persists, reinstall.', None, 0, -2147221219), None)

如果我尝试使用它们(lastName,firstName),我会得到以下内容:

代码语言:javascript
运行
复制
pywintypes.com_error: (-2009857777, 'OLE error 0x8834010f', None, None)

请注意,我指的是日历,而不是共享日历。我不确定这两者之间是否真的有区别,但它们在我看来是Outlook中不同的部分。

我所指的一些参考资料(对于那些发现本页并有此问题的其他人):

通过Python读取Outlook事件

https://learn.microsoft.com/en-us/office/vba/api/outlook.namespace.getdefaultfolder (及相关页面)

通过python读取Outlook共享日历的问题

https://learn.microsoft.com/en-us/answers/questions/607061/how-to-create-new-events-share-calendar-with-pytho.html

EN

Stack Overflow用户

回答已采纳

发布于 2022-01-19 22:40:04

在委托场景中使用NameSpace.GetSharedDefaultFolder方法,其中一个用户为他们的一个或多个默认文件夹(例如,共享日历文件夹)向另一个用户委派了访问权限。因此,您需要确保用户作为委托访问您。

如果日历在Outlook的导航窗格中可见,则可以使用NavigationGroups对象访问它。可以使用NavigationGroupsNavigationFolders集合遍历导航窗格中模块的组和文件夹层次结构。NavigationGroups对象的NavigationModule集合包含显示在导航模块中的每个导航组,而NavigationGroup对象的NavigationFolders集合包含显示在导航组中的每个导航文件夹。通过组合使用这些集合,可以枚举导航窗格中显示的导航模块的每个导航文件夹。下面是示例VBA代码,您可以在该代码中了解所需的OOM属性方法以获得所需的日历:

代码语言:javascript
运行
复制
Dim WithEvents objPane As NavigationPane 
 
Private Sub EnumerateActiveCalendarFolders() 
 Dim objModule As CalendarModule 
 Dim objGroup As NavigationGroup 
 Dim objFolder As NavigationFolder 
 Dim intCounter As Integer 
 
 On Error GoTo ErrRoutine 
 
 ' Get the NavigationPane object for the 
 ' currently displayed Explorer object. 
 Set objPane = Application.ActiveExplorer.NavigationPane 
 
 ' Get the CalendarModule object, if one exists, 
 ' for the current Navigation Pane. 
 Set objModule = objPane.Modules.GetNavigationModule(olModuleCalendar) 
 
 ' Iterate through each NavigationGroup contained 
 ' by the CalendarModule. 
 For Each objGroup In objModule.NavigationGroups 
 ' Iterate through each NavigationFolder contained 
 ' by the NavigationGroup. 
 For Each objFolder In objGroup.NavigationFolders 
 ' Check if the folder is selected. 
 If objFolder.IsSelected Then 
 intCounter = intCounter + 1 
 End If 
 Next 
 Next 
 
 ' Display the results. 
 MsgBox "There are " & intCounter & " selected calendars in the Calendar module." 
 
EndRoutine: 
 On Error GoTo 0 
 Set objFolder = Nothing 
 Set objGroup = Nothing 
 Set objModule = Nothing 
 Set objPane = Nothing 
 intCounter = 0 
 Exit Sub 
 
ErrRoutine: 
 MsgBox Err.Number & " - " & Err.Description, _ 
 vbOKOnly Or vbCritical, _ 
 "EnumerateActiveCalendarFolders" 
End Sub

Outlook对象模型对于所有类型的编程语言来说都是常见的,因此要识别完成工作所需的属性和方法的顺序并不需要付出很大的努力。

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70778543

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档