Trying to update this VBA code to have pargraph with page breaks

cedman02

New Member
Joined
Jul 9, 2019
Messages
6
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

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:

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Wherever you want the "paragraph breaks" if I am understanding you correctly, add in

& VbCrLf &


so for example...




Rich (BB code):
"Hello Sales Associate, " & vbcrlf & "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." & vbcrlf & "If customer appointments / addresses are missing, Please respond with the date, appointment time, and address of any missing appointments." & vbcrlf & "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."
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,164
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top