【问题标题】:Send mail to different recipients via Outlook using a single macro使用单个宏通过 Outlook 将邮件发送给不同的收件人
【发布时间】:2017-01-31 22:55:42
【问题描述】:

我对 VBA 还是很陌生。 我想出了一种方法来发送邮件,从表格中提取内容并使用宏将其发送给所需的收件人。

现在,我需要向多个收件人发送具有不同内容的邮件,所有必需的数据都存在于同一个表中,收件人名称是其中一列。任何帮助将不胜感激。

Private Sub CommandButton1_Click()

Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Dim StrBody As String
Dim LastRow As Long

StrBody = "Hi," & "<br>" & "<br>" & _
"The following Talents were last reporting to you and have now moved to bench. Please confirm the plans. " & "<br><br>"

With Worksheets("To-Bench")
LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
End With

Set rng = Nothing
On Error Resume Next
'For Only the visible cells in the selection
'Set rng = Selection.SpecialCells(xlCellTypeVisible)
'For fixed range 

Set rng = Sheets("To-Bench").Range("A1:G2").SpecialCells(xlCellTypeVisible)
'Hardcoded the number of rows which is actually indefinite'
  On Error GoTo 0

If rng Is Nothing Then
    MsgBox "The selection is not a range or the sheet is protected" & _
           vbNewLine & "please correct and try again.", vbOKOnly
    Exit Sub
End If

With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With

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

On Error Resume Next



With OutMail

    'Application.Goto ActiveWorkbook.Sheets("Sheet2").Cells(6, 5)
    .To = ActiveSheet.Cells(2, 9).Text   'I've hardcoded the recipient as of now'
    .CC = ""
    .BCC = ""
    .Subject = "Movement of " & Range("C2").Value & " Talents to Bench"
    .HTMLBody = StrBody & rangetoHTML(rng)
    .Display
End With
On Error GoTo 0

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

Set OutMail = Nothing
Set OutApp = Nothing
End Sub

Function rangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2013
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

【问题讨论】:

  • 一些附加说明,数据将从多行具有相同收件人的表中提取。还有没有办法将收件人对应的所有数据合并起来,在一个邮件中发送?
  • 你能分享电子表格的图片吗?
  • 将包含不同内容的邮件发送给多个收件人意味着您正在向多个收件人发送不同的电子邮件正文(他们可以知道列表中的其他人)?您可以使用收件人或密件抄送。您尝试实现的目标是可行的,但在帖子中缺少关键逻辑。我猜您将使用循环来遍历内容的行,然后为收件人使用不同的循环?
  • @PatricK 多个收件人,如多个电子邮件的单个收件人,由单个宏生成。
  • @0m3r 完成。此处,如果两行具有相同的“PM”(收件人),则应将它们的数据合并并在单个邮件中发送。

标签: vba excel outlook


【解决方案1】:

试试 Ron deBruin 的这个解决方案。

在 A 列中:人名 在 B 栏中:电子邮件地址 在 C:Z 列中:像这样的文件名 C:\Data\Book2.xls(不必是 Excel 文件)

宏将遍历“Sheet1”中的每一行,如果 B 列中有电子邮件地址 和 C:Z 列中的文件名,它将使用此信息创建一封邮件并发送。

Sub Send_Files()
'Working in Excel 2000-2016
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
    Dim OutApp As Object
    Dim OutMail As Object
    Dim sh As Worksheet
    Dim cell As Range
    Dim FileCell As Range
    Dim rng As Range

    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With

    Set sh = Sheets("Sheet1")

    Set OutApp = CreateObject("Outlook.Application")

    For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)

        'Enter the path/file names in the C:Z column in each row
        Set rng = sh.Cells(cell.Row, 1).Range("C1:Z1")

        If cell.Value Like "?*@?*.?*" And _
           Application.WorksheetFunction.CountA(rng) > 0 Then
            Set OutMail = OutApp.CreateItem(0)

            With OutMail
                .to = cell.Value
                .Subject = "Testfile"
                .Body = "Hi " & cell.Offset(0, -1).Value

                For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
                    If Trim(FileCell) <> "" Then
                        If Dir(FileCell.Value) <> "" Then
                            .Attachments.Add FileCell.Value
                        End If
                    End If
                Next FileCell

                .Send  'Or use .Display
            End With

            Set OutMail = Nothing
        End If
    Next cell

    Set OutApp = Nothing
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
End Sub

如果您需要将多个单元格合并为一个单元格,您可以使用以下方法连接一个范围。

Function ConcRange(ByRef myRange As Range, Optional ByVal Seperator As String = ";")

ConcRange = vbNullString

Dim rngCell As Range

For Each rngCell In myRange
    If ConcRange = vbNullString Then
        If Not rngCell.Value = vbNullString Then
            ConcRange = CStr(rngCell.Value)
        End If
    Else
        If Not rngCell.Value = vbNullString Then
            ConcRange = ConcRange & Seperator & CStr(rngCell.Value)
        End If
    End If
Next rngCell


End Function

【讨论】:

  • 我的邮件收件人可以多行通用,我需要将所有对应的数据合并到一个唯一的收件人到一封邮件中。
猜你喜欢
  • 2016-01-06
  • 1970-01-01
  • 1970-01-01
  • 2018-11-19
  • 1970-01-01
  • 2023-03-17
  • 2017-01-10
  • 2021-06-27
  • 1970-01-01
相关资源
最近更新 更多