简体   繁体   English

使用 VBA 和 Outlook 发送 email 时出错

[英]Error while sending email using VBA with Outlook

I would like to make an easy automation within excel to send an email from the user form when fulfilled by the user.我想在 excel 中进行简单的自动化,以便在用户完成时从用户表单发送 email。 I have created test code to do so, according to Microsoft how to page.我已经创建了测试代码来做到这一点,根据微软如何页面。 My main problem is visible when I try to use.send (email address swapped to dummy one for the purpose of the question) -当我尝试使用时,我的主要问题是可见的。发送(出于问题的目的,电子邮件地址已交换为虚拟地址)-

"application-defined or object-defined error". “应用程序定义的或对象定义的错误”。

However when I use.display the message shows up on the screen with no issues.但是,当我 use.display 时,消息会毫无问题地显示在屏幕上。 I have added references for Outlook 16 and Office 16, what was suggested in other solutions, and still no positive result.我添加了 Outlook 16 和 Office 16 的参考,其他解决方案中建议的内容,但仍然没有积极的结果。 I wonder if that might be an issue related to company policies... but not sure, just wanted to ask for some guidance.我想知道这是否可能与公司政策有关……但不确定,只是想寻求一些指导。 Below is the code that was causing the error:以下是导致错误的代码:

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. 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. It happens when you try to automate Outlook from external applications.当您尝试从外部应用程序自动化 Outlook 时,就会发生这种情况。 You can find a similar thread which lists possible solutions, see How to avoid the Outlook Security warning for email automation - VBA .您可以找到列出可能解决方案的类似线程,请参阅如何避免 email 自动化的 Outlook 安全警告 - VBA

There are several ways for suppressing security prompts/errors in the code:有几种方法可以抑制代码中的安全提示/错误:

  1. Use a third-party components for suppressing Outlook security warnings.使用第三方组件来抑制 Outlook 安全警告。 See Security Manager for Microsoft Outlook for more information.有关详细信息,请参阅Microsoft Outlook 的安全管理器

  2. Use a low-level API instead of OOM.使用低级 API 代替 OOM。 Or any other third-party wrappers around that API, for example, Redemption.或围绕该 API 的任何其他第三方包装器,例如 Redemption。

  3. Develop a COM add-in which has access to the trusted Application object.开发一个 COM 插件,它可以访问受信任的Application object。

  4. Use group policy objects for setting up machines.使用组策略对象来设置机器。

See Outlook Object Model Security Warnings for more information.有关详细信息,请参阅Outlook Object Model 安全警告 Also you may find the following articles helpful:此外,您可能会发现以下文章很有帮助:

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

相关问题 从 Excel VBA 通过 Outlook 发送电子邮件时出错 - Error While Sending Email through Outlook from Excel VBA Excel VBA 代码在使用任务计划程序发送 Outlook 电子邮件时引发运行时错误 429 - Excel VBA code throws run-time error 429 while sending Outlook email using Task Scheduler 使用Excel VBA发送Outlook电子邮件会生成错误:未定义用户定义的类型 - Sending Outlook Email Using Excel VBA generates error: user-defined type not defined VBA运行时错误:为什么在使用mailenvelope通过Outlook发送邮件时出现错误 - VBA runtime error : Why am i getting an error while sending mail through outlook using mailenvelope 使用Excel VBA从Outlook发送富文本电子邮件 - Sending rich text email from Outlook using Excel VBA 使用VBA Excel在特定日期发送带有Outlook的电子邮件 - Sending email with outlook at a particular date, using VBA excel 使用VBA在Outlook Body中发送带有PNG图像的电子邮件 - Sending Email with PNG image in Outlook Body using VBA 当 Outlook 不可用时,使用 Z6E3EC7E6A9F6007B4838FC0EEZ9A 从 Excel 发送 Email - Sending an Email from Excel when Outlook not available using VBA 运行时错误424:未定义对象“-使用Outlook 2013发送电子邮件时 - Runtime Error 424: Object not Defined"- While Sending Email Using Outlook 2013 VBA-向未知数量的收件人发送Outlook电子邮件 - VBA - Sending an Outlook Email to unknown number of recipients
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM