【发布时间】:2013-12-25 20:23:57
【问题描述】:
我使用 Ron de Bruin 的脚本发送电子邮件,它可以选择从 B 列值发送电子邮件地址。
我从 B 列到 E 列至少有 4 列电子邮件地址。如何修改它以发送这封电子邮件?
例子:
在 Sheets("Sheet1") 中使用:
- 在 A 列中:人名
- 在 B 列中:电子邮件地址
- 在 C:Z 列中:文件名如下:
C:\Data\Book2.xls(不必是 Excel 文件)
宏将循环遍历Sheet1 中的每一行,如果 B 列中有电子邮件地址,C:Z 列中有文件名,它将创建包含此信息的邮件并发送。
Sub Send_Files()
'Working in Excel 2000-2013
'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
【问题讨论】:
-
标题有点误导,第二行的信息也不匹配。你说你有来自
B:E的电子邮件地址,但你也有来自C:Z的文件名。假设您在B中只有电子邮件地址并且您要附加的文件在C:Z中是否安全? -
对不起,我没有修改脚本就粘贴了,实际上电子邮件地址在 col B:E 中,附件仅在 col F 中。
-
感谢您澄清这一点。那么,它的工作方式是,如果
F有附件并且B:E的ALL 有收件人,则发送给所有这些收件人? -
根据您的规格进行了更改。请参阅下面的编辑代码。