如何从 excel 中具有任何值(文件路径)的列中添加多个附件

How to add multiple attachments from a column from excel that has any value (file path) in it

我正在尝试创建一个 VBA 代码,它将能够告诉 excel 从具有任何值(文件路径)的特定列添加多个附件。

我的 Excel 列 BD2:BD2000 仅包含电子邮件附件列表,我希望 excel 到 select 单元格 BD2:BD20000 中的所有值(如果它们不为空,和哪些基本上是文件路径)并将其作为多个附件附加到我的 outlook 电子邮件中。

但是,我无法让它工作。

这是我目前拥有的:

Private Sub ToggleButton3_Click()

 Dim OutApp As Object
    Dim OutMail As Object

    Set emailRng = Worksheets("Workings").Range("BC2:BC2000")
    Set FilepathRng = Worksheets("Workings").Range("BD2:BD2000")


    For Each cl In emailRng
        sTo = sTo & ";" & cl.Value

    Next

    sTo = Mid(sTo, 2)

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    Next

    sTo = Mid(sTo, 2)


    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next
    With OutMail
        .To = ComboBox17.Value
        .CC = sTo
        .BCC = ""
        .Subject = TextBox18.Value
        .Body = "Hi there"
        .Attachments.add = FilepathRng


    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing


End Sub

如果有人愿意提供帮助,我们将不胜感激。我是 VBA 的新手,我正在尝试为此找到简单的解决方案,所以我将不胜感激 understand/advice/solutions 任何人都可以提供的任何详细和简单的解决方案。

谢谢!

此代码将附加 "BC2:BC2000" 范围内的所有文件路径。

警告:

在一封电子邮件中添加那么多文件不是​​一个好方法。您的系统可能会挂起。要添加 2000 个文件,请相应地更改 For 循环中的范围。

Private Sub ToggleButton3_Click()

    Dim OutApp As Object
    Dim OutMail As Object
    Dim cl As Range
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    Set emailRng = Worksheets("Workings").Range("BC2:BC2000")

    For Each cl In emailRng
        sTo = sTo & ";" & cl.Value

    Next

    sTo = Mid(sTo, 2)

    With OutMail
        .To = ComboBox17.Value
        .CC = sTo
        .BCC = ""
        .Subject = TextBox18.Value
        .Body = "Hi there"

        For Each cl In Worksheets("Workings").Range("BD2:BD2000")
            If Len(cl.Value) > 1 Then
            .Attachments.Add cl.Value
            End If
        Next

        .Display

    End With


    Set OutMail = Nothing
    Set OutApp = Nothing


End Sub

首先你需要用你的路径填充一个数组,例如:

With ThisWorkbook.Sheets("Workings")
    LastRow = .Cells(.Rows.Count, "BD").End(xlUp).Row
    ReDim myFileList(1 To LastRow - 2)
    i = 1
    For Each C In .Range("BD2:BD" & LastRow)
        myFileList(i) = C
        i = i + 1
    Next C
End With

然后添加你需要做的附件:

    For i = LBound(myFileList) To UBound(myFileList)
        .Attachments.Add myFileList(i)
    Next i