Sub Mail_Outlook()
Dim OutApp As Object
Dim OutMail As Object
Dim subject As Object
Dim cellRange As range
Dim sh As Worksheet
Dim cell As range
Dim FileCell As range
Dim rngBody As range
Dim i As Integer
LastRow = ActiveSheet.range("A" & Rows.Count).End(xlUp).Row
If Cells(LastRow, 1).Value <> "" Then
MailTo = ActiveWorkbook.Worksheets("Contatos").Cells(LastRow, 1).Offset(0, 1).Value
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set sh = Sheets("Contatos")
Set OutApp = CreateObject("Outlook.Application")
i = 0
' loop para criar emails separados
For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)
'Enter the path/file names in the C:Z column in each row
Set cellRange = sh.Cells(cell.Row, 1).range("C1:Z1")
MailSubject = ActiveWorkbook.Worksheets("Email").Cells(2, 1)
Dim tmp As String
tmp = "Prezado(a) " & ActiveWorkbook.Worksheets("Contatos").Cells(cell.Row, 1).Offset(0, 0).Value & ", " & getPeriodo & "." & vbNewLine
SendKeys (tmp)
' Copy Cell content
With ActiveWorkbook.Worksheets("Email")
Set rngBody = .range(.range("B2"), .range("B2"))
End With
rngBody.Copy
' generate email body
MailBody = "[B]Prezado(a) " & ActiveWorkbook.Worksheets("Contatos").Cells(cell.Row, 1).Offset(0, 0).Value & ", " & getPeriodo & ".[/B]
" & vbNewLine & _
"" & vbTab & vbNewLine & "
"
If cell.Value Like "?*@?*.?*" And _
Application.WorksheetFunction.CountA(cellRange) > 0 Then
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = cell.Value
.subject = MailSubject
.HTMLBody = MailBody
'ou .Body ou .BodyPasteSpecial
' attach pdf dile on cell C2 and so on
.Attachments.Add "C:\Users\sixel\Desktop\DNV\Certificados\" & i & "_" & ActiveWorkbook.Worksheets("Contatos").Cells(2, 3).Value & ".pdf"
End If
'Next FileCell
.Display 'Or use .Send
End With
Set OutMail = Nothing
End If
i = i + 1
' ctl+v to outlook
SendKeys "^({v})", True
Next cell
Set OutApp = Nothing
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End If
Set rngBody = Nothing
End Sub
Private Sub Wait(ByVal nSec As Long)
nSec = nSec + Timer
While nSec > Timer
DoEvents
Wend
End Sub
Function getPeriodo() As String
If Now - Date < 0.5 Then
getPeriodo = "bom dia"
ElseIf Now - Date < 0.75 Then
getPeriodo = "boa tarde"
Else
getPeriodo = "boa noite "
End If
End Function
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