Excel电子邮件挂起Outlook直到发送 - 错误处理?

时间:2013-12-12 22:23:16

标签: excel vba email excel-2007

我正在使用宏发送Excel工作簿,如下所示。我使用空白电子邮件地址,以便在Outlook中显示电子邮件,并允许用户输入电子邮件地址。但是,除非发送或关闭电子邮件而不发送,否则Excel将不允许用户在Outlook中执行任何其他操作,甚至打开附件以进行检查。在处理完电子邮件之前,它不会关闭文件,因此它会停留在此循环中。我怎么能绕过这个?

TempFilePath = Environ$("temp") & "\"
TempFileName = "The File Name"
FileExtStr = ".xlsx"

With TheWorkbook
.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
On Error Resume Next
For I = 1 To 3
 .SendMail "", _
    "This is the Subject line"
    If Err.Number = 0 Then Exit For
Next I
On Error GoTo 0
.Close SaveChanges:=False
End With

1 个答案:

答案 0 :(得分:4)

而不是.SendMail为什么不与Outlook绑定?这样,Excel就不必等待操作完成了吗?

参见此示例

Option Explicit

Sub Sample()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim i As Long

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    With ThisWorkbook
        '
        '~~> Do Something
        '

        For i = 1 To 3
            Set OutMail = OutApp.CreateItem(0)

            With OutMail
                .Subject = "This is the Subject line"
                .Body = "Hello World"
                .Attachments.Add TempFilePath & TempFileName & FileExtStr

                '~~> Show the email
                .Display
            End With
        Next i
    End With
End Sub