当前位置: 首页 > 知识库问答 >
问题:

如何使用Excel VBA向范围内的唯一用户发送电子邮件?

鞠自明
2023-03-14

我试图查看A列,找到所有唯一的电子邮件地址,为每个地址创建一个Outlook电子邮件,并用该电子邮件地址所在的行填充该电子邮件的正文(也包括标题)。

示例数据:

+----------------+---------------------+---------+
|     Email      |     Application     | Version |
+----------------+---------------------+---------+
| test1@test.com | Microsoft_Office_13 | v2.0    |
| test1@test.com | Putty               | v3.0    |
| test1@test.com | Notepad             | v5.6    |
| test2@test.com | Microsoft_Office_13 | v2.0    |
| test2@test.com | Putty               | v3.0    |
| test2@test.com | Adobe_Reader        | v6.4    |
| test3@test.com | Microsoft_Office_13 | v3.6    |
| test3@test.com | Paint               | v6.4    |
| test3@test.com | Adobe_Reader        | v6.4    |
+----------------+---------------------+---------+

这是我发现的,但它会为每次列出地址创建一封电子邮件。它也没有任何代码来说明如何将一个单元格范围拉到主体中。

Sub Test1()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim cell As Range

    Application.ScreenUpdating = False
    Set OutApp = CreateObject("Outlook.Application")

    On Error GoTo cleanup
    For Each cell In Columns("A").Cells.SpecialCells(xlCellTypeConstants)
        If cell.Value Like "?*@?*.?*" And _
           LCase(Cells(cell.Row, "C").Value) = "yes" Then

            Set OutMail = OutApp.CreateItem(0)
            On Error Resume Next
            With OutMail
                .To = cell.Value
                .Subject = "Reminder"
                .Body = "Hi, please find your account permissions below:"
                .Display
            End With
            On Error GoTo 0
            Set OutMail = Nothing
        End If
    Next cell

cleanup:
    Set OutApp = Nothing
    Application.ScreenUpdating = True
End Sub
+----------------+---------------------+---------+
|     Email      |     Application     | Version |
+----------------+---------------------+---------+
| test2@test.com | Microsoft_Office_13 | v2.0    |
| test2@test.com | Putty               | v3.0    |
| test2@test.com | Adobe_Reader        | v6.4    |
+----------------+---------------------+---------+

共有1个答案

陈宜修
2023-03-14

你可以用不同的方法做到这一点,但我只是给你一个快速的答案,这将解决你的问题。我使用Ron de Bruin开发的函数将范围转换为html主体。

>

  • 我删除了一个条件来检查A列中单元格的内容,所以请确保将其放回并用自己的数据进行测试

    我使用字典来存储我们生成的outlook实例的电子邮件,所以如果在其他单元格中有相同的电子邮件,您将不会再次生成电子邮件

    Option Explicit
    
    
    Sub Test1()
        Dim OutApp As Object
        Dim OutMail As Object
        Dim dict As Object 'keep the unique list of emails
        Dim cell As Range
        Dim cell2 As Range
        Dim rng As Range
        Dim i As Long
        Dim WS As Worksheet
    
        Application.ScreenUpdating = False
        Set OutApp = CreateObject("Outlook.Application")
        Set dict = CreateObject("scripting.dictionary")
        Set WS = ThisWorkbook.Sheets("Sheet1") 'change the name of the sheet accordingly
    
        On Error GoTo cleanup
        For Each cell In WS.Columns("A").Cells.SpecialCells(xlCellTypeConstants)
            If cell.Value Like "?*@?*.?*" Then
    
                'check if this email address has been used to generate an outlook email or not
                If dict.exists(cell.Value) = False Then
    
                    dict.Add cell.Value, "" 'add the new email address
                    Set OutMail = OutApp.CreateItem(0)
                    Set rng = WS.UsedRange.Rows(1)
    
                    'find all of the rows with the same email and add it to the range
                    For Each cell2 In WS.UsedRange.Columns(1).Cells
                        If cell2.Value = cell.Value Then
                            Set rng = Application.Union(rng, WS.UsedRange.Rows(cell2.Row))
                        End If
                    Next cell2
    
                    On Error Resume Next
                    With OutMail
                        .To = cell.Value
                        .Subject = "Reminder"
                        .HTMLBody = "Hi, please find your account permissions below:" & vbNewLine & vbNewLine & RangetoHTML(rng)
                        .Display
                    End With
    
                    On Error GoTo 0
                    Set OutMail = Nothing
                End If
            End If
        Next cell
    
    cleanup:
        Set OutApp = Nothing
        Application.ScreenUpdating = True
    End Sub
    
    Function RangetoHTML(rng As Range)
    ' coded by Ron de Bruin 28-Oct-2006
    ' Working in Office 2000-2016
        Dim fso As Object
        Dim ts As Object
        Dim TempFile As String
        Dim TempWB As Workbook
    
        TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
    
        'Copy the range and create a new workbook to past the data in
        rng.Copy
        Set TempWB = Workbooks.Add(1)
        With TempWB.Sheets(1)
            .Cells(1).PasteSpecial Paste:=8
            .Cells(1).PasteSpecial xlPasteValues, , False, False
            .Cells(1).PasteSpecial xlPasteFormats, , False, False
            .Cells(1).Select
            Application.CutCopyMode = False
            On Error Resume Next
            .DrawingObjects.Visible = True
            .DrawingObjects.Delete
            On Error GoTo 0
        End With
    
        'Publish the sheet to a htm file
        With TempWB.PublishObjects.Add( _
             SourceType:=xlSourceRange, _
             Filename:=TempFile, _
             Sheet:=TempWB.Sheets(1).Name, _
             Source:=TempWB.Sheets(1).UsedRange.Address, _
             HtmlType:=xlHtmlStatic)
            .Publish (True)
        End With
    
        'Read all data from the htm file into RangetoHTML
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
        RangetoHTML = ts.readall
        ts.Close
        RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                              "align=left x:publishsource=")
    
        'Close TempWB
        TempWB.Close savechanges:=False
    
        'Delete the htm file we used in this function
        Kill TempFile
    
        Set ts = Nothing
        Set fso = Nothing
        Set TempWB = Nothing
    End Function
    

  •  类似资料:
    • 问题内容: 我在网站上使用PHP,并且想添加电子邮件功能。 我已经安装了WAMPSERVER。 如何使用PHP发送电子邮件? 问题答案: 使用PHP的功能是可能的。请记住,邮件功能在本地服务器上不起作用。

    • 伙计们,我在问我如何能发送验证到用户的邮件,换句话说,所有我在谷歌和youtube找到的东西,如何发送到你的电子邮件主机作为邮件陷阱等等。但是没有人解释如何使用用户的电子邮件。您对RQ有想法吗:我正在使用Laravel8和Laravel/UI引导程序

    • 我知道这里已经有很多关于向hotmail发送电子邮件的问题了。我已经通读了它们,以及过去几周的许多在线帖子,但仍然无法解决这个问题。 我遇到的问题是,我无法向拥有hotmail电子邮件地址的客户发送电子邮件。我可以向雅虎发送电子邮件,也可以向gmail发送电子邮件(尽管这些邮件似乎进入了垃圾文件夹),但是当我向hotmail电子邮件地址发送电子邮件时,它们似乎永远不会到达。 我在PHP Symfo

    • 我正在使用Laravel 7,我想使用sendemail驱动程序和Laravel Mail Facade发送电子邮件,因为当我使用php邮件功能时,它可以工作,但我想使用Laravel Mail Facade。 这是我的. env文件电子邮件配置 这是我在config/mail中的默认邮件。php 什么是正确的配置?如何使其工作?

    • 我正在开发PHP应用程序,注册后,使用Amazon SES(简单电子邮件服务)向注册的电子邮件地址发送激活邮件。 但是SES只向经过验证的用户发送电子邮件。 如何将激活链接发送到任何未经验证的电子邮件地址?

    • 问题内容: 如何使用curl命令行程序从gmail帐户发送电子邮件? 我尝试了以下方法: 使用file.txt作为电子邮件的内容,但是,当我运行此命令时,出现以下错误: 是否可以从仍由curl托管的个人服务器托管帐户发送电子邮件?这样会使身份验证过程更容易吗? 问题答案: curl –url 'smtps://smtp.gmail.com:465’ –ssl-reqd \ –mail-from '