excel - 如何循环遍历一个表列来过滤另一个表并通过电子邮件发送每个过滤后的表?

标签 excel vba loops

我正在尝试:

  • 使用表 A(列 - 人员姓名)中的值在单独的工作表中过滤表 B
  • 将过滤后的表 B 复制到电子邮件正文(Outlook)
  • 将 Outlook 电子邮件发送到该收件人的电子邮件地址(来自表 A)
  • 再次循环执行该流程,查找表 A 中的下一个人

表 A 示例:
enter image description here

表 B 的示例:
enter image description here

例如,对于第一次迭代

  • 从表 A 中选取 Dave Jones,并在表 B 中筛选出 Dave Jones。
  • 将过滤后的表 B 复制到新电子邮件的正文
  • 发送给戴夫·琼斯 ([email protected])。
  • 返回表 A 查找下一个条目(在本例中为 Anne Smith),并执行相同的操作。重复直到表 A 末尾。

我编写了用于设置电子邮件的代码,但这需要整个工作表并且不进行任何过滤。我无法弄清楚如何将多封电子邮件的循环放在一起:

Sub SendWorkSheet_SENDEMAILS1()
    Dim xFile As String
    Dim xFormat As Long
    Dim Wb As Workbook
    Dim Wb2 As Workbook
    Dim FilePath As String
    Dim FileName As String
    Dim OutlookApp As Object
    Dim OutlookMail As Object

    On Error Resume Next

    Application.ScreenUpdating = False
    Set Wb = Application.ActiveWorkbook
    ActiveSheet.Copy
    Set Wb2 = Application.ActiveWorkbook
    Select Case Wb.FileFormat
    Case xlOpenXMLWorkbook:
        xFile = ".xlsx"
        xFormat = xlOpenXMLWorkbook
    Case xlOpenXMLWorkbookMacroEnabled:
        If Wb2.HasVBProject Then
            xFile = ".xlsm"
            xFormat = xlOpenXMLWorkbookMacroEnabled
        Else
            xFile = ".xlsx"
            xFormat = xlOpenXMLWorkbook
        End If
    Case Excel8:
        xFile = ".xls"
        xFormat = Excel8
    Case xlExcel12:
        xFile = ".xlsb"
        xFormat = xlExcel12
    End Select
    FilePath = Environ$("temp") & "\"
    FileName = Wb.name & Format(Now, "dd-mmm-yy h-mm-ss")
    Set OutlookApp = CreateObject("Outlook.Application")
    Set OutlookMail = OutlookApp.CreateItem(0)
    Wb2.SaveAs FilePath & FileName & xFile, FileFormat:=xFormat
    With OutlookMail
        .to = "EMAIL ADDRESS HERE"
        .CC = ""
        .BCC = ""
        .Subject = "Suppliers"
        .HTMLBody = "Hi all," & "<br>" & "<br>" & "Please find attached etc. etc." & "<br>" & "<br>" & "Kind regards," & "<br>" & "<br>" & "Sender"
        '.Body = ""
            .Attachments.Add Wb2.FullName
        .Display
        '.Send
    End With
    Wb2.Close
    Kill FilePath & FileName & xFile
    Set OutlookMail = Nothing
    Set OutlookApp = Nothing
    Application.ScreenUpdating = True
End Sub

最佳答案

我过去多次需要执行您描述的任务,以下是我提出的解决方案。非常感谢 Sigma Coding https://www.youtube.com/watch?v=ZlInSp0-MdU&ab_channel=SigmaCoding 用于提供大量代码 - 我为自己的特定应用程序添加的循环和过滤器内容。

为了使以下内容正常工作,您需要在 VBA 中启用几个引用。在 VBA 编辑器中,选择“工具/引用”并选中“Microsoft Outlook 16.0 对象库”和“Microsoft Word 16.0 对象库”框。如果尚未选中它们,您会发现它们按字母顺序列出。

以下代码建议假设如下:

• 经理列表位于 Sheet1 上,其包含的范围称为“MyRange”

• 要过滤的表格位于 Sheet2 上,从单元格 A1 开始

此代码适合我 - 让我知道您如何使用它。

Option Explicit
Dim Outlook As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim OutInspect As Outlook.Inspector
Dim EmailTo As String

Dim OutWrdDoc As Word.Document
Dim OutWrdRng As Word.Range
Dim OutWrdTbl As Word.Table

Dim rng As Range, c As Range, MyRange As Range, myFilter As String

Sub TestEmail()

For Each c In Sheet1.Range("MyRange")

    myFilter = c.Value
    EmailTo = c.Offset(0, 1).Value
    
    Sheet2.Range("A1:E1").AutoFilter Field:=2, Criteria1:="=" & myFilter
    
'ERROR TRAP 
If EmailTo = "" Or Sheet2.Cells.SpecialCells(xlCellTypeVisible).Rows.Count = 1 Then
    GoTo Missing:
End If

    Set rng = Sheet2.Cells.SpecialCells(xlCellTypeVisible)

On Error Resume Next

Set Outlook = GetObject(, "Outlook.Application")
    
    If Err.Number = 429 Then
    Set Outlook = New Outlook.Application
    End If
    
Set OutMail = Outlook.CreateItem(olMailItem)

With OutMail
            .To = EmailTo
            .Subject = "Suppliers"
            .Body = "Please find attached etc."
                       
            .Display
            
            Set OutInspect = .GetInspector
            Set OutWrdDoc = OutInspect.WordEditor
            
            rng.Copy
            Set OutWrdRng = OutWrdDoc.Application.ActiveDocument.Content
                OutWrdRng.Collapse Direction:=wdCollapseEnd
            
            Set OutWrdRng = OutWrdDoc.Paragraphs.Add
                OutWrdRng.InsertBreak
            
            OutWrdRng.PasteExcelTable Linkedtoexcel:=True, wordformatting:=True, RTF:=True
            
            Set OutWrdTbl = OutWrdDoc.Tables(1)
            
                OutWrdTbl.AllowAutoFit = True
                OutWrdTbl.AutoFitBehavior (wdAutoFitWindow)
            
            .Send
            
        Application.CutCopyMode = False
        Sheet2.AutoFilterMode = False
        
        End With

Missing:
Next c

End Sub

关于excel - 如何循环遍历一个表列来过滤另一个表并通过电子邮件发送每个过滤后的表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64633369/

相关文章:

vba - 循环VBA后变量值被遗忘

html - 需要帮助从 HTML 获取表格

swift - 循环遍历描述按键后应在屏幕上执行哪些操作的实体

c++ - 使用 "for"打破 "break"循环被认为是有害的?

python - C 到 Python : For loop

vba - Excel计数目标然后在单元格中输出值

vba - 确定何时在 Excel VBA 中插入或删除行/单元格

excel - 当我选择打印全部时,我会看到一个打印对话框。如果我选择取消,它仍然会打印

excel - 使用 ADODB 连接打开存储在 SharePoint 上作为数据源的 Excel 文件

Excel-VBA : Get Value of a Visible Cell in a Table after applying Filter?