You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VBA实现从Excel读取多附件路径批量添加至Outlook邮件

VBA批量发邮件自动匹配客户附件实现方法

前置结构约定

先确认存放附件路径的工作表满足以下基础结构(可根据实际情况调整代码对应参数):

  • 工作表默认名称:Anexos
  • A列:客户名称,需和主表、Mailinfo表中的客户命名完全一致,作为匹配依据
  • B列:单个附件的完整绝对路径,同个客户有N个附件就占N行,每行对应1个路径

核心修改逻辑

在原有代码基础上做3处调整即可实现需求:

  • 新增附件匹配相关的变量声明
  • 每封邮件配置完收件人、正文后,遍历附件表匹配当前客户的所有有效路径
  • 增加文件存在性校验,避免路径错误导致整个宏运行中断

修改后完整可运行代码

Sub Controle_de_orçamentos()

response = MsgBox("Deseja enviar as cobranças?", vbYesNo)
 
If response = vbNo Then
    MsgBox ("Então tchau")
    Exit Sub
End If


    Dim OutApp As Object
    Dim OutMail As Object
    Dim rng As Range
    Dim Ash As Worksheet
    Dim Cws As Worksheet
    Dim Rcount As Long
    Dim Rnum As Long
    Dim FilterRange As Range
    Dim FieldNum As Integer
    Dim mailAddress As String
    ' 新增附件处理相关变量
    Dim attachWs As Worksheet
    Dim attachLastRow As Long
    Dim attachRow As Long
    Dim currentClient As String
    Dim attachPath As String

    On Error GoTo cleanup
    Set OutApp = CreateObject("Outlook.Application")
    ' 绑定附件路径存储表,可修改为实际表名
    Set attachWs = ThisWorkbook.Worksheets("Anexos")

    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    
    
    'Set filter sheet, you can also use Sheets("MySheet")
    Set Ash = ActiveSheet

    'Set filter range and filter column (Column with names)
    Set FilterRange = Ash.Range("A1:H" & Ash.Rows.Count)
    FieldNum = 1    'Filter column = A because the filter range start in A

    'Add a worksheet for the unique list and copy the unique list in A1
    Set Cws = Worksheets.Add
    FilterRange.Columns(FieldNum).AdvancedFilter _
            Action:=xlFilterCopy, _
            CopyToRange:=Cws.Range("A1"), _
            CriteriaRange:="", Unique:=True

    'Count of the unique values + the header cell
    Rcount = Application.WorksheetFunction.CountA(Cws.Columns(1))
    
                                              
    'If there are unique values start the loop
    If Rcount >= 2 Then
        For Rnum = 2 To Rcount
            ' 记录当前循环处理的客户名称
            currentClient = Cws.Cells(Rnum, 1).Value
            'Filter the FilterRange on the FieldNum column
            FilterRange.AutoFilter Field:=FieldNum, _
                                   Criteria1:=currentClient

            'Look for the mail address in the MailInfo worksheet
            mailAddress = ""
            On Error Resume Next
            mailAddress = Application.WorksheetFunction. _
                          VLookup(currentClient, _
                                Worksheets("Mailinfo").Range("A1:B" & _
                                Worksheets("Mailinfo").Rows.Count), 2, False)
                        On Error GoTo 0

            If mailAddress <> "" Then
                With Ash.AutoFilter.Range
                    On Error Resume Next
                    Set rng = .SpecialCells(xlCellTypeVisible)
                    On Error GoTo 0
                
                End With
                
                Set OutMail = OutApp.CreateItem(0)

   
                On Error Resume Next
                With OutMail
                    .To = mailAddress
                    .Subject = "Orçamentos aguardando aprovação - Indi Empilhadeiras"
                    .HTMLBody = "Prezados(as), boa tarde!<br>" & _
                    "Poderiam, por gentileza, informar se os orçamentos abaixo estão aprovados?" & RangetoHTML(rng) & _
                    "<br>Obrigado!<br>" & _
                    "Denis Scalco<br>" & _
                    "(15) 98145-0856"
                    
                    ' 遍历附件表匹配当前客户所有有效附件
                    attachLastRow = attachWs.Cells(attachWs.Rows.Count, "A").End(xlUp).Row
                    For attachRow = 2 To attachLastRow ' 默认第1行为表头,从第2行开始读数据
                        If attachWs.Cells(attachRow, "A").Value = currentClient Then
                            attachPath = attachWs.Cells(attachRow, "B").Value
                            ' 校验文件存在才添加,避免路径错误中断运行
                            If Dir(attachPath) <> "" Then
                                .Attachments.Add attachPath
                            End If
                        End If
                    Next attachRow

                    .Display  'Or use Send
                    .Send
                  
                End With
                On Error GoTo 0

                Set OutMail = Nothing
            End If

            'Close AutoFilter
            Ash.AutoFilterMode = False
            
        Next Rnum
        
    End If

cleanup:
    Set OutApp = Nothing
    Set attachWs = Nothing
    Application.DisplayAlerts = False
    Cws.Delete
    Application.DisplayAlerts = True

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    
    End With
End Sub

可调参数说明

  • 附件路径存储表名和代码中不一致的,直接修改Set attachWs = ThisWorkbook.Worksheets("Anexos")引号内的表名即可
  • 附件表中客户名、附件路径所在列有调整的,对应修改attachWs.Cells(attachRow, "A")、attachWs.Cells(attachRow, "B")里的列标即可
  • 附件表无表头的,将附件遍历循环的起始值attachRow = 2改为attachRow = 1

注意:附件路径必须填写完整绝对路径(含盘符、文件夹层级、完整文件名+后缀),无效路径对应的文件会被自动跳过,不会中断宏运行。

内容的提问来源于stack exchange,提问作者Adilson Lolatto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.30 23:39:34