简体   繁体   English

从 Excel Z6E3EC7E6A9F79007B098AFCZ 访问 Outlook email 的“主体”

[英]Accessing the ‘Body’ of an Outlook email from Excel VBA

The following code stopped working after upgrading from Office 2010 on W7 to Office 365 on W10.从 W7 上的 Office 2010 升级到 W10 上的 Office 365 后,以下代码停止工作。

Sub readbodytest()

    Dim OL As Outlook.Application
    Dim DIB As Outlook.Folder
    Dim i As Object 'Outlook.ReportItem
    Dim Filter As String

    Set OL = CreateObject("Outlook.Application")
    Set DIB = OL.Session.GetDefaultFolder(olFolderInbox)

    Const PR_SENT_REPRESENTING_EMAIL_ADDRESS = "http://schemas.microsoft.com/mapi/proptag/0x0065001E"
    Filter = "@SQL=" & _
        """" & PR_SENT_REPRESENTING_EMAIL_ADDRESS & """ ci_phrasematch 'mailer-daemon' OR " & _
        """" & PR_SENT_REPRESENTING_EMAIL_ADDRESS & """ ci_phrasematch 'postmaster' OR " & _
        "urn:schemas:httpmail:subject ci_phrasematch 'undeliverable' OR " & _
        "urn:schemas:httpmail:subject ci_phrasematch 'returned'"
    For Each i In DIB.Items.Restrict(Filter)
        Debug.Print i.Body '<< Code fails here
    Next

    Set i = Nothing
    Set DIB = Nothing
    Set OL = Nothing
End Sub

It returns a runtime error -2147467259 “Method 'Body' of object '_MailItem' failed”它返回运行时错误 -2147467259 “object '_MailItem' 的方法 'Body' 失败”

The code will work when run directly in outlook VBA, but not when run externally.代码直接在 outlook VBA 中运行时可以工作,但在外部运行时则不行。

The purpose of the code was to do a bulk review of returned mail items, match information in the Body of the email to a record on a database, and update the database to record the failure.代码的目的是对退回的邮件进行批量审查,将 email 的正文中的信息与数据库上的记录匹配,并更新数据库以记录失败。

Looking to see if anyone has any suggestions before I just re-write the code to run in reverse (eg from Outlook VBA to the database; instead of the database trying to retrieve it from Outlook)在我重新编写代码以反向运行之前看看是否有人有任何建议(例如,从 Outlook VBA 到数据库;而不是数据库试图从 Outlook 检索它)

It makes sense to use the Logon method of the Application class which logs the user on to MAPI, obtaining a MAPI session.使用应用程序 class 的登录方法是有意义的,该方法将用户登录到 MAPI,获得 MAPI session。 Here is what MSDN says:这是 MSDN 所说的:

Use the Logon method only to log on to a specific profile when Outlook is not already running.当 Outlook 尚未运行时,仅使用登录方法登录到特定配置文件。 This is because only one Outlook process can run at a time, and that Outlook process uses only one profile and supports only one MAPI session.这是因为一次只能运行一个 Outlook 进程,而 Outlook 进程只使用一个配置文件,并且只支持一个 MAPI session。 When users start Outlook a second time, that instance of Outlook runs within the same Outlook process, does not create a new process, and uses the same profile.当用户第二次启动 Outlook 时,该 Outlook 实例在同一 Outlook 进程中运行,不会创建新进程,并使用相同的配置文件。

If Outlook is not running and you only want to start Outlook with the default profile, do not use the Logon method.如果 Outlook 未运行,并且您只想使用默认配置文件启动 Outlook,请不要使用登录方法。 A better alternative is shown in the following code example, InitializeMAPI: first, instantiate the Outlook Application object, then reference a default folder such as the Inbox.下面的代码示例 InitializeMAPI 显示了更好的替代方法:首先,实例化 Outlook 应用程序 object,然后引用默认文件夹,例如收件箱。 This has the side effect of initializing MAPI to use the default profile and to make the object model fully functional.这具有初始化 MAPI 以使用默认配置文件并使 object model 功能齐全的副作用。

Second, I'd suggest checking the item type before accessing any properties.其次,我建议在访问任何属性之前检查项目类型。 Not all items may contain such properties.并非所有项目都可能包含此类属性。

Another possible pitfall and most probably that is a security issue when dealing with the Outlook object model.在处理 Outlook object model 时,另一个可能的陷阱很可能是一个安全问题。 When you try to access any sensitive property Outlook may trigger a security issue (it may be an error in the code or UI guard/prompt).当您尝试访问任何敏感属性时,Outlook 可能会触发安全问题(可能是代码或 UI 保护/提示中的错误)。 "Security" in this context refers to the so-called "object model guard" that triggers security prompts and blocks access to certain features in an effort to prevent malicious programs from harvesting email addresses from Outlook data and using Outlook to propagate viruses and spam. "Security" in this context refers to the so-called "object model guard" that triggers security prompts and blocks access to certain features in an effort to prevent malicious programs from harvesting email addresses from Outlook data and using Outlook to propagate viruses and spam. You can use the following ways to bridge the gap:您可以使用以下方法来弥补差距:

  1. The Security Manager for Outlook component allows to turn prompts off/on at runtime. Outlook 组件的安全管理器允许在运行时关闭/打开提示。

  2. Use the low-level code which doesn't generate security prompts.使用不生成安全提示的低级代码。 Or any other third-party wrappers around that API (for example, Redemption).或围绕该 API 的任何其他第三方包装器(例如,兑换)。

  3. Deploy a group policy to avoid security prompts.部署组策略以避免安全提示。

  4. Running an up-to-date antivirus software.运行最新的防病毒软件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM