VBA Code for formatting spacing in Outlook

CarlStephens

Board Regular
Joined
Sep 25, 2020
Messages
128
Office Version
  1. 2016
Platform
  1. Windows
When using VBA code to create emails in Outlook, the email creates spaces between lines/rows which are not there on the spreadsheet. I have to use the Format Text tab in Outlook and select the Ad Space Before/After options to get the desired look, per the second screen shot. Anyone know of a fix for this?

1634561744790.png

1634561792449.png

Sub Outcome_Email()

Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String
Dim ws As Worksheet
Set ws = Sheets("RT1")
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)

With xOutMail
.Display
.To = ws.Range("C2").Value
.CC = ws.Range("C3").Value
.BCC = ws.Range("C4").Value
.Subject = ws.Range("C5").Value
.body = Join(Application.Transpose(ws.Range("C14", ws.Cells(ws.Rows.Count, "C").End(xlUp)).Value), vbCrLf)
On Error Resume Next
.Attachments.Add ws.Range("C7").Value
.Attachments.Add ws.Range("C8").Value
.Attachments.Add ws.Range("C9").Value
.Attachments.Add ws.Range("C10").Value
.Attachments.Add ws.Range("C11").Value
.Attachments.Add ws.Range("C12").Value
On Error GoTo 0


End With

On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
You've got vbCrLf as parameter for JOIN, so you will get extra line between lines.
 
Upvote 0
Check. I do not know what you've got in cells C14:CXX...
 
Upvote 0
Check. I do not know what you've got in cells C14:CXX...
I found the fix, which is to replace the vbCrLf with vbVerticalTab and eh voila, I got the spacing removed. Thank you for your inspiration.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,834
Messages
6,121,876
Members
449,056
Latest member
ruhulaminappu

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