|
楼主 |
发表于 2017-8-15 14:59
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Function GetBoiler(ByVal sFile As String) As String
- Dim fso As Object
- Dim ts As Object
- Set fso = CreateObject("Scripting.FileSystemObject")
- Set ts = fso.GetFile(sFile).OpenAsTextStream(1, -2)
- GetBoiler = ts.readall
- ts.Close
- End Function
- Function IsFileExists(ByVal strFileName As String) As Boolean
- If Dir(strFileName, 16) <> Empty Then
- IsFileExists = True
- Else
- IsFileExists = False
- End If
- End Function
- Sub sendmail()
- '先通过VBA窗口"工具"菜单--"引用"选择 "Microsoft outlook 11.0 object Library,同时已在Outlook中设置可发送邮件的正常帐号
- On Error Resume Next
- Dim rowCount, endRowNo, endColumnsNo
- Dim objOutlook As New Outlook.Application
- Dim objMail As MailItem
- Dim strbody As String
- Dim SigString As String
- Dim Signature As String
- endRowNo = Cells(1, 1).CurrentRegion.Rows.Count '行数计算
- endColumnsNo = Cells(1, 1).CurrentRegion.Columns.Count
- Set objO = New Outlook.Application '创建objO为Outlook应用程序对象
- For rowCount = 2 To endRowNo
- Set objMail = objOutlook.CreateItem(olMailItem)
- strbody = Cells(rowCount, 4)
- SigString = Environ("appdata") & "\Microsoft\Signatures\签名.htm" '获取签名路径
- If Dir(SigString) <> "" Then '判断签名路径存在,存在则获取签名
- Signature = GetBoiler(SigString)
- Else
- Signature = ""
- End If
- With objMail
- .To = Cells(rowCount, 1) '收件人
- .CC = Cells(rowCount, 2) '抄送人
- .Subject = Cells(rowCount, 3) '主题栏
- '.Body = Cells(rowCount, 4) '正文比较简单可以使用这样
- .HTMLBody = strbody & "<br><br>" & Signature '推荐使用HTML格式的正文
-
- a = 0
- For i = 5 To endColumnsNo
- If Sheets("Sheet1").Cells(rowCount, i).Value <> "" Then
- .Display
- .Attachments.Add ThisWorkbook.Path & Cells(rowCount, i).Value
- If IsFileExists(ThisWorkbook.Path & Cells(rowCount, i).Value) = True Then a = a + 1
- End If
- Next i
- If a > 0 Then
- .Send
- Else
- Application.SendKeys "%{F4}n"
- End If
- End With
- Set objMail = Nothing
- Next
- Set objOutlook = Nothing
- End Sub
复制代码
|
|