在我的收件箱下访问文件夹时随机错误
我对Python相对较新,所以不要太粗糙地判断我:)
我有以下代码可以连接到我的收件箱,并将电子邮件移至收件箱下的另一个文件夹。 问题是我第一次打开Outlook时,它起作用。之后,它停止工作并生成错误:
追溯(最近的最新通话): 文件“ C:\ Learn!新过程Cerberus \ OutlookCheck.py”,第72行,in Identity_new_create = common_account.folders ['inbox']。文件夹['Identity Registration']* 文件“ c:\ user \ me \ appdata \ local \ temp \ temp \ gen_py \ 3.7 \ 00062fff-0000-0000-0000-0000-0000-000000000000000000000046x0x9x6x6_folders.py”,第103行 返回自我。 -0000-C000-0000000046}') pywintypes.com_error:(-2147352567,“发生异常”。 >
在结束并再次打开Outlook之后,程序再次起作用。 我试图删除临时文件,在此处搜索答案,但是我尝试过的工作,每次都没有效果和开放式前景并不代表解决方案,因为收件箱是用于办公室的,并且与服务器同步需要永远。 请参阅下面的代码(我已经删除了非相关变量和列表,因为它们不是原因,我已经尝试过):
import win32com.client as client
from pathlib import Path
common_account='WorkAccount'
try:
outlook = client.gencache.EnsureDispatch("Outlook.Application")
except:
home = str(Path.home()) + r'\AppData\Local\Temp\gen_py'
try:
rem(Path(home))
except:
print('Could not perform a Python reset. Please delete temp files and folders yourself')
try:
outlook = client.gencache.EnsureDispatch("Outlook.Application")
except:
print('Could not link to Outlook')
namespace = outlook.GetNamespace("MAPI")
common_account = namespace.Folders[common_account]
common_inbox = common_account.Folders['Inbox'].Items
identity_new_create = common_account.Folders['Inbox'].Folders['Identity Registration']
# Everything is perfect, program run flowless on the first run.
# After the first run I get the error above.
# After closing Outlook and opening it again, program runs again without error.
email = common_inbox.GetLast()
#Some other irellevant code
email.move(identity_new_create)
以后进行编辑:
我每次我每次搜索文件夹都找到了一种绕过此错误的方法初始化Outlook,但不是一个优雅的解决方案,需要额外的处理时间和计算能力。所以问题仍在继续。
I'm relatively new to Python so don't judge me too rough :)
I have the below code that it suppose to connect to my inbox and move emails to another folder under the inbox.
Problem is that the first time I open Outlook, it works. After that it stop working and generate the error:
Traceback (most recent call last):
File "C:\learn!New Process Cerberus\OutlookCheck.py", line 72, in
identity_new_create = common_account.Folders['Inbox'].Folders['Identity Registration']*
File "C:\Users\ME\AppData\Local\Temp\gen_py\3.7\00062FFF-0000-0000-C000-000000000046x0x9x6_Folders.py", line 103, in getitem
return self.get_good_object(self.oleobj.Invoke((81, LCID, 1, 1, key)), "Item", '{00063006-0000-0000-C000-000000000046}')
pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'The attempted operation failed. An object could not be found.', None, 0, -2147221233), None)
After closing and opening again Outlook, program works again.
I have tried to delete temp files, search here for answers but nothing I tried worked and closing and opening Outlook every time does not represent a solution because the inbox is for office and it takes forever to sync with the server.
Please see below the code (I have cut the non related variables and lists from here as they are not the cause, I've tried):
import win32com.client as client
from pathlib import Path
common_account='WorkAccount'
try:
outlook = client.gencache.EnsureDispatch("Outlook.Application")
except:
home = str(Path.home()) + r'\AppData\Local\Temp\gen_py'
try:
rem(Path(home))
except:
print('Could not perform a Python reset. Please delete temp files and folders yourself')
try:
outlook = client.gencache.EnsureDispatch("Outlook.Application")
except:
print('Could not link to Outlook')
namespace = outlook.GetNamespace("MAPI")
common_account = namespace.Folders[common_account]
common_inbox = common_account.Folders['Inbox'].Items
identity_new_create = common_account.Folders['Inbox'].Folders['Identity Registration']
# Everything is perfect, program run flowless on the first run.
# After the first run I get the error above.
# After closing Outlook and opening it again, program runs again without error.
email = common_inbox.GetLast()
#Some other irellevant code
email.move(identity_new_create)
Later Edit:
I have found a way to bypass this error by searching for the folder each time I initialize Outlook but is not an elegant solution and require extra processing time and computing power. So the question is still on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经看到了该行
可以从您的Exchange帐户返回PF文件夹的情况,而交换帐户显然没有收件箱文件夹。如果您始终想要默认的邮箱,则可以使用
namep.getDefaultFolder(olfolderinbox)
而不是common_account.folders ['inbox']
。您也可以尝试尝试使用
namepace.getSharedDefaultFolder(common_account,olfolderinbox)
。I have seen cases where the line
can return a PF folder from your Exchange account, which obviously does not have an Inbox folder. If you always want the default mailbox, you can use
Namespace.GetDefaultFolder(olFolderInbox)
instead ofcommon_account.Folders['Inbox']
.You can also try to use
Namespace.GetSharedDefaultFolder(common_account, olFolderInbox)
instead.似乎以下代码在运行时会引发错误:
在这种情况下,我建议您在所有子文件夹上进行迭代并检查文件夹名称,因此您将找到目标文件夹并至少确保存在此类文件夹存在那里。
另外,我已经注意到了以下代码行:
而是使用 getDefaultFolder 返回
文件夹
对象的方法,它表示当前配置文件的默认文件夹;例如,为当前登录的用户获取默认inbox
文件夹。然后,您可以得到子文件夹。如果您在Outlook中配置了多个帐户,则可以使用 namepace.stores 返回
stores
集合对象的属性,该对象表示当前配置文件中的所有Store
对象。It seems the following line of code throws an error at runtime:
In that case I'd recommend iterating over all subfolders and checking the folder name, so you will find the target folder and at least be sure such folder exists there.
Also I've noticed the following line of code:
Instead, use the GetDefaultFolder method which returns a
Folder
object that represents the default folder of the requested type for the current profile; for example, obtains the defaultInbox
folder for the user who is currently logged on. Then you can get subfolders.If you have got several accounts configured in Outlook you may use the NameSpace.Stores property which returns a
Stores
collection object that represents all theStore
objects in the current profile.