VBA - An outgoing call cannot be made since the application is dispatching an input-synchronous call

575 views Asked by At

I want to send an email via code using Microsoft Visual Basic for Applications. The Microsoft Outlook 12.0 Object Library is checked under Tools/References. The code compiles error-free. The code works fine on two different machines, but when I test it on a third machine, it gives the error when executing line #2.

Dim email As Outlook.MailItem
Set email = Application.CreateItem(olMailItem)
    email.To = "[email protected]"
    email.Subject = "Subject"
    email.Body = "Body"
    email.Send
Set email = Nothing

I have reviewed many posts about this error but cannot find a matching solution. Thanks in advance for your help.

2

There are 2 answers

0
Steve Duff On BEST ANSWER

Thanks, everyone, for the intial responses. Turns out what solved the problem is to use CDO for sending the email. Here is the code that worked for me:

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\[email protected]"
Flds.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "myPassword"
Flds.Update
Set objMessage.Configuration = objConfig
objMessage.To = "[email protected]"
objMessage.From = "[email protected]"
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
2
Dmitry Streblechenko On

When exactly is that code being executed? Is it running from a tray icon event handler by any chance?

Start up a timer, when the timer fires (you will be out of the incoming RPC call), run your code above.