我試圖循環一組工作表,將它們中的每一個都保存爲單獨的工作簿,然後通過郵件將它們作爲附件發送。通過Outlook發送郵件 - 錯誤287
但是,當運行下面的代碼時,最終由.Send觸發的錯誤287結束。我有前景開放,所以這不是問題。如果我更改。發送到.Display,郵件生成爲草稿,正確顯示並附上正確的表單。
Sub SendWorksheetsByMail()
Dim wb As Workbook
Dim destinationWb As Workbook
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Set wb = Workbooks("Test.xlsm")
Application.EnableEvents = False
Application.ScreenUpdating = False
For Each ws In wb.Worksheets
'Ignore Summary and Config
If ws.Name <> "Summary" And ws.Name <> "Config" Then
'On Error Resume Next
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
ws.Copy
Set destinationWb = ActiveWorkbook
destinationWb.SaveAs "C:\****************\" & ws.Name & ".xlsx", FileFormat:=51
With OutMail
.To = "*******************"
.Subject = "Test"
.Body = "Test"
.Attachments.Add destinationWb.FullName
.Send
End With
Set OutMail = Nothing
Set OutApp = Nothing
End If
Next ws
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
編輯:「它也沒有即使沒有附着實質上生成僅包含主題和文本的消息。‘測試’。」
有關如何解決此問題的任何建議?這將節省大量時間,無需爲每個單獨的郵件點擊發送,因爲發送郵件的數量可能會變得相當大。
你試過'.Save'之前'。發送'?只是一個想法。 –
只是好奇,爲什麼當它包含'With OutMail'時,在'OutMail.Attachments.Add destinationWb.FullName'這一行包含'OutMail'? – BruceWayne
Typo,一開始就寫了它,並且認爲這是起初造成問題的附件,所以拿掉了那部分。然後粘貼它而不改變它。儘管這兩個作品都有效。但會更新上面的代碼。 – johankr