简体   繁体   English

Excel VBA代码生成电子邮件,但使Outlook崩溃

[英]Excel VBA code generates emails but crashes Outlook

This code below creates (it does not send because they need visual verification) several emails (about 60) with 2 attachments, one is 209KB pptx ( I compressed it down as far as I could) and an .xlsb file (30Kb - 700kb depending). 下面的这段代码创建了(大约60个)带有2个附件的电子邮件(由于需要视觉验证而没有发送),一个电子邮件是209KB pptx(我尽可能将其压缩)和一个.xlsb文件(30Kb-700kb,具体取决于)。 Text is in HTML just cause we require some highlighting. 文本为HTML格式只是因为我们需要突出显示。 I thought this would be better than calling a template out of outlook but if thats wrong let me know, I cant find any info about that. 我认为这比在前景之外调用模板要好,但是如果那是错误的,请告诉我,我找不到有关此信息。

The issue is while it will generate all the emails and attach all the files, it freezes my outlook to the point I have to close everything and restart from the task manager. 问题是,它将生成所有电子邮件并附加所有文件,同时冻结了我的视线,以至于我必须关闭所有内容并从任务管理器重新启动。 I have waiting on it for over an hour to see if it works but it just generates the emails and then freezes. 我已经等待了一个多小时,看看它是否有效,但是它只会生成电子邮件,然后冻结。 I can see the emails through my taskbar but i can't select them or my outlook inbox. 我可以通过任务栏看到电子邮件,但是无法选择它们或Outlook收件箱。

Any idea how to over come this? 知道如何克服这个吗?

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

Out of memory issue, when it relies on the amount of email you generate. 内存不足问题,取决于您生成的电子邮件数量。 Add a save and a close into your loop to avoid getting out of memory. 在循环中添加保存和结束操作,以避免内存不足。

For my version of Excel (2010) the following worked fine reducing memory usage: 对于我的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM