我有两张纸。一个包含针对其交付管理器的项目和客户名称,另一页包含针对其名称的交付管理器电子邮件地址列表。对于某些帐户,有两个交付经理。
我想使用Excel VBA根据他们的项目和客户名称列将邮件发送给交付经理。
我尝试过将邮件发送给传递经理,但是它发送到单个条目,而不是从列表中选择所有传递经理。我要筛选“客户”和“项目”,然后从列中选择交付管理器,并将他们从第二张工作表中的邮寄地址添加到邮寄列表中。
Sub Mail_Selection_Range_Outlook_Body()
Dim rng As Range
Dim OutApp As Object 'Dim OutApp As Outlook.Application
Dim OutMail As Object 'Dim OutMail As Outlook.MailItem
Dim ws1 As Worksheet, ws2 As Worksheet
Dim body1 As String, body2 As String, mail_Message As String, mail_Subject As String, mail_from As String, mail_on_behfalfof As String
Dim last_row, last_row2 As Long
Dim last_col, last_col2 As Integer
Dim I As Integer
I = 1
Set rng = Nothing
mail_Message = ""
mail_Message_end = " "
mail_Subject = " "
mail_from = " "
mail_on_behalfof = ""
Set ws1 = ThisWorkbook.Worksheets("sheet1")
Set ws2 = ThisWorkbook.Worksheets("sheet2")
If ws1.FilterMode Then
ActiveSheet.ShowAllData
End If
last_row = ws1.Cells(ws1.Rows.Count, 1).End(xlUp).Row
last_row2 = ws2.Cells(ws2.Rows.Count, 1).End(xlUp).Row
last_col = ws1.Cells(1, ws1.Columns.Count).End(xlToLeft).Column
last_col2 = ws2.Cells(1, ws1.Columns.Count).End(xlToLeft).Column
ws1.Range(Cells(1, 1), Cells(1, last_col)).AutoFilter
For I = 1 To last_row2 - 1
body1 = "<P STYLE='font-family:Calibri (Body);font-size:14.5'>" & "Hi " & ws2.Range("A1").Offset(I, 1).Value & "," & "<br>" & "<br>" & mail_Message & "<br>" & "</p>"
body2 = "<P STYLE='font-family:Calibri (Body);font-size:14.5'>" & "<br>" & mail_Message_end & "<br>" & "Regards," & "<br>" & mail_from & "</p>"
ws1.AutoFilterMode = False
ws1.Range(Cells(1, 1), Cells(1, last_col)).AutoFilter Field:=1, Criteria1:=ws2.Range("A1").Offset(I, 0).Value
Set rng = ws1.Range(Cells(1, 1), Cells(last_row, last_col)).SpecialCells(xlCellTypeVisible)
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)
'Set OutMail = OutApp.CreateItem(olMailItem)
Dim Ldate As Date
On Error Resume Next
With OutMail
.SentOnBehalfOfName = mail_on_behfalfof
.To = ws2.Range("A1").Offset(I, 2).Value
.CC = ""
.BCC = ""
.Subject = mail_Subject
.HTMLBody = body1 & RangetoHTML(rng) & body2
.SendUsingAccount = OutApp.Session.Accounts.Item(2)
.Display 'use .Send or .Display for testing
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
Next I
End Sub
Function RangetoHTML(rng As Range)
' Changed 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
.Cells(1).EntireRow.AutoFit
.Cells(1).EntireColumn.AutoFit
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With
TempWB.Sheets(1).UsedRange.Columns.AutoFit
'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
它应该根据客户和项目将邮件发送给交付经理。如果有两个交付经理,则应将两者都放入“列表”中。