簡體   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