Hello,
I'm am trying to update the this VBA code to send the body of the information with paragraph breaks within the body of the paragraph is there anyone that might help below is the VBA code that I am using
I'm am trying to update the this VBA code to send the body of the information with paragraph breaks within the body of the paragraph is there anyone that might help below is the VBA code that I am using
Code:
Private outlookApp As Object
Private newApp As Boolean
Public Sub EmailWorksheets()
Dim emailAddress As String
Dim tempPaths As New Collection
Dim tempPath As String
Dim sh As Worksheet
Dim j As Long
On Error GoTo ErrorHandler
Application.DisplayAlerts = False
Application.ScreenUpdating = False
InitializeOutlook
For Each sh In ThisWorkbook.Worksheets
tempPath = CopySheetToTempWorkbook(sh)
tempPaths.Add tempPath
emailAddress = sh.Range("B1").Text '<--- change to cell containing email address
SendEmail emailAddress, "Mileage Confirmation for Week of 07/07/2019", "Hello Sales Associate,Please see attached mileage confirmation with your calculated mileage for the past work week.Please confirm the mileage is correct by replying “Confirmed” to this email. If customer appointments / addresses are missing,Please respond with the date, appointment time, and address of any missing appointments.We will send you an updated mileage confirmation sheet.Please send your confirmation or updates as soon as possible.Final confirmed mileage received this week by Friday 12:00 pm Pacific time will be submitted for payment on this coming week’s check.", tempPath
Next sh
MsgBox tempPaths.Count & " e-mails were sent.", vbInformation
ExitHandler:
On Error Resume Next
TerminateOutlook
For j = tempPaths.Count To 1 Step -1
Kill tempPaths(j)
tempPaths.Remove j
Next j
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Set tempPaths = Nothing
Set sh = Nothing
Exit Sub
ErrorHandler:
MsgBox Err.Description, vbExclamation
Resume ExitHandler
End Sub
Private Sub InitializeOutlook()
On Error Resume Next
Set outlookApp = GetObject(, "Outlook.Application")
On Error GoTo 0
If outlookApp Is Nothing Then
Set outlookApp = CreateObject("Outlook.Application")
newApp = True
Else
newApp = False
End If
End Sub
Private Function CopySheetToTempWorkbook(ByVal sheetToCopy As Object) As String
Dim tempPath As String
tempPath = Environ("temp") & "" & sheetToCopy.Name & ".xlsx"
If Dir(tempPath) <> vbNullString Then Kill tempPath
sheetToCopy.Copy
ActiveWorkbook.SaveAs tempPath, xlOpenXMLWorkbook
ActiveWorkbook.Close
CopySheetToTempWorkbook = tempPath
End Function
Private Sub SendEmail( _
ByVal toRecipient As String, _
ByVal subjectText As String, _
ByVal bodyText As String, _
ByVal attachmentPath As String)
With outlookApp.CreateItem(0)
.to = toRecipient
.Subject = subjectText
.Body = bodyText
.Attachments.Add attachmentPath
.Send
End With
End Sub
Private Sub TerminateOutlook()
If newApp Then outlookApp.Quit
Set outlookApp = Nothing
End Sub
Last edited by a moderator: