繁体   English   中英

使用 VBA 和 Outlook 发送 email 时出错

[英]Error while sending email using VBA with Outlook

我想在 excel 中进行简单的自动化,以便在用户完成时从用户表单发送 email。 我已经创建了测试代码来做到这一点,根据微软如何页面。 当我尝试使用时,我的主要问题是可见的。发送(出于问题的目的,电子邮件地址已交换为虚拟地址)-

“应用程序定义的或对象定义的错误”。

但是,当我 use.display 时,消息会毫无问题地显示在屏幕上。 我添加了 Outlook 16 和 Office 16 的参考,其他解决方案中建议的内容,但仍然没有积极的结果。 我想知道这是否可能与公司政策有关……但不确定,只是想寻求一些指导。 以下是导致错误的代码:

Sub Mail_workbook_Outlook()

    Dim OutApp As Object
    Dim OutMail As Object
    Dim oListObj As Object

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    
    email_to = "test.user@test.com"
    email_body = "test 123"
    email_subject = "Test email"
    
    With OutMail
        .To = email_to
        .CC = ""
        .BCC = ""
        .Subject = email_subject
        .Body = email_body
        '.Display
        .Send ' THIS LINE IS CAUSING ERROR FOR ME
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing
    
End Sub

The Send method of the MailItem class is a protected method in the Outlook object model which means it can throw security prompts in Outlook or just give errors when using an unsafe Outlook Application instance. 当您尝试从外部应用程序自动化 Outlook 时,就会发生这种情况。 您可以找到列出可能解决方案的类似线程,请参阅如何避免 email 自动化的 Outlook 安全警告 - VBA

有几种方法可以抑制代码中的安全提示/错误:

  1. 使用第三方组件来抑制 Outlook 安全警告。 有关详细信息,请参阅Microsoft Outlook 的安全管理器

  2. 使用低级 API 代替 OOM。 或围绕该 API 的任何其他第三方包装器,例如 Redemption。

  3. 开发一个 COM 插件,它可以访问受信任的Application object。

  4. 使用组策略对象来设置机器。

有关详细信息,请参阅Outlook Object Model 安全警告 此外,您可能会发现以下文章很有帮助:

暂无
暂无

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

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