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
您是否嘗試過在沒有「On Error Resume Next」的情況下運行它,以便您可以查看是否有任何錯誤?你是否已經瀏覽了代碼,看看它是否達到'.Send'? – YowE3K
是的,我已經嘗試過了,但仍然沒有錯誤信息 – mabanger
嘗試將'.Send'改爲'.Display'(我認爲是對的 - 但我從未使用過Outlook VBA),以便它顯示消息而不是發送它。它顯示嗎? – YowE3K