Using CDO.sys dll for sending messages in VBA freezes Excel

一世执手 提交于 2020-07-09 12:50:11

问题


I'm facing with a strange problem using CDO.sys for sending email messages from a VBA-enabled Excel application. I've used the following code, found at https://www.rondebruin.nl/win/s1/cdo.htm :

Sub SendMailCDO(FSubject As String, FBody As String, FFrom As String, FTo As String, FCc As String)

    Dim iMsg As Object
    Dim iConf As Object
    Dim Flds As Variant

    Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")

    iConf.Load -1    ' CDO Source Defaults
    Set Flds = iConf.Fields
    With Flds
        .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "myServerIPAddress"
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
        .Update
    End With

    With iMsg
        Set .Configuration = iConf
        .To = "mydestination"
        .CC = ""
        .BCC = ""
        .From = FFrom
        .Subject = FSubject
        .TextBody = ""
        .HTMLBody = FBody
        .Send
    End With
    MsgBox ("Email sent to " & FTo)
    Set iMsg = Nothing
    Set iConf = Nothing
    Set Flds = Nothing
End Sub

Now, the code works fine and the emails are sent. However, after the first email is sent, immediately Excel becomes unresponsive on many different commands. For example, normal operations like changing the font dimensions in cells or applying a conditional formatting are not active anymore. In addition, Excel appear slow executing everything. The only solution to fix this condition is to close Excel and open it again. Any suggestion to fix the problem? Thanks in advance, Emilio


回答1:


Absolutely unexpected, but removing the Msgbox call "Email sent to" solved the problem!



来源:https://stackoverflow.com/questions/51326781/using-cdo-sys-dll-for-sending-messages-in-vba-freezes-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!