【问题标题】:Add Multiple Attachments based on Comma Separated list in a cell根据单元格中的逗号分隔列表添加多个附件
【发布时间】:2018-03-16 14:46:04
【问题描述】:

我需要根据 B 列单元格中的内容附加多个文件。

Sub EmailTest()

Dim OutLookApp As Object
Dim OutLookMailItem As Object
Dim lastRow As Long
Dim MailDest As String
Dim subj As String

lastRow = ThisWorkbook.Worksheets("Sheet1").Cells(Rows.Count,"A").End(xlUp).Row 'change worksheet as needed

For i = 2 To lastRow

    Set OutLookApp = CreateObject("Outlook.application")
    Set OutLookMailItem = OutLookApp.CreateItem(0)
    Set Attach = OutLookMailItem.Attachments

    With OutLookMailItem
        .To = Cells(i, 1).Value
        .Subject = "Test"
        .Body = "Testing an email macro, just delete me"
        On Error Resume Next
        Attach.Add [F2] & Cells(i, 2).Value & ".xlsx" 'change cell reference in brackets as needed
        .Display
        On Error GoTo 0
        '.Send
    End With

Next

End Sub

我找到了下面的链接,但我做了一些不成功的尝试。

Adding multiple attachments to a single email using outlook VBA

在 B 列中,我想输入 Book2、Book3 和代码以了解这是两个不同的附件。可能会有更多附件,这就是为什么我想使用 Attachments = Split 的一些变体。

在单元格 F2 中,我有一个连接公式,允许用户在不了解 VBA 的情况下更改代码引用的文件路径。

【问题讨论】:

  • 您是否有一个单元格包含附件列表作为逗号分隔列表?这是您需要获取(或创建)然后拆分并循环添加为附件的内容。
  • 您只想发送一封带有多个附件的电子邮件吗?好像没有。

标签: excel vba


【解决方案1】:

您必须再使用一个循环来遍历单元格中的所有名称,为了获得这些名称的数组,我使用了Spli() 方法。

'declare variable att the beginning
Dim att as Variant
'loop, also you have to enter separator here, I used ","
For Each att in Split(Cells(i, 2).Value, ",")
    Attach.Add [F2] & att & ".xlsx" 'change cell reference in brackets as needed
Next

【讨论】:

  • 假设有逗号分隔的列表
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-21
  • 1970-01-01
  • 1970-01-01
  • 2016-03-30
  • 2011-03-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多