首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >内存外通过excel vba生成电子邮件

内存外通过excel vba生成电子邮件
EN

Stack Overflow用户
提问于 2017-09-26 12:17:52
回答 2查看 704关注 0票数 0

我有一些VBA代码,我从excel中运行,它根据主题名称生成电子邮件并将excel文件附加到电子邮件中。宏似乎运行良好的101封电子邮件,然后失败,几乎100%的时间。每个附件是15 to,总数量的电子邮件创建将有所不同,但测试,我有128个总数。

实际的电子邮件组合是电子邮件的主体,附加了默认签名,主题是静态的和可变的。

我无法识别代码所需的任何修改,每次迭代时我都会用到OAMail项,所以我有点失去了(这似乎是个错误的标准问题)。

代码如下:

代码语言:javascript
复制
Sub Generate_Emails()

    Dim OApp As Object
    Dim OMail As Object
    Dim signature As String
    Dim emailbody As String
    Dim ET As Worksheet
    Dim Sum_WS As Worksheet
    Dim EL As Worksheet
    Dim CS As Worksheet

    Set ET = ActiveWorkbook.Worksheets("EmailTemplate")
    Set Sum_WS = ActiveWorkbook.Worksheets("Summary")
    Set EL = ActiveWorkbook.Worksheets("EmailList")
    Set CS = ActiveWorkbook.Worksheets("ControlSheet")
    Set OApp = CreateObject("Outlook.Application")

    'Check if emails can be generated
    If CS.Range("F2") = "No" Then
        MsgBox "Cannot generate files until Files have been generated", vbExclamation
        Exit Sub
    Else
        i = Application.WorksheetFunction.CountA(EL.Range("A:A"))
        body = ET.Range("A1")

        'Go through each email in email list
        For j = 2 To i
            'Create email object
            Set OMail = OApp.CreateItem(0)
            'Get default signature
            With OMail
                .GetInspector
            End With
            'Allocate signature and body
            signature = OMail.HTMLBody
            'Create the whole email and add attachment
            With OMail
                .To = EL.Cells(j, 2)
                .Subject = "SOx RemTP Audit " & Sum_WS.Range("C2")
                .HTMLBody = body & vbNewLine & signature
                .Attachments.Add Sum_WS.Range("B2") & "\SOx RemTP Audit " & Sum_WS.Range("C2") & " - " & EL.Cells(j, 1) & ".xlsx"
                .Save
            End With

            Application.StatusBar = "Generating Email " & j & " of " & i
            DoEvents

            Set OMail = Nothing
        Next j
        Application.StatusBar = False
    End If
    Set OApp = Nothing
    MsgBox "All emails placed into Outlook draft folder", vbInformation
End Sub

如能提供任何协助,将不胜感激。

干杯

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-09-26 13:51:04

注意:您的代码看起来很好,但是使用Option Explicit

请参阅以'##…开头的代码中的注释

代码语言:javascript
复制
Option Explicit '## force proper variable declare to avoid typos and issues

Public Sub Generate_Emails()
    Dim OApp As Object
    Dim OMail As Object
    Dim signature As String
    Dim emailbody As String
    Dim ET As Worksheet
    Dim Sum_WS As Worksheet
    Dim EL As Worksheet
    Dim CS As Worksheet

    Set ET = ActiveWorkbook.Worksheets("EmailTemplate")
    Set Sum_WS = ActiveWorkbook.Worksheets("Summary")
    Set EL = ActiveWorkbook.Worksheets("EmailList")
    Set CS = ActiveWorkbook.Worksheets("ControlSheet")
    Set OApp = CreateObject("Outlook.Application")

    'Check if emails can be generated
    If CS.Range("F2") = "No" Then
        MsgBox "Cannot generate files until Files have been generated", vbExclamation
        Exit Sub
    Else
        Dim i As Long '## dim i
        i = Application.WorksheetFunction.CountA(EL.Range("A:A"))
        emailbody = ET.Range("A1")

        'Go through each email in email list
        Dim j As Long '## dim j
        For j = 2 To i
            'Create email object
            Set OMail = OApp.CreateItem(0)
            'Get default signature
            With OMail '## tidied up your with block (one is enough)
                .GetInspector

                'Allocate signature and body
                signature = .HTMLBody

                'Create the whole email and add attachment
                .To = EL.Cells(j, 2)
                .Subject = "SOx RemTP Audit " & Sum_WS.Range("C2")
                .HTMLBody = emailbody & vbNewLine & signature
                .Attachments.Add Sum_WS.Range("B2") & "\SOx RemTP Audit " & Sum_WS.Range("C2") & " - " & EL.Cells(j, 1) & ".xlsx"
                .Save
                .Close 0 '## close the mail to not leave it open (this might be the issue)
                         '0=olSave; 1=olDiscard; 2=olPromptForSave
            End With

            Application.StatusBar = "Generating Email " & j & " of " & i
            DoEvents

            'Set OMail = Nothing '## not needed
        Next j
        Application.StatusBar = False
    End If
    'Set OApp = Nothing '## not needed

    MsgBox "All emails placed into Outlook draft folder", vbInformation
End Sub

Set Something = Nothing几乎不需要,因为VBA在End Sub上自动执行此操作。

票数 0
EN

Stack Overflow用户

发布于 2017-09-26 14:32:08

通过在With语句中添加".close 0“来解决问题。

原始循环:

代码语言:javascript
复制
'Go through each email in email list
        For j = 2 To i
            'Create email object
            Set OMail = OApp.CreateItem(0)
            'Get default signature
            With OMail
                .GetInspector
            End With
            'Allocate signature and body
            signature = OMail.HTMLBody
            'Create the whole email and add attachment
            With OMail
                .To = EL.Cells(j, 2)
                .Subject = "SOx RemTP Audit " & Sum_WS.Range("C2")
                .HTMLBody = body & vbNewLine & signature
                .Attachments.Add Sum_WS.Range("B2") & "\SOx RemTP Audit " & Sum_WS.Range("C2") & " - " & EL.Cells(j, 1) & ".xlsx"
                .Save
            End With

            Application.StatusBar = "Generating Email " & j & " of " & i
            DoEvents

            Set OMail = Nothing
        Next j

修改后的循环,在添加.close 0并与以下内容合并后:

代码语言:javascript
复制
    'Go through each email in email list
    For j = 2 To i
        'Create email object
        Set OMail = OApp.CreateItem(0)
        'Get default signature
        With OMail
            .GetInspector
            'Allocate signature
            signature = OMail.HTMLBody
            'Create the whole email and add attachment
            .To = EL.Cells(j, 2)
            .Subject = emailsubject
            .HTMLBody = emailbody & vbNewLine & signature
            .Attachments.Add attachmentsfolder & EL.Cells(j, 1) & ".xlsx"
            .Save
            .Close 0
        End With


        Application.StatusBar = "Generating Email " & j & " of " & i
        DoEvents

        Set OMail = Nothing
    Next j

感谢Peh和Sam提供的解决方案

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46426347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档