2017-08-11 57 views
0

我在Excel中有以下腳本,它應該發送一封電子郵件(收件人應該在B24中),但我沒有收到任何錯誤消息,但電子郵件也沒有發送。任何幫助將非常感激。爲什麼這個VB腳本不發送電子郵件?

有人可以向我解釋什麼是錯的,或者我在這裏做錯了什麼?

Sub Email2() 
    Dim sh As Worksheet 
    Dim wb As Workbook 
    Dim FileExtStr As String 
    Dim FileFormatNum As Long 
    Dim TempFilePath As String 
    Dim TempFileName As String 
    Dim OutApp As Object 
    Dim OutMail As Object 

    TempFilePath = Environ$("temp") & "\" 

    If Val(Application.Version) < 12 Then 
     FileExtStr = ".xls": FileFormatNum = -4143 
    Else 
     FileExtStr = ".xlsm": FileFormatNum = 52 
    End If 

    With Application 
     .ScreenUpdating = False 
     .EnableEvents = False 
    End With 

    Set OutApp = CreateObject("Outlook.Application") 

    For Each sh In ThisWorkbook.Worksheets 
     If sh.Range("B28").Value Like "?*@?*.?*" Then 

      sh.Copy 
      Set wb = ActiveWorkbook 

      TempFileName = "Performance " & sh.Name & " date " & Format(Now, "dd-mmm-yy h-mm-ss") 

      Set OutMail = OutApp.CreateItem(0) 

      With wb 
       .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum 

       On Error Resume Next 
       With OutMail 
        .TO = sh.Range("B24").Value 
        .CC = "" 
        .BCC = "" 
        .Subject = "This is the subject" 
        .Body = "Hello," 
        .Attachments.Add wb.FullName 
        .Send 
       End With 
       On Error GoTo 0 

       .Close savechanges:=False 
      End With 

      Set OutMail = Nothing 

      Kill TempFilePath & TempFileName & FileExtStr 

     End If 
    Next sh 

    Set OutApp = Nothing 

    With Application 
     .ScreenUpdating = True 
     .EnableEvents = True 
    End With 
End Sub 
+2

您是否嘗試過在沒有「On Error Resume Next」的情況下運行它,以便您可以查看是否有任何錯誤?你是否已經瀏覽了代碼,看看它是否達到'.Send'? – YowE3K

+0

是的,我已經嘗試過了,但仍然沒有錯誤信息 – mabanger

+0

嘗試將'.Send'改爲'.Display'(我認爲是對的 - 但我從未使用過Outlook VBA),以便它顯示消息而不是發送它。它顯示嗎? – YowE3K

回答

1

你爲我提供工作在Excel 2010中

所有我能看到的是,你是否在「B28」的值看起來像一個電子郵件地址,然後發送電子郵件到的代碼地址在「B​​24」中。這是問題嗎?

對於我的測試,我把我的地址放在「B28」和「B24」中。

+0

大聲笑 - 我正準備發表評論! (我想我會+1,因爲它幾乎肯定會是原因。) – YowE3K