【问题标题】:Excel macro to send an email generates runtime error 287发送电子邮件的 Excel 宏生成运行时错误 287
【发布时间】:2022-08-21 16:58:37
【问题描述】:

我引用了许多网站,甚至复制和粘贴了代码。我无法让我的 Excel 宏按钮发送电子邮件。

当我在 VBA 项目中单击运行子/用户窗体(播放按钮)时,我得到

运行时错误 287

Sub Send_Email()
    Dim MyOutlook As Object
    Set MyOutlook = CreateObject(\"Outlook.Application\")

    Dim MyMail As Object
    Set MyMail = MyOutlook.CreateItem(olMailItem)

    MyMail.To = \"notlistedpublicly\"
    MyMail.Subject = Range(\"B6\") & \"Has completed his Skills Matrix\"
    MyMail.Body = Range(\"B6\") & \"has completed his Skills Matrix. Please review\"

    MyMail.Send
End Sub
  • 可能是与安全相关的issue

标签: excel vba


【解决方案1】:

我有一个类似的代码,我在你的代码中添加了一些行。

Sub Send_Email()

Dim MyOutlook As Object
Set MyOutlook = CreateObject("Outlook.Application")

Dim MyMail As Object
'I change this
'Set MyMail = MyOutlook.CreateItem(olMailItem)
Set MyMail = MyOutlook.CreateItem(0)

MyOutlook.Session.Logon
With MyMail
      .To = "notlistedpublicly"
      .Subject = Range("B6") & "Has completed his Skills Matrix"
      .Body = Range("B6") & "has completed his Skills Matrix. Please review"
      'Before try to see how the mail looks.
      '.Send
      .Display
End with

Set MyMail = Nothing
Set MyOutlook = Nothing

End Sub

【讨论】:

  • 好的,所以我让它在打开准备发送的电子邮件的位置运行,但我希望它在单击宏按钮后自动发送电子邮件。我注意到当我将 .send 放入代码中时,它会产生调试问题。
  • Outlook 是否打开?
  • 是的。我查看了其他论坛,有人说这可能是安全限制,因为它在工作笔记本电脑上。 .显示工作,但不是 .send
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-14
  • 2017-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-16
  • 2022-07-06
相关资源
最近更新 更多