|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 kong12 于 2015-7-12 10:23 编辑
2楼的代码原文见下
- Sub sendemail()
- Dim myOlApp As Object
- Dim myitem As Object
- Dim i As Integer, j As Integer
- Dim strg As String
- Dim atts As Object
- Dim mycc As Object
- Dim myfile As String
- Set myOlApp = CreateObject("Outlook.Application")
-
- With Sheets("Sheet1")
- i = 2
- Do While .Cells(i, 2) <> ""
- Set myitem = myOlApp.CreateItem(0)
- Set atts = myitem.Attachments
- myitem.To = .Cells(i, 2) '收件人E-mail
- myitem.Subject = .Cells(i, 3) '标题
- myitem.Body = .Cells(i, 1) & ",你好!" & vbNewLine & vbNewLine & vbNewLine & .Cells(i, 4) '正文
- myfile = Dir(ThisWorkbook.Path & "\*" & .Cells(i, 1) & "*.*")
- Do Until myfile = ""
- myitem.Attachments.Add ThisWorkbook.Path & "" & myfile, 1
- myfile = Dir
- Loop
- myitem.display '预览,如果想直接发送,把.display改为.send
- i = i + 1
- strg = ""
- Loop
- End With
- Set myitem = Nothing
- End Sub
复制代码 注意:该代码如果对有名字包含的人,比如一个李华一个李华盛这种人名,就会把邮件的附件发重复,李华收到李华和李华盛两个附件。
要把 myfile = Dir(ThisWorkbook.Path & "\*" & .Cells(i, 1) & "*.*") 改成下面的这样才行,
myfile = Dir(ThisWorkbook.Path & "\" & .Cells(i, 2) & ".*")
并且再添加附件时也要加限制, 否则如果没有对应的附件就会出错。
要加一个 Do 循环 或者 一个 If 条件 语句,
修改后的代码见下
- Sub 全自动发送邮件()
- Dim myOlApp As Object
- Dim myitem As Object
- Dim i As Integer, j As Integer
- Dim strg As String
- Dim atts As Object
- Dim mycc As Object
- Dim myfile As String
- Set myOlApp = CreateObject("Outlook.Application")
-
- '设置对Sheet1工作表进行操作,可自行修改
- With Sheets("Sheet1")
- i = 2
- Do While .Cells(i, 2) <> ""
- '设置调用Outlook来发送邮件
- Set myitem = myOlApp.CreateItem(0)
- Set atts = myitem.Attachments
- '收件人邮箱地址调用了第3列邮箱的数据
- myitem.To = .Cells(i, 3)
- '邮件标题调用了第2列姓名、第4列标题的数据
- myitem.Subject = .Cells(i, 2) & "老师," & .Cells(i, 4)
- '邮件正文,调用第2列即B列的姓名和第4列即D列的邮件标题。
- myitem.Body = .Cells(i, 2) & "老师,你好!" & vbNewLine & vbNewLine & vbNewLine & .Cells(i, 4) & ",具体请看附件。" & vbNewLine & vbNewLine & vbNewLine & "祝暑假愉快!"
-
- '在本工作薄的根目录下找出附件,且附件的文件名是收件人的名字
- myfile = Dir(ThisWorkbook.Path & "" & .Cells(i, 2) & ".*")
-
- '下面是个添加一个到多个附件的循环。如果没找到对应人名的附件,则发无附件邮件。
- Do Until myfile = ""
- myitem.Attachments.Add ThisWorkbook.Path & "" & myfile, 1
- myfile = Dir
- Loop
-
- '下面一句适用于只添加一个附件用的,可以替换上面的循环。如果要用请取消掉前面的注释符 '
- 'If myfile <> "" Then myitem.Attachments.Add ThisWorkbook.Path & "" & myfile, 1
-
- '预览,如果想直接发送,把.display改为.send
- myitem.send
- i = i + 1
- strg = ""
- Loop
- End With
-
- Set myitem = Nothing
-
- End Sub
复制代码
以上代码在Excel 2013中执行通过,并且对“通讯录”Excel 和“全自动发送邮件”宏代码稍作修改就可以完成任何形式的大量邮件的个性化带附件群发。
我的联系方式见下。
QQ::851890581
]网站:http://www.eqmap.us/bbs/ (去电脑软件板块 发帖找kong12即可)
|
评分
-
1
查看全部评分
-
|