VBA - 由于应用程序正在调度输入同步调用,因此无法进行传出呼叫

时间:2016-12-21 20:00:00

标签: vba email outlook basic

我想使用Microsoft Visual Basic for Applications通过代码发送电子邮件。在工具/参考下选中Microsoft Outlook 12.0对象库。代码编译无错误。代码在两台不同的机器上运行良好,但是当我在第三台机器上测试时,它在执行第2行时会出错。

Dim email As Outlook.MailItem
Set email = Application.CreateItem(olMailItem)
    email.To = "myemailaddress@gmail.com"
    email.Subject = "Subject"
    email.Body = "Body"
    email.Send
Set email = Nothing

我已查看过很多关于此错误的帖子,但无法找到匹配的解决方案。在此先感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

什么时候执行该代码?它是否从托盘图标事件处理程序运行?

启动计时器,当计时器触发时(您将不在传入的RPC调用中),运行上面的代码。

答案 1 :(得分:0)

谢谢大家的回复。原来解决问题的是使用CDO发送电子邮件。以下代码对我有用:

Set objMessage = CreateObject("cdo.message")
Set objConfig = CreateObject("cdo.configuration")
Set Flds = objConfig.Fields
Flds.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
Flds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "myExchangeServerName"
Flds.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1  'basic (clear-text) authentication
Flds.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "myDomain\user@myDomain.com"
Flds.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "myPassword"
Flds.Update
Set objMessage.Configuration = objConfig
objMessage.To = "toEmail@someDomain.com"
objMessage.From = "fromEmail@someOtherDomain.com"
objMessage.Subject = "My Subject"
objMessage.Fields.Update
objMessage.HTMLBody = "<p><span style=""font-family: 'Calibri','Arial','sans-serif'"";>My Body Text</span></p>"
objMessage.AddAttachment "C:/path/filename.txt"
objMessage.Send