excel - 发送带附件的电子邮件的 VBA 循环还包括所有以前迭代的附件

标签 excel vba email attachment

我需要在 Excel 2007 中发送一封电子邮件,邮件正文中包含工作簿中的一系列单元格,并且每个收件人都有不同的附件。

我在使用下面的代码时遇到困难。除了添加附件外,一切都按预期工作。当我开始循环发送带有各自附件的电子邮件时,它包括所有先前迭代的附件。也就是说邮件是这样发送的:

电子邮件 1 - 附件 1

电子邮件 2 - 附件 1、附件 2

电子邮件 3 - 附件 1、附件 2、附件 3;等等。

Sub Send_Range()
Dim x As Integer
Dim i As Integer
x = Sheets("MarketMacro").Range("M1").Text 'A count of how many emails to send.
i = 2
  Do
   ' Select the range of cells on the active worksheet.
   Sheets("Summary").Range("A1:M77").Select
   ' Show the envelope on the ActiveWorkbook.
   ActiveWorkbook.EnvelopeVisible = True

   With ActiveSheet.MailEnvelope
      .Introduction = "This is a sample worksheet."
      .Item.To = Sheets("MarketMacro").Range("A" & i).Text
      .Item.Subject = "Test" 'email subject
      .Item.attachments.Add (Sheets("MarketMacro").Range("H" & i).Text) 'add attachment based on path in worksheet cell
      .Item.Send 'sends without displaying the email
   End With
   i = i + 1 
Loop Until i = x + 2
    MsgBox ("The tool sent " & i - 2 & " reports.")
End Sub

有没有人能解决这个问题?我有另一种方法以编程方式发送带有附件的电子邮件,效果非常好,但我无法发送一系列单元格作为电子邮件的正文。

最佳答案

试试这个:

Sub Send_Range()
Dim x As Integer
Dim i As Integer

x = Sheets("MarketMacro").Range("M1").Text 'A count of how many emails to send.
i = 2

Do
   ' Select the range of cells on the active worksheet.
   Sheets("Summary").Range("A1:M77").Select
   ' Show the envelope on the ActiveWorkbook.
   ActiveWorkbook.EnvelopeVisible = True

   With ActiveSheet.MailEnvelope
      'Before we send emails, we will loop through the Attachments collection
      'and delete any that are in there already.
      'There seemed to be an issue with the For...Each construct which
      'would not delete all the attachments.  This is the only way I could
      'do it.
      Do Until .Item.attachments.Count = 0
          .Item.attachments(1).Delete
      Loop

      .Introduction = "This is a sample worksheet."
      .Item.To = Sheets("MarketMacro").Range("A" & i).Text
      .Item.Subject = "Test" 'email subject
      .Item.attachments.Add (Sheets("MarketMacro").Range("H" & i).Text) 'add attachment based on path in worksheet cell
      .Item.Send 'sends without displaying the email
   End With
   i = i + 1 
Loop Until i = x + 2
    MsgBox ("The tool sent " & i - 2 & " reports.")
End Sub

我相信代码只是重复使用相同的 MailEnvelope 对象,每次进入 Do...Until 循环时都会覆盖每个属性。但是由于 Attachments 是一个集合而不是一个标量,所以您每次执行循环时都会附加一个额外的项目。我在那个外循环中添加了一个小循环,它将搜索 .Item.Attachments 并在 .Attachments.Count 大于 0 时删除每个附件。这样,在发送邮件。

编辑: 我的 MailEnvelope 对象总是会在我发送第一封邮件后抛出异常(-2147467259:自动化错误。未指定的错误)。不确定您是否看到了这个(似乎没有)。我以前没有玩过这个对象,也不知道它是如何自动化 Outlook 的,所以我真的帮不上忙。希望你不会看到它。

关于excel - 发送带附件的电子邮件的 VBA 循环还包括所有以前迭代的附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24372824/

相关文章:

PHP 群发邮件 (25K-100K) 列表

mySQL + vba,检查表中是否已存在某个项目

vba - 使用VBA打开网站并点击视频

vba - Excel/PowerPoint VBA 和后期绑定(bind)

vba - 在一个工作表中查找一个值,对其进行计数并将结果粘贴到另一个工作表中

保存附件时排除图像 png 和 gif 的 VBA 代码

c# - 图片不出现在电子邮件中

email - Powershell 发送带图片的电子邮件

r - 如何在 R 中读取带有大量数字(可能是科学记数法)的 csv?

excel - 索引匹配 : String in cell to mappings