[英]Excel VBA code generates emails but crashes Outlook
下面的這段代碼創建了(大約60個)帶有2個附件的電子郵件(由於需要視覺驗證而沒有發送),一個電子郵件是209KB pptx(我盡可能將其壓縮)和一個.xlsb文件(30Kb-700kb,具體取決於)。 文本為HTML格式只是因為我們需要突出顯示。 我認為這比在前景之外調用模板要好,但是如果那是錯誤的,請告訴我,我找不到有關此信息。
問題是,它將生成所有電子郵件並附加所有文件,同時凍結了我的視線,以至於我必須關閉所有內容並從任務管理器重新啟動。 我已經等待了一個多小時,看看它是否有效,但是它只會生成電子郵件,然后凍結。 我可以通過任務欄看到電子郵件,但是無法選擇它們或Outlook收件箱。
知道如何克服這個嗎?
Sub email()
Dim olApp As Outlook.Application
Dim olMail As Outlook.MailItem
Dim MailMessage As String
Dim CusName As String
Dim Lastrow As Long
Dim i As Long
Application.ScreenUpdating = False
Call getemails
MailMessage = "<HTML><BODY> Hello, <br><br>" _
& "Attached you will find your trailing 12 month (TTM) margin leak
report which was discussed on the Best Practices call in August. (Deck from
meeting attached as well)<br><br><br>" _
& "<li> Tab 1 shows margin leak by item<br><br>" _
& "<li>Tab 2 shows margin leak by vendor then by item<br><br>" _
& "<li>Tab 3 is data tab where you can see all the data<br><br>" _
& "Tab 1 and 2 includes a filter at the top so you can look at or
exclude specific PCATs.<br><br>" _
& "<b>Key definitions of fields on data tab:</b><br><br>" _
& "<li>Base Price, Base Cost, Base Margin – Price, Cost and Margin
dollars prior to margin leak<br><br>" _
& "Thank you,<br><br>" _
Lastrow = Range("A" & rows.Count).End(xlUp).Row
For i = 1 To Lastrow
Set olApp = GetObject(Class:="Outlook.Application")
If olApp Is Nothing Then
Set olApp = CreateObject(Class:="outlook.application")
End If
Set olMail = olApp.CreateItem(0)
With olMail
.To = Cells(i, 2).Value
.Subject = Format(MonthName(Month(Now))) & " - Margin Leak - " & Cells(i,
1).Value
.display
.HTMLBody = MailMessage
.Attachments.Add ("C:\Linking_Files\Best Practices Margin Leak.pptx")
.Attachments.Add ("C:\Desktop\June\" & Cells(i, 1).Value & ".xlsb")
End With
Set olMail = Nothing
Set olApp = Nothing
Next i
Application.ScreenUpdating = True
End Sub
內存不足問題,取決於您生成的電子郵件數量。 在循環中添加保存和結束操作,以避免內存不足。
對於我的Excel(2010)版本,可以很好地減少內存使用:
With olMail
.To = Cells(i, 2).Value
.Subject = Format(MonthName(Month(Now))) & " - Margin Leak - " & Cells(i, 1).Value
.display
.HTMLBody = MailMessage
.Attachments.Add ("C:\Users\u\Desktop\test.xls")
.Save
.Close 1
End With
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.