Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 使用单个宏通过Outlook向不同的收件人发送邮件_Vba_Excel_Outlook - Fatal编程技术网

Vba 使用单个宏通过Outlook向不同的收件人发送邮件

Vba 使用单个宏通过Outlook向不同的收件人发送邮件,vba,excel,outlook,Vba,Excel,Outlook,我对VBA相当陌生。 我找到了一种方法,可以发送一封邮件,从表中提取内容,然后使用宏将其发送给所需的收件人 现在,我需要向多个收件人发送不同内容的邮件,所有必需的数据都显示在同一个表中,收件人名称是其中一列。任何帮助都将不胜感激 Private Sub CommandButton1_Click() Dim rng As Range Dim OutApp As Object Dim OutMail As Object Dim StrBody As String Dim LastRow As Lo

我对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
Private子命令按钮1\u单击()
变暗rng As范围
Dim OutApp作为对象
将邮件变暗为对象
像弦一样暗的链子
最后一排一样长
StrBody=“嗨,&”
“&”
“&”_ “以下人才上一次向您报告,现在已移到替补席。请确认计划。”&“

” 带工作表(“工作台”) LastRow=.Cells(Rows.Count,“A”).End(xlUp).Row 以 设置rng=无 出错时继续下一步 '仅适用于选定区域中的可见单元格 'Set rng=Selection.SpecialCells(xlCellTypeVisible) “对于固定范围 设置rng=板材(“至工作台”)。范围(“A1:G2”)。特殊单元(xlCellTypeVisible) '硬编码实际不确定的行数' 错误转到0 如果rng不算什么,那么 MsgBox“所选内容不是范围或工作表受保护”&_ vbNewLine&“请更正并重试。”,vbOKOnly 出口接头 如果结束 应用 .EnableEvents=False .ScreenUpdate=False 以 Set-OutApp=CreateObject(“Outlook.Application”) Set-OutMail=OutApp.CreateItem(0) 出错时继续下一步 发邮件 'Application.Goto ActiveWorkbook.Sheets(“Sheet2”).单元格(6,5) .To=ActiveSheet.Cells(2,9)。文本“我已经硬编码了收件人” .CC=“” .BCC=“” .Subject=“移动”和范围(“C2”).价值和“替补人才” .HTMLBody=StrBody和rangetoHTML(rng) .展示 以 错误转到0 应用 .EnableEvents=True .ScreenUpdate=True 以 发送邮件=无 设置应用程序=无 端接头 函数rangetoHTML(rng作为范围) “由Ron de Bruin于2006年10月28日更改 “2000-2013年在办公室工作 作为对象的Dim fso 将T作为对象 将文件设置为字符串 将TempWB设置为工作簿 TempFile=Environ$(“temp”)和“\”格式(现在是“dd-mm-yy h-mm-ss”)和“.htm” '复制范围并创建一个新工作簿,以超过中的数据 收到 Set TempWB=工作簿。添加(1) 带临时工作表(1) .单元格(1).粘贴特殊粘贴:=8 .单元格(1).粘贴特殊值,False,False .单元格(1).粘贴特殊xlPasteFormats,False,False .单元格(1)。选择 Application.CutCopyMode=False 出错时继续下一步 .DrawingObjects.Visible=True .DrawingObjects.Delete 错误转到0 以 '将工作表发布到htm文件 使用TempWB.PublishObjects.Add(_ SourceType:=xlSourceRange_ 文件名:=临时文件_ 工作表:=临时工作表(1).名称_ 来源:=TempWB.Sheets(1).UsedRange.Address_ HtmlType:=xlHtmlStatic) .发布(真实) 以 '将htm文件中的所有数据读入RangetoHTML 设置fso=CreateObject(“Scripting.FileSystemObject”) 设置ts=fso.GetFile(TempFile).OpenAsTextStream(1,-2) rangetoHTML=ts.readall 关闭 rangetoHTML=Replace(rangetoHTML,“align=center x:publishsource=”_ “align=left x:publishsource=”) “关闭TempWB TempWB.Close savechanges:=False '删除此函数中使用的htm文件 杀死临时文件 设置ts=无 设置fso=无 设置TempWB=Nothing 端函数
试试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

另外,数据将从多行具有相同收件人的表中提取。是否还有一种方法可以组合与收件人对应的所有数据并在一封邮件中发送?您能否共享电子表格的图像?向多个收件人发送具有不同内容的邮件,这意味着您正在向多个收件人发送不同的电子邮件正文(他们可以知道列表中的其他收件人)?您可以使用To或BCC。你试图实现的目标是可行的,但在文章中缺少关键的逻辑。我猜您将使用循环来循环内容的行,然后对收件人使用不同的循环?@PatricK多个收件人如中所示,单个收件人用于单个宏生成的多封电子邮件。@0m3r完成。这里,如果两行具有相同的“PM”(收件人),则它们的数据应合并并在一封邮件中发送。我的电子邮件收件人可以是多行的公用收件人,我需要将所有对应的数据合并到一封邮件中。
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